aboutsummaryrefslogtreecommitdiffstats
path: root/common/threads.cpp
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2018-11-27 13:41:30 -0800
committerChris Robinson <[email protected]>2018-11-27 13:41:30 -0800
commitf26083e9edc6491f553aae951886d89b70288528 (patch)
tree1bb2b82ec786cbe2a98a3a5066c0be7c52dff1d1 /common/threads.cpp
parent3f745be1dc4df7ffeec89f1d90af41e139fb49db (diff)
Make and use a semaphore class
Diffstat (limited to 'common/threads.cpp')
-rw-r--r--common/threads.cpp50
1 files changed, 31 insertions, 19 deletions
diff --git a/common/threads.cpp b/common/threads.cpp
index b4f471d7..c9478871 100644
--- a/common/threads.cpp
+++ b/common/threads.cpp
@@ -22,25 +22,7 @@
#include "threads.h"
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-
-
-#ifndef UNUSED
-#if defined(__cplusplus)
-#define UNUSED(x)
-#elif defined(__GNUC__)
-#define UNUSED(x) UNUSED_##x __attribute__((unused))
-#elif defined(__LCLINT__)
-#define UNUSED(x) /*@unused@*/ x
-#else
-#define UNUSED(x) x
-#endif
-#endif
-
-
-#define THREAD_STACK_SIZE (2*1024*1024) /* 2MB */
+#include <system_error>
#ifdef _WIN32
@@ -116,6 +98,7 @@ int alsem_trywait(alsem_t *sem)
#include <sys/time.h>
#include <unistd.h>
+#include <errno.h>
#include <pthread.h>
#ifdef HAVE_PTHREAD_NP_H
#include <pthread_np.h>
@@ -210,3 +193,32 @@ int alsem_trywait(alsem_t *sem)
#endif /* __APPLE__ */
#endif
+
+
+namespace al {
+
+semaphore::semaphore(unsigned int initial)
+{
+ if(alsem_init(&mSem, initial) != althrd_success)
+ throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
+}
+
+semaphore::~semaphore()
+{ alsem_destroy(&mSem); }
+
+void semaphore::post()
+{
+ if(alsem_post(&mSem) != althrd_success)
+ throw std::system_error(std::make_error_code(std::errc::value_too_large));
+}
+
+void semaphore::wait() noexcept
+{
+ while(alsem_wait(&mSem) == -2) {
+ }
+}
+
+int semaphore::trywait() noexcept
+{ return alsem_wait(&mSem) == althrd_success; }
+
+} // namespace al