17#if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)) && !defined(__CYGWIN__)
19#elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
28class NamedCriticalSection::Impl
31 Impl(
const std::string& name, uint32_t spin) : _shared(name), _event(name +
"_event"), _spin(spin) {}
35 if (_shared->thread_id == Thread::CurrentThreadId())
36 fatality(SystemException(
"Named critical section should not be destroyed if our thread owns it!"));
39 const std::string& name()
const
41 return _shared.name();
49 bool TryLock(uint32_t spin)
51 uint32_t iteration = 0;
52 uint64_t thread_id = Thread::CurrentThreadId();
57#if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)) && !defined(__CYGWIN__)
58 if (__sync_bool_compare_and_swap(&_shared->lock_count, 0, 1))
59#elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
60 if (InterlockedCompareExchange(&_shared->lock_count, 1, 0) == 0)
64 _shared->thread_id = thread_id;
65 _shared->recurse_count = 1;
68 else if (_shared->thread_id == thread_id)
71#if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)) && !defined(__CYGWIN__)
72 __sync_add_and_fetch(&_shared->lock_count, 1);
73#elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
74 InterlockedIncrement(&_shared->lock_count);
76 _shared->recurse_count++;
79 else if (iteration++ >= spin)
96 uint64_t thread_id = Thread::CurrentThreadId();
98#if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)) && !defined(__CYGWIN__)
99 if (__sync_add_and_fetch(&_shared->lock_count, 1) == 1)
100#elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
101 if (InterlockedIncrement(&_shared->lock_count) == 1)
105 _shared->thread_id = thread_id;
106 _shared->recurse_count = 1;
110 if (_shared->thread_id == thread_id)
113 _shared->recurse_count++;
121 _shared->thread_id = thread_id;
122 _shared->recurse_count = 1;
129 if (_shared->thread_id != Thread::CurrentThreadId())
130 throwex SystemException(
"Named critical section can not be unlocked from other thread!");
133 if (--_shared->recurse_count > 0)
136#if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)) && !defined(__CYGWIN__)
137 __sync_sub_and_fetch(&_shared->lock_count, 1);
138#elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
139 InterlockedDecrement(&_shared->lock_count);
145 _shared->thread_id = 0;
147#if (defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)) && !defined(__CYGWIN__)
148 if (__sync_sub_and_fetch(&_shared->lock_count, 1) > 0)
149#elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
150 if (InterlockedDecrement(&_shared->lock_count) > 0)
161 struct CriticalSectionHeader
163 volatile uint64_t lock_count;
167 CriticalSectionHeader() : lock_count(0), recurse_count(0), thread_id(0) {}
171 SharedType<CriticalSectionHeader> _shared;
172 NamedEventAutoReset _event;
182 static_assert((StorageSize >=
sizeof(Impl)),
"NamedCriticalSection::StorageSize must be increased!");
183 static_assert(((StorageAlign %
alignof(Impl)) == 0),
"NamedCriticalSection::StorageAlign must be adjusted!");
186 new(&_storage)Impl(
name, 4000);
192 reinterpret_cast<Impl*
>(&_storage)->~Impl();
bool TryLock()
Try to acquire critical section without block.
void Lock()
Acquire critical section with block.
void Unlock()
Release critical section.
const std::string & name() const
Get the critical section name.
NamedCriticalSection(const std::string &name)
Default class constructor.
bool TryLockFor(const Timespan ×pan)
Try to acquire critical section for the given timespan.
High resolution timestamp.
static void Yield() noexcept
Yield to other threads.
Aligned storage validator.
#define throwex
Throw extended exception macro.
Fatal abort execution definition.
#define fatality(...)
Fatal abort execution extended macro.
Named critical section synchronization primitive definition.
Named auto-reset event synchronization primitive definition.
C++ Common project definitions.
Shared memory type definition.
Aligned storage validator definition.