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 
30 #include <map>
31 
32 namespace fawkes {
33 
34 template <typename KeyType, typename ValueType, typename LessKey = std::less<KeyType>>
35 class RWLockMap : public std::map<KeyType, ValueType, LessKey>
36 {
37 public:
38  RWLockMap();
39  RWLockMap(const RWLockMap<KeyType, ValueType, LessKey> &lm);
40  virtual ~RWLockMap();
41 
42  void lock_for_read();
43  void lock_for_write();
44  bool try_lock_for_read();
45  bool try_lock_for_write();
46  void unlock();
48 
49  void erase_locked(const KeyType &key);
50 
51 private:
52  RefPtr<ReadWriteLock> rwlock_;
53 };
54 
55 /** @class RWLockMap core/utils/rwlock_map.h
56  * Hash map with a lock.
57  * This class provides a map that has an intrinsic read/write lock. The lock can
58  * be applied with the regular locking methods.
59  *
60  * @see ReadWriteLock
61  * @ingroup FCL
62  * @author Tim Niemueller
63  */
64 
65 /** Constructor. */
66 template <typename KeyType, typename ValueType, typename LessKey>
68 {
69 }
70 
71 /** Copy constructor.
72  * @param lm RWLockMap to copy
73  */
74 template <typename KeyType, typename ValueType, typename LessKey>
76 : std::map<KeyType, ValueType, LessKey>::map(lm), rwlock_(new ReadWriteLock())
77 {
78 }
79 
80 /** Destructor. */
81 template <typename KeyType, typename ValueType, typename LessKey>
83 {
84 }
85 
86 /** Lock list for reading. */
87 template <typename KeyType, typename ValueType, typename LessKey>
88 void
90 {
91  rwlock_->lock_for_read();
92 }
93 
94 /** Lock list for writing. */
95 template <typename KeyType, typename ValueType, typename LessKey>
96 void
98 {
99  rwlock_->lock_for_write();
100 }
101 
102 /** Try to lock list for reading.
103  * @return true, if the lock has been aquired, false otherwise.
104  */
105 template <typename KeyType, typename ValueType, typename LessKey>
106 bool
108 {
109  return rwlock_->try_lock_for_read();
110 }
111 
112 /** Try to lock list for writing.
113  * @return true, if the lock has been aquired, false otherwise.
114  */
115 template <typename KeyType, typename ValueType, typename LessKey>
116 bool
118 {
119  return rwlock_->try_lock_for_write();
120 }
121 
122 /** Unlock list. */
123 template <typename KeyType, typename ValueType, typename LessKey>
124 void
126 {
127  return rwlock_->unlock();
128 }
129 
130 /** Remove item with lock.
131  * The map is automatically locked and unlocked during the removal.
132  * @param key key of the value to erase
133  */
134 template <typename KeyType, typename ValueType, typename LessKey>
135 void
137 {
138  rwlock_->lock_for_write();
139  std::map<KeyType, ValueType, LessKey>::erase(key);
140  rwlock_->unlock();
141 }
142 
143 /** Get access to the internal rwlock.
144  * Can be used with RwlockLocker.
145  * @return internal rwlock
146  */
147 template <typename KeyType, typename ValueType, typename LessKey>
150 {
151  return rwlock_;
152 }
153 
154 } // end namespace fawkes
155 
156 #endif
fawkes::RWLockMap::rwlock
RefPtr< ReadWriteLock > rwlock() const
Get access to the internal rwlock.
Definition: rwlock_map.h:155
fawkes::RWLockMap::~RWLockMap
virtual ~RWLockMap()
Destructor.
Definition: rwlock_map.h:88
fawkes::RWLockMap::erase_locked
void erase_locked(const KeyType &key)
Remove item with lock.
Definition: rwlock_map.h:142
fawkes::RefPtr
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:57
fawkes::RWLockMap::lock_for_write
void lock_for_write()
Lock list for writing.
Definition: rwlock_map.h:103
fawkes::RWLockMap::RWLockMap
RWLockMap()
Constructor.
Definition: rwlock_map.h:73
fawkes::RWLockMap::unlock
void unlock()
Unlock list.
Definition: rwlock_map.h:131
fawkes::ReadWriteLock
Definition: read_write_lock.h:37
fawkes
fawkes::RWLockMap
Definition: rwlock_map.h:41
fawkes::RWLockMap::try_lock_for_write
bool try_lock_for_write()
Try to lock list for writing.
Definition: rwlock_map.h:123
fawkes::RWLockMap::try_lock_for_read
bool try_lock_for_read()
Try to lock list for reading.
Definition: rwlock_map.h:113
fawkes::RWLockMap::lock_for_read
void lock_for_read()
Lock list for reading.
Definition: rwlock_map.h:95