Fawkes API  Fawkes Development Version
rwlock_map.h
1 
2 /***************************************************************************
3  * rwlock_map.h - Map with read/write lock
4  *
5  * Created: Tue Jan 13 16:29:33 2009
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __CORE_UTILS_RWLOCK_MAP_H_
25 #define __CORE_UTILS_RWLOCK_MAP_H_
26 
27 #include <core/threading/read_write_lock.h>
28 #include <core/utils/refptr.h>
29 #include <map>
30 
31 namespace fawkes {
32 
33 
34 template <typename KeyType,
35  typename ValueType,
36  typename LessKey = std::less<KeyType> >
37 class RWLockMap : public std::map<KeyType, ValueType, LessKey>
38 {
39  public:
40  RWLockMap();
42  virtual ~RWLockMap();
43 
44  void lock_for_read();
45  void lock_for_write();
46  bool try_lock_for_read();
47  bool try_lock_for_write();
48  void unlock();
50 
51  void erase_locked(const KeyType &key);
52 
53  private:
54  RefPtr<ReadWriteLock> __rwlock;
55 
56 };
57 
58 
59 /** @class RWLockMap core/utils/rwlock_map.h
60  * Hash map with a lock.
61  * This class provides a map that has an intrinsic read/write lock. The lock can
62  * be applied with the regular locking methods.
63  *
64  * @see ReadWriteLock
65  * @ingroup FCL
66  * @author Tim Niemueller
67  */
68 
69 
70 /** Constructor. */
71 template <typename KeyType, typename ValueType, typename LessKey>
73  : __rwlock(new ReadWriteLock())
74 {}
75 
76 
77 /** Copy constructor.
78  * @param lm RWLockMap to copy
79  */
80 template <typename KeyType, typename ValueType, typename LessKey>
82  : std::map<KeyType, ValueType, LessKey>::map(lm), __rwlock(new ReadWriteLock())
83 {}
84 
85 
86 /** Destructor. */
87 template <typename KeyType, typename ValueType, typename LessKey>
89 {}
90 
91 
92 /** Lock list for reading. */
93 template <typename KeyType, typename ValueType, typename LessKey>
94 void
96 {
97  __rwlock->lock_for_read();
98 }
99 
100 
101 /** Lock list for writing. */
102 template <typename KeyType, typename ValueType, typename LessKey>
103 void
105 {
106  __rwlock->lock_for_write();
107 }
108 
109 
110 /** Try to lock list for reading.
111  * @return true, if the lock has been aquired, false otherwise.
112  */
113 template <typename KeyType, typename ValueType, typename LessKey>
114 bool
116 {
117  return __rwlock->try_lock_for_read();
118 }
119 
120 
121 /** Try to lock list for writing.
122  * @return true, if the lock has been aquired, false otherwise.
123  */
124 template <typename KeyType, typename ValueType, typename LessKey>
125 bool
127 {
128  return __rwlock->try_lock_for_write();
129 }
130 
131 
132 /** Unlock list. */
133 template <typename KeyType, typename ValueType, typename LessKey>
134 void
136 {
137  return __rwlock->unlock();
138 }
139 
140 
141 /** Remove item with lock.
142  * The map is automatically locked and unlocked during the removal.
143  * @param key key of the value to erase
144  */
145 template <typename KeyType, typename ValueType, typename LessKey>
146 void
148 {
149  __rwlock->lock_for_write();
150  std::map<KeyType, ValueType, LessKey>::erase(key);
151  __rwlock->unlock();
152 }
153 
154 
155 /** Get access to the internal rwlock.
156  * Can be used with RwlockLocker.
157  * @return internal rwlock
158  */
159 template <typename KeyType, typename ValueType, typename LessKey>
162 {
163  return __rwlock;
164 }
165 
166 
167 } // end namespace fawkes
168 
169 #endif
bool try_lock_for_write()
Try to lock list for writing.
Definition: rwlock_map.h:126
RWLockMap()
Constructor.
Definition: rwlock_map.h:72
Hash map with a lock.
Definition: rwlock_map.h:37
bool try_lock_for_read()
Try to lock list for reading.
Definition: rwlock_map.h:115
Fawkes library namespace.
void lock_for_read()
Lock list for reading.
Definition: rwlock_map.h:95
STL namespace.
virtual ~RWLockMap()
Destructor.
Definition: rwlock_map.h:88
void unlock()
Unlock list.
Definition: rwlock_map.h:135
Read/write lock to allow multiple readers but only a single writer on the resource at a time...
RefPtr< ReadWriteLock > rwlock() const
Get access to the internal rwlock.
Definition: rwlock_map.h:161
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:49
void erase_locked(const KeyType &key)
Remove item with lock.
Definition: rwlock_map.h:147
void lock_for_write()
Lock list for writing.
Definition: rwlock_map.h:104