8c0855932a21882311a2c984c8fba7a5946c3c9b
[junction.git] / junction / extra / impl / MapAdapter_Linear_RWLock.h
1 /*------------------------------------------------------------------------
2   Junction: Concurrent data structures in C++
3   Copyright (c) 2016 Jeff Preshing
4
5   Distributed under the Simplified BSD License.
6   Original location: https://github.com/preshing/junction
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the LICENSE file for more information.
11 ------------------------------------------------------------------------*/
12
13 #ifndef JUNCTION_EXTRA_IMPL_MAPADAPTER_LINEAR_RWLOCK_H
14 #define JUNCTION_EXTRA_IMPL_MAPADAPTER_LINEAR_RWLOCK_H
15
16 #include <junction/Core.h>
17 #include <junction/SingleMap_Linear.h>
18 #include <turf/RWLock.h>
19 #include <turf/Util.h>
20
21 namespace junction {
22 namespace extra {
23
24 class MapAdapter {
25 public:
26     static TURF_CONSTEXPR const char* MapName = "Single + RWLock";
27
28     MapAdapter(ureg) {
29     }
30
31     class ThreadContext {
32     public:
33         ThreadContext(MapAdapter&, ureg) {
34         }
35
36         void registerThread() {
37         }
38
39         void unregisterThread() {
40         }
41
42         void update() {
43         }
44     };
45
46     class Map {
47     private:
48         turf::RWLock m_rwLock;
49         SingleMap_Linear<u32, void*> m_map;
50
51     public:
52         Map(ureg capacity) : m_map(capacity) {
53         }
54
55         void set(u32 key, void* value) {
56             turf::ExclusiveLockGuard<turf::RWLock> guard(m_rwLock);
57             m_map.set(key, value);
58         }
59
60         void* get(u32 key) {
61             turf::SharedLockGuard<turf::RWLock> guard(m_rwLock);
62             return m_map.get(key);
63         }
64
65         void erase(u32 key) {
66             turf::ExclusiveLockGuard<turf::RWLock> guard(m_rwLock);
67             m_map.erase(key);
68         }
69     };
70
71     static ureg getInitialCapacity(ureg maxPopulation) {
72         return turf::util::roundUpPowerOf2(maxPopulation / 4);
73     }
74 };
75
76 } // namespace extra
77 } // namespace junction
78
79 #endif // JUNCTION_EXTRA_IMPL_MAPADAPTER_LINEAR_RWLOCK_H