summaryrefslogtreecommitdiffstats
path: root/include/raul/Semaphore.hpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-01-02 14:46:29 +0100
committerDavid Robillard <d@drobilla.net>2021-01-02 14:46:29 +0100
commitbd0214b1da66225f410641692e89e492f668472a (patch)
tree80956f373070734dc14a7f103e5fa21a5baa0b6e /include/raul/Semaphore.hpp
parent5c263125aeae87dcd4694a4d3a4781bda7247a00 (diff)
downloadraul-bd0214b1da66225f410641692e89e492f668472a.tar.gz
raul-bd0214b1da66225f410641692e89e492f668472a.tar.bz2
raul-bd0214b1da66225f410641692e89e492f668472a.zip
Format all code with clang-format
Diffstat (limited to 'include/raul/Semaphore.hpp')
-rw-r--r--include/raul/Semaphore.hpp204
1 files changed, 104 insertions, 100 deletions
diff --git a/include/raul/Semaphore.hpp b/include/raul/Semaphore.hpp
index f87dac3..5e9303b 100644
--- a/include/raul/Semaphore.hpp
+++ b/include/raul/Semaphore.hpp
@@ -17,14 +17,14 @@
#define RAUL_SEMAPHORE_HPP
#ifdef __APPLE__
-# include <mach/mach.h>
+# include <mach/mach.h>
#elif defined(_WIN32)
-# define NOMINMAX
-# include <windows.h>
+# define NOMINMAX
+# include <windows.h>
#else
-# include <cerrno>
-# include <ctime>
-# include <semaphore.h>
+# include <cerrno>
+# include <ctime>
+# include <semaphore.h>
#endif
#include <chrono>
@@ -47,58 +47,57 @@ namespace Raul {
class Semaphore
{
public:
- /**
- Create a new semaphore.
+ /**
+ Create a new semaphore.
- Chances are you want 1 wait() per 1 post(), an initial value of 0.
- */
- explicit Semaphore(unsigned initial)
- : _sem()
- {
- if (!init(initial)) {
- throw std::runtime_error("Failed to create semaphore");
- }
- }
+ Chances are you want 1 wait() per 1 post(), an initial value of 0.
+ */
+ explicit Semaphore(unsigned initial)
+ : _sem()
+ {
+ if (!init(initial)) {
+ throw std::runtime_error("Failed to create semaphore");
+ }
+ }
- inline Semaphore(const Semaphore&) = delete;
- inline Semaphore& operator=(const Semaphore&) = delete;
+ inline Semaphore(const Semaphore&) = delete;
+ inline Semaphore& operator=(const Semaphore&) = delete;
- inline Semaphore(Semaphore&&) = delete;
- inline Semaphore& operator=(Semaphore&&) = delete;
+ inline Semaphore(Semaphore&&) = delete;
+ inline Semaphore& operator=(Semaphore&&) = delete;
- inline ~Semaphore() {
- destroy();
- }
+ inline ~Semaphore() { destroy(); }
- /** Destroy and reset to a new initial value. */
- inline void reset(unsigned initial) {
- destroy();
- init(initial);
- }
+ /** Destroy and reset to a new initial value. */
+ inline void reset(unsigned initial)
+ {
+ destroy();
+ init(initial);
+ }
- /** Post/Increment/Signal */
- inline void post();
+ /** Post/Increment/Signal */
+ inline void post();
- /** Wait/Decrement. Return false on error. */
- inline bool wait();
+ /** Wait/Decrement. Return false on error. */
+ inline bool wait();
- /** Attempt Wait/Decrement. Return true iff decremented. */
- inline bool try_wait();
+ /** Attempt Wait/Decrement. Return true iff decremented. */
+ inline bool try_wait();
- /** Wait for at most `ms` milliseconds. Return true iff decremented. */
- template<class Rep, class Period>
- inline bool timed_wait(const std::chrono::duration<Rep, Period>& wait);
+ /** Wait for at most `ms` milliseconds. Return true iff decremented. */
+ template<class Rep, class Period>
+ inline bool timed_wait(const std::chrono::duration<Rep, Period>& wait);
private:
- inline bool init(unsigned initial);
- inline void destroy();
+ inline bool init(unsigned initial);
+ inline void destroy();
#if defined(__APPLE__)
- semaphore_t _sem; // sem_t is a worthless broken mess on OSX
+ semaphore_t _sem; // sem_t is a worthless broken mess on OSX
#elif defined(_WIN32)
- HANDLE _sem; // types are overrated anyway
+ HANDLE _sem; // types are overrated anyway
#else
- sem_t _sem;
+ sem_t _sem;
#endif
};
@@ -107,52 +106,55 @@ private:
inline bool
Semaphore::init(unsigned initial)
{
- if (semaphore_create(mach_task_self(), &_sem, SYNC_POLICY_FIFO, int(initial))) {
- return false;
- }
- return true;
+ if (semaphore_create(
+ mach_task_self(), &_sem, SYNC_POLICY_FIFO, int(initial))) {
+ return false;
+ }
+ return true;
}
inline void
Semaphore::destroy()
{
- semaphore_destroy(mach_task_self(), _sem);
+ semaphore_destroy(mach_task_self(), _sem);
}
inline void
Semaphore::post()
{
- semaphore_signal(_sem);
+ semaphore_signal(_sem);
}
inline bool
Semaphore::wait()
{
- if (semaphore_wait(_sem) != KERN_SUCCESS) {
- return false;
- }
- return true;
+ if (semaphore_wait(_sem) != KERN_SUCCESS) {
+ return false;
+ }
+
+ return true;
}
inline bool
Semaphore::try_wait()
{
- const mach_timespec_t zero = { 0, 0 };
- return semaphore_timedwait(_sem, zero) == KERN_SUCCESS;
+ const mach_timespec_t zero = {0, 0};
+ return semaphore_timedwait(_sem, zero) == KERN_SUCCESS;
}
template<class Rep, class Period>
inline bool
Semaphore::timed_wait(const std::chrono::duration<Rep, Period>& wait)
{
- namespace chr = std::chrono;
+ namespace chr = std::chrono;
+
+ const chr::seconds sec(chr::duration_cast<chr::seconds>(wait));
+ const chr::nanoseconds nsec(wait - sec);
- const chr::seconds sec(chr::duration_cast<chr::seconds>(wait));
- const chr::nanoseconds nsec(wait - sec);
+ const mach_timespec_t t = {static_cast<unsigned>(sec.count()),
+ static_cast<int>(nsec.count())};
- const mach_timespec_t t = { static_cast<unsigned>(sec.count()),
- static_cast<int>(nsec.count()) };
- return semaphore_timedwait(_sem, t) == KERN_SUCCESS;
+ return semaphore_timedwait(_sem, t) == KERN_SUCCESS;
}
#elif defined(_WIN32)
@@ -160,109 +162,111 @@ Semaphore::timed_wait(const std::chrono::duration<Rep, Period>& wait)
inline bool
Semaphore::init(unsigned initial)
{
- if (!(_sem = CreateSemaphore(NULL, (LONG)initial, LONG_MAX, NULL))) {
- return false;
- }
- return true;
+ if (!(_sem = CreateSemaphore(NULL, (LONG)initial, LONG_MAX, NULL))) {
+ return false;
+ }
+
+ return true;
}
inline void
Semaphore::destroy()
{
- CloseHandle(_sem);
+ CloseHandle(_sem);
}
inline void
Semaphore::post()
{
- ReleaseSemaphore(_sem, 1, NULL);
+ ReleaseSemaphore(_sem, 1, NULL);
}
inline bool
Semaphore::wait()
{
- if (WaitForSingleObject(_sem, INFINITE) != WAIT_OBJECT_0) {
- return false;
- }
- return true;
+ if (WaitForSingleObject(_sem, INFINITE) != WAIT_OBJECT_0) {
+ return false;
+ }
+
+ return true;
}
inline bool
Semaphore::try_wait()
{
- return WaitForSingleObject(_sem, 0) == WAIT_OBJECT_0;
+ return WaitForSingleObject(_sem, 0) == WAIT_OBJECT_0;
}
template<class Rep, class Period>
inline bool
Semaphore::timed_wait(const std::chrono::duration<Rep, Period>& wait)
{
- namespace chr = std::chrono;
+ namespace chr = std::chrono;
- const chr::milliseconds ms(chr::duration_cast<chr::milliseconds>(wait));
- return WaitForSingleObject(
- _sem, static_cast<DWORD>(ms.count())) == WAIT_OBJECT_0;
+ const chr::milliseconds ms(chr::duration_cast<chr::milliseconds>(wait));
+ return WaitForSingleObject(_sem, static_cast<DWORD>(ms.count())) ==
+ WAIT_OBJECT_0;
}
-#else /* !defined(__APPLE__) && !defined(_WIN32) */
+#else /* !defined(__APPLE__) && !defined(_WIN32) */
inline bool
Semaphore::init(unsigned initial)
{
- return !sem_init(&_sem, 0, initial);
+ return !sem_init(&_sem, 0, initial);
}
inline void
Semaphore::destroy()
{
- sem_destroy(&_sem);
+ sem_destroy(&_sem);
}
inline void
Semaphore::post()
{
- sem_post(&_sem);
+ sem_post(&_sem);
}
inline bool
Semaphore::wait()
{
- while (sem_wait(&_sem)) {
- if (errno != EINTR) {
- return false; // We are all doomed
- }
- /* Otherwise, interrupted (rare/weird), so try again. */
- }
-
- return true;
+ while (sem_wait(&_sem)) {
+ if (errno != EINTR) {
+ return false; // We are all doomed
+ }
+ /* Otherwise, interrupted (rare/weird), so try again. */
+ }
+
+ return true;
}
inline bool
Semaphore::try_wait()
{
- return (sem_trywait(&_sem) == 0);
+ return (sem_trywait(&_sem) == 0);
}
template<class Rep, class Period>
inline bool
Semaphore::timed_wait(const std::chrono::duration<Rep, Period>& wait)
{
- namespace chr = std::chrono;
+ namespace chr = std::chrono;
- // Use clock_gettime to ensure sem_timedwait uses the same clock
- struct timespec time{};
- clock_gettime(CLOCK_REALTIME, &time);
+ // Use clock_gettime to ensure sem_timedwait uses the same clock
+ struct timespec time {};
+ clock_gettime(CLOCK_REALTIME, &time);
- const auto now(chr::seconds(time.tv_sec) + chr::nanoseconds(time.tv_nsec));
- const auto end(now + wait);
+ const auto now(chr::seconds(time.tv_sec) + chr::nanoseconds(time.tv_nsec));
+ const auto end(now + wait);
- const chr::seconds end_sec(chr::duration_cast<chr::seconds>(end));
- const chr::nanoseconds end_nsec(end - end_sec);
+ const chr::seconds end_sec(chr::duration_cast<chr::seconds>(end));
+ const chr::nanoseconds end_nsec(end - end_sec);
- const struct timespec ts_end = { static_cast<time_t>(end_sec.count()),
- static_cast<long>(end_nsec.count()) };
+ const struct timespec ts_end = {static_cast<time_t>(end_sec.count()),
+ static_cast<long>(end_nsec.count())};
- return (sem_timedwait(&_sem, &ts_end) == 0);
+ return (sem_timedwait(&_sem, &ts_end) == 0);
}
#endif