CyberEngineMkIII
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CYBSemaphore.cpp
Go to the documentation of this file.
1 #include "CYB.hpp"
3 
5  Lock();
7  WakeAll();
8  Unlock();
9 }
10 
11 void CYB::Platform::System::Semaphore::SignalN(const unsigned long long AN) noexcept {
12  Lock();
13  FWakeCount += AN;
14  for (auto I(0ULL); I < AN; ++I)
15  WakeOne();
16  Unlock();
17 }
18 
20  Lock();
21 
22  const auto WaitingFor(FServiceCount);
23  ++FServiceCount;
24 
25  API::Assert::NotEqual(FServiceCount, 0ULL);
26 
27  bool NotFirst(false);
28  while (FWakeCount < WaitingFor) {
29  if (NotFirst)
30  WakeOne();
31 
32  ++FSleepCount;
33  std::atomic_thread_fence(std::memory_order_release);
34  EnterCV();
35  std::atomic_thread_fence(std::memory_order_acquire);
36  --FSleepCount;
37 
38  NotFirst = true;
39  }
40 
41  if (FSleepCount == 0) {
42  //take this opportunity to reset the counters
43  //overflow will never happen in a million years, but it would be BAD
44  FServiceCount = 1;
45  FWakeCount = 0;
46  std::atomic_thread_fence(std::memory_order_release);
47  }
48 
49  Unlock();
50 }
unsigned long long FWakeCount
The number of threads that will be woken.
Definition: CYBSemaphore.hpp:9
void Unlock(void) noexcept
Unlock the mutex associated with the CV.
unsigned long long FSleepCount
The number of threads currently asleep.
Definition: CYBSemaphore.hpp:9
void Lock(void) noexcept
Lock the mutex associated with the CV.
static void NotEqual(const AType &ALHS, const AType &ARHS) noexcept
Unequivalence assertion function. May not be evaluated.
void WakeAll(void) noexcept
Wake all threads waiting on the condition variable.
void SignalN(const unsigned long long AN) noexceptfinaloverride
Signal N waiters of this semaphore. Will not signal more than are currently waiting.
Precompiled header for inter-engine operations.
void Wait(void) noexceptfinaloverride
Wait indefinitely for a signal from this semaphore. Will be placed last into the queue.
void SignalAll(void) noexceptfinaloverride
Signal all current waiters of this semaphore.
Definition: CYBSemaphore.cpp:4