Rename set() to assign()
[junction.git] / junction / extra / impl / MapAdapter_Linear_Mutex.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_MUTEX_H
14 #define JUNCTION_EXTRA_IMPL_MAPADAPTER_LINEAR_MUTEX_H
15
16 #include <junction/Core.h>
17 #include <junction/SingleMap_Linear.h>
18 #include <turf/Mutex.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 + Mutex";
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::Mutex m_mutex;
49         SingleMap_Linear<u32, void*> m_map;
50
51     public:
52         Map(ureg capacity) : m_map(capacity) {
53         }
54
55         void assign(u32 key, void* value) {
56             turf::LockGuard<turf::Mutex> guard(m_mutex);
57             m_map.assign(key, value);
58         }
59
60         void* get(u32 key) {
61             turf::LockGuard<turf::Mutex> guard(m_mutex);
62             return m_map.get(key);
63         }
64
65         void* erase(u32 key) {
66             turf::LockGuard<turf::Mutex> guard(m_mutex);
67             return 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_MUTEX_H