summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/sys/RefCountedMap.h
blob: 20415486675f2a2858bad939b2fddd36a95239d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef QPID_SYS_REFCOUNTEDMAP_H
#define QPID_SYS_REFCOUNTEDMAP_H

/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include "qpid/sys/Mutex.h"
#include "qpid/RefCounted.h"

#include <map>

namespace qpid {
namespace sys {

/**
 * A thread-safe, RefCounted map of RefCounted entries.  Entries are
 * automatically erased when released The entire map is released when
 * all its entries are erased.
 *
 * The assumption is that some other object/thread holds an iterator
 * to each entry for as long as it is useful.
 *
 * The map can be cleared with close()
 *
 * WARNING: Assigning an intrusive_ptr<D> returned by the map locks the
 * map.  To avoid deadlock, you MUST NOT modify an iterator while
 * holding another lock that could be locked as a result of erasing
 * the entry and destroying its value.
 *
 * @param D must be public RefCounted 
 */
template <class Key, class Data>
class RefCountedMap : public RefCounted
{
  public:
    typedef intrusive_ptr<Data> DataPtr;

  private:
    struct Entry : public Data {
        typedef typename RefCountedMap::Iterator Iterator;
        intrusive_ptr<RefCountedMap> map;
        Iterator self;
        void init(intrusive_ptr<RefCountedMap> m, Iterator s) {
            map=m; self=s;
        }
        void released() const {
            if (map) {
                intrusive_ptr<RefCountedMap> protect(map);
                map->map.erase(self);
            }
        }
    };

    typedef std::map<Key,Entry> Map;
    typedef typename Map::iterator Iterator;

    typedef Mutex::ScopedLock Lock;
    struct OpenLock : public Lock {
        OpenLock(RefCountedMap& m) : Lock(m.lock) { assert(!m.closed); }
    };
    
    DataPtr ptr_(Iterator i) { return i==map.end() ? 0 : &i->second; }

    Mutex lock;
    Map map;
    bool closed;
    
  friend struct Entry;
  friend class iterator;

  public:
    RefCountedMap() : closed(false) {}
    
    /** Return 0 if not found
     * @pre !isClosed()
     */
    DataPtr find(const Key& k) {
        OpenLock l(*this);
        return ptr_(map.find(k));
    }

    /** Find existing or create new entry for k 
     * @pre !isClosed()
     */
    DataPtr get(const Key& k)  {
        OpenLock l(*this);
        std::pair<Iterator,bool> ib=
            map.insert(std::make_pair(k, Entry()));
        if (ib.second)
            ib.first->second.init(this, ib.first);
        return ptr_(ib.first);
    }
    
    size_t size() { Lock l(lock); return map.size(); }

    bool empty() { return size() == 0u; }

    bool isClosed() { Lock l(lock); return closed; }
    
    /**
     * Close the map and call functor on each remaining entry.
     * Note the map will not be deleted until all entries are
     * released, the assumption is that functor takes some
     * action that will release the entry.
     *
     * close() does nothing if isClosed() 
     */
    template <class F>
    void close(F functor) {
        Lock l(lock);
        if (closed) return;
        closed=true;            // No more inserts
        intrusive_ptr<RefCountedMap> protect(this);
        Iterator i=map.begin();
        while (i != map.end()) {
            Iterator j=i;
            ++i;
            functor(j->second); // May erase j
        }
    }
};


}} // namespace qpid::sys

#endif  /*!QPID_SYS_REFCOUNTEDMAP_H*/