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
32namespace fawkes {
33
34template <typename KeyType, typename ValueType, typename LessKey = std::less<KeyType>>
35class RWLockMap : public std::map<KeyType, ValueType, LessKey>
36{
37public:
38 RWLockMap();
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
51private:
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. */
66template <typename KeyType, typename ValueType, typename LessKey>
68{
69}
70
71/** Copy constructor.
72 * @param lm RWLockMap to copy
73 */
74template <typename KeyType, typename ValueType, typename LessKey>
76: std::map<KeyType, ValueType, LessKey>::map(lm), rwlock_(new ReadWriteLock())
77{
78}
79
80/** Destructor. */
81template <typename KeyType, typename ValueType, typename LessKey>
83{
84}
85
86/** Lock list for reading. */
87template <typename KeyType, typename ValueType, typename LessKey>
88void
90{
91 rwlock_->lock_for_read();
92}
93
94/** Lock list for writing. */
95template <typename KeyType, typename ValueType, typename LessKey>
96void
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 */
105template <typename KeyType, typename ValueType, typename LessKey>
106bool
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 */
115template <typename KeyType, typename ValueType, typename LessKey>
116bool
118{
119 return rwlock_->try_lock_for_write();
120}
121
122/** Unlock list. */
123template <typename KeyType, typename ValueType, typename LessKey>
124void
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 */
134template <typename KeyType, typename ValueType, typename LessKey>
135void
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 */
147template <typename KeyType, typename ValueType, typename LessKey>
150{
151 return rwlock_;
152}
153
154} // end namespace fawkes
155
156#endif
Hash map with a lock.
Definition: rwlock_map.h:36
RWLockMap()
Constructor.
Definition: rwlock_map.h:67
bool try_lock_for_write()
Try to lock list for writing.
Definition: rwlock_map.h:117
void lock_for_read()
Lock list for reading.
Definition: rwlock_map.h:89
void erase_locked(const KeyType &key)
Remove item with lock.
Definition: rwlock_map.h:136
void unlock()
Unlock list.
Definition: rwlock_map.h:125
RefPtr< ReadWriteLock > rwlock() const
Get access to the internal rwlock.
Definition: rwlock_map.h:149
bool try_lock_for_read()
Try to lock list for reading.
Definition: rwlock_map.h:107
void lock_for_write()
Lock list for writing.
Definition: rwlock_map.h:97
virtual ~RWLockMap()
Destructor.
Definition: rwlock_map.h:82
Read/write lock to allow multiple readers but only a single writer on the resource at a time.
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:50
Fawkes library namespace.