Initial commit
[junction.git] / junction / extra / impl / MapAdapter_LeapFrog.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_LEAPFROG_H
14 #define JUNCTION_EXTRA_IMPL_MAPADAPTER_LEAPFROG_H
15
16 #include <junction/Core.h>
17 #include <junction/QSBR.h>
18 #include <junction/ConcurrentMap_LeapFrog.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 = "Junction LeapFrog map";
27     
28     MapAdapter(ureg) {
29     }
30
31     class ThreadContext {
32     private:
33         QSBR::Context m_qsbrContext;
34         
35     public:
36         ThreadContext(MapAdapter&, ureg) {
37         }
38         
39         void registerThread() {
40             m_qsbrContext = DefaultQSBR.createContext();
41         }
42         
43         void unregisterThread() {
44             DefaultQSBR.destroyContext(m_qsbrContext);
45         }
46         
47         void update() {
48             DefaultQSBR.update(m_qsbrContext);
49         }
50     };
51     
52     typedef ConcurrentMap_LeapFrog<u32, void*> Map;
53
54     static ureg getInitialCapacity(ureg maxPopulation) {
55         return turf::util::roundUpPowerOf2(maxPopulation / 4);
56     }
57 };
58
59 } // namespace extra
60 } // namespace junction
61
62 #endif // JUNCTION_EXTRA_IMPL_MAPADAPTER_LEAPFROG_H