Adds garbage collection to map test cases
[junction.git] / junction / striped / ConditionBank.cpp
1 /*------------------------------------------------------------------------
2   Junction: Concurrent data structures in C++
3   Copyright (c) 2016-2017 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 #include <junction/Core.h>
14 #include <junction/striped/ConditionBank.h>
15
16 #if JUNCTION_USE_STRIPING
17
18 namespace junction {
19 namespace striped {
20
21 ConditionBank DefaultConditionBank;
22
23 ConditionBank::~ConditionBank() {
24     m_initSpinLock.lock();
25     ConditionPair* pairs = m_pairs.exchange(nullptr, turf::ConsumeRelease);
26     delete [] pairs;
27     m_initSpinLock.unlock();
28 }
29
30 ConditionPair* ConditionBank::initialize() {
31     m_initSpinLock.lock();
32     ConditionPair* pairs = m_pairs.loadNonatomic();
33     if (!pairs) {
34         pairs = new ConditionPair[SizeMask + 1];
35         m_pairs.store(pairs, turf::Release);
36     }
37     m_initSpinLock.unlock();
38     return pairs;
39 }
40
41 } // namespace striped
42 } // namespace junction
43
44 #endif // JUNCTION_USE_STRIPING