1 /*------------------------------------------------------------------------
2 Junction: Concurrent data structures in C++
3 Copyright (c) 2016 Jeff Preshing
5 Distributed under the Simplified BSD License.
6 Original location: https://github.com/preshing/junction
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 ------------------------------------------------------------------------*/
13 #ifndef JUNCTION_STRIPED_MUTEX_H
14 #define JUNCTION_STRIPED_MUTEX_H
16 #include <junction/Core.h>
18 #if JUNCTION_USE_STRIPING
20 //-----------------------------------
22 //-----------------------------------
23 #include <junction/striped/AutoResetEvent.h>
31 turf::Atomic<sreg> m_status;
32 junction::striped::AutoResetEvent m_event;
35 while (m_status.exchange(1, turf::Acquire) >= 0)
40 Mutex() : m_status(-1), m_event(false) {
44 if (m_status.exchange(0, turf::Acquire) >= 0)
49 return (m_status.compareExchange(-1, 0, turf::Acquire) < 0);
53 if (m_status.exchange(-1, turf::Release) > 0)
58 } // namespace striped
59 } // namespace junction
61 #else // JUNCTION_USE_STRIPING
63 //-----------------------------------
65 //-----------------------------------
66 #include <turf/Mutex.h>
70 typedef turf::Mutex Mutex;
71 } // namespace striped
72 } // namespace junction
74 #endif // JUNCTION_USE_STRIPING
76 #endif // JUNCTION_STRIPED_MUTEX_H