Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
semaphore_fast.h
1#pragma once
2
3#include "gaia/config/config.h"
4
5#include <atomic>
6
7#include "gaia/mt/semaphore.h"
8
9namespace gaia {
10 namespace mt {
12 class GAIA_API SemaphoreFast final {
13 Semaphore m_sem;
14 std::atomic_int32_t m_cnt;
15
16 SemaphoreFast(SemaphoreFast&&) = delete;
17 SemaphoreFast(const SemaphoreFast&) = delete;
18 SemaphoreFast& operator=(SemaphoreFast&&) = delete;
19 SemaphoreFast& operator=(const SemaphoreFast&) = delete;
20
21 public:
22 explicit SemaphoreFast(int32_t count = 0): m_sem(count), m_cnt(0) {}
23 ~SemaphoreFast() = default;
24
26 void release(int32_t count = 1) {
27 const int32_t prevCount = m_cnt.fetch_add(count, std::memory_order_release);
28 int32_t toRelease = -prevCount;
29 if (count < toRelease)
30 toRelease = count;
31
32 if (toRelease > 0)
33 m_sem.release(toRelease);
34 }
35
39 bool wait() {
40 const int32_t oldCount = m_cnt.fetch_sub(1, std::memory_order_acquire);
41 bool result = true;
42 if (oldCount <= 0)
43 result = m_sem.wait();
44
45 return result;
46 }
47 };
48 } // namespace mt
49} // namespace gaia
An optimized version of Semaphore that avoids expensive system calls when the counter is greater than...
Definition semaphore_fast.h:12
bool wait()
Decrements semaphore count by 1. If the count is already 0, it waits indefinitely until semaphore cou...
Definition semaphore_fast.h:39
void release(int32_t count=1)
Increments semaphore count by the specified amount.
Definition semaphore_fast.h:26
Definition semaphore.h:18
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9