semaphore.hpp
Go to the documentation of this file.
1 #pragma once
2 
11 #include <condition_variable>
12 #include <mutex>
13 
14 namespace cras
15 {
16 
23 {
24 public:
29  explicit ReverseSemaphore(bool waitZeroAtDestroy = true);
30 
35 
40  bool acquire();
41 
49  void release();
50 
56  bool waitZero();
57 
61  void disable();
62 
66  void enable();
67 
71  bool isEnabled() const;
72 
78  size_t getCount() const;
79 
80 private:
83 
85  bool isDestroying {false};
86 
88  volatile size_t count {0};
89 
91  volatile bool disabled {false};
92 
94  mutable ::std::mutex mutex;
95 
97  ::std::condition_variable cv;
98 };
99 
108 template <typename T>
110 {
111 public:
113  {
114  this->acquireSucceeded = this->semaphore.acquire();
115  }
116 
118  {
119  if (this->acquireSucceeded)
120  this->semaphore.release();
121  }
122 
128  bool acquired() const
129  {
130  return this->acquireSucceeded;
131  }
132 
133 private:
136 
138  bool acquireSucceeded {false};
139 };
140 }
cras::SemaphoreGuard
RAII guard for operations with a semaphore. On creation, the semaphore is acquired,...
Definition: semaphore.hpp:109
cras
Definition: any.hpp:15
cras::SemaphoreGuard::SemaphoreGuard
SemaphoreGuard(T &semaphore)
Definition: semaphore.hpp:112
cras::ReverseSemaphore::getCount
size_t getCount() const
Get the current number of unreleased acquire()s.
cras::ReverseSemaphore::release
void release()
Release the semaphore (decrease its count).
cras::ReverseSemaphore::cv
::std::condition_variable cv
Condition variable used for signalling between release() and waitZero().
Definition: semaphore.hpp:97
cras::ReverseSemaphore::waitZeroAtDestroy
bool waitZeroAtDestroy
Whether to wait for zero when the object is being destroyed.
Definition: semaphore.hpp:82
cras::ReverseSemaphore::mutex
mutable ::std::mutex mutex
Mutex protecting cv, count and disabled.
Definition: semaphore.hpp:94
cras::ReverseSemaphore::waitZero
bool waitZero()
Wait until the internal count reaches zero.
cras::ReverseSemaphore::acquire
bool acquire()
Acquire the semaphore (increase its count). This method never blocks for long.
cras::ReverseSemaphore::disabled
volatile bool disabled
Whether the semaphore is disabled.
Definition: semaphore.hpp:91
cras::ReverseSemaphore::enable
void enable()
Enable the semaphore. Calling acquire() works normally after this call.
cras::ReverseSemaphore::count
volatile size_t count
The internal count of the semaphore.
Definition: semaphore.hpp:88
cras::ReverseSemaphore::isDestroying
bool isDestroying
True if the destructor has begun.
Definition: semaphore.hpp:85
cras::ReverseSemaphore::~ReverseSemaphore
~ReverseSemaphore()
Destroys this semaphore. Internally blocks it and waits for zero count.
cras::SemaphoreGuard::acquired
bool acquired() const
Whether the semaphore acquisition succeeded when constructing this guard.
Definition: semaphore.hpp:128
cras::SemaphoreGuard::~SemaphoreGuard
~SemaphoreGuard()
Definition: semaphore.hpp:117
cras::SemaphoreGuard::semaphore
T & semaphore
The guarded semaphore.
Definition: semaphore.hpp:135
cras::ReverseSemaphore::ReverseSemaphore
ReverseSemaphore(bool waitZeroAtDestroy=true)
Create the semaphore (internal count is zero).
cras::ReverseSemaphore::isEnabled
bool isEnabled() const
Whether the semaphore is enabled or not.
cras::ReverseSemaphore::disable
void disable()
Disable the semaphore. All following acquire() calls will return immediately with false.
cras::SemaphoreGuard::acquireSucceeded
bool acquireSucceeded
Whether the acquire succeeded.
Definition: semaphore.hpp:138
cras::ReverseSemaphore
A reverse counting semaphore which can wait until its count is zero. Each acquire() increases this co...
Definition: semaphore.hpp:22


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Sun Jan 14 2024 03:48:14