aboutsummaryrefslogtreecommitdiffstats
path: root/common/threads.cpp
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2018-11-27 14:50:41 -0800
committerChris Robinson <[email protected]>2018-11-27 14:52:04 -0800
commitc2da83dea86f57e3095a704ef1f9c7b01852cd03 (patch)
tree164ea68efa0f9abe77d6bf9e41ee4030800e8ce4 /common/threads.cpp
parent89abbe8d94436376c1dc00f5af226a79fe3bc811 (diff)
Avoid alsem* wrappers for al::semaphore
Diffstat (limited to 'common/threads.cpp')
-rw-r--r--common/threads.cpp143
1 files changed, 42 insertions, 101 deletions
diff --git a/common/threads.cpp b/common/threads.cpp
index c9478871..eaf2cbbc 100644
--- a/common/threads.cpp
+++ b/common/threads.cpp
@@ -22,14 +22,11 @@
#include "threads.h"
+#include <limits>
#include <system_error>
-#ifdef _WIN32
-
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#include <mmsystem.h>
+#ifdef _WIN32
void althrd_setname(const char *name)
{
@@ -59,52 +56,42 @@ void althrd_setname(const char *name)
#endif
}
+namespace al {
-int alsem_init(alsem_t *sem, unsigned int initial)
+semaphore::semaphore(unsigned int initial)
{
- *sem = CreateSemaphore(NULL, initial, INT_MAX, NULL);
- if(*sem != NULL) return althrd_success;
- return althrd_error;
+ if(initial > static_cast<unsigned int>(std::numeric_limits<int>::max()))
+ throw std::system_error(std::make_error_code(std::errc::value_too_large));
+ mSem = CreateSemaphore(nullptr, initial, std::numeric_limits<int>::max(), nullptr);
+ if(mSem == nullptr)
+ throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
}
-void alsem_destroy(alsem_t *sem)
-{
- CloseHandle(*sem);
-}
+semaphore::~semaphore()
+{ CloseHandle(mSem); }
-int alsem_post(alsem_t *sem)
+void semaphore::post()
{
- DWORD ret = ReleaseSemaphore(*sem, 1, NULL);
- if(ret) return althrd_success;
- return althrd_error;
+ if(!ReleaseSemaphore(mSem, 1, nullptr))
+ throw std::system_error(std::make_error_code(std::errc::value_too_large));
}
-int alsem_wait(alsem_t *sem)
-{
- DWORD ret = WaitForSingleObject(*sem, INFINITE);
- if(ret == WAIT_OBJECT_0) return althrd_success;
- return althrd_error;
-}
+void semaphore::wait() noexcept
+{ WaitForSingleObject(mSem, INFINITE); }
-int alsem_trywait(alsem_t *sem)
-{
- DWORD ret = WaitForSingleObject(*sem, 0);
- if(ret == WAIT_OBJECT_0) return althrd_success;
- if(ret == WAIT_TIMEOUT) return althrd_busy;
- return althrd_error;
-}
+int semaphore::trywait() noexcept
+{ return WaitForSingleObject(mSem, 0) == WAIT_OBJECT_0; }
+
+} // namespace al
#else
-#include <sys/time.h>
-#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#ifdef HAVE_PTHREAD_NP_H
#include <pthread_np.h>
#endif
-
void althrd_setname(const char *name)
{
#if defined(HAVE_PTHREAD_SETNAME_NP)
@@ -122,103 +109,57 @@ void althrd_setname(const char *name)
#endif
}
+namespace al {
#ifdef __APPLE__
-int alsem_init(alsem_t *sem, unsigned int initial)
+semaphore::semaphore(unsigned int initial)
{
- *sem = dispatch_semaphore_create(initial);
- return *sem ? althrd_success : althrd_error;
+ mSem = dispatch_semaphore_create(initial);
+ if(!mSem)
+ throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
}
-void alsem_destroy(alsem_t *sem)
-{
- dispatch_release(*sem);
-}
+semaphore::~semaphore()
+{ dispatch_release(mSem); }
-int alsem_post(alsem_t *sem)
-{
- dispatch_semaphore_signal(*sem);
- return althrd_success;
-}
+void semaphore::post()
+{ dispatch_semaphore_signal(mSem); }
-int alsem_wait(alsem_t *sem)
-{
- dispatch_semaphore_wait(*sem, DISPATCH_TIME_FOREVER);
- return althrd_success;
-}
+void semaphore::wait() noexcept
+{ dispatch_semaphore_wait(mSem, DISPATCH_TIME_FOREVER); }
-int alsem_trywait(alsem_t *sem)
-{
- long value = dispatch_semaphore_wait(*sem, DISPATCH_TIME_NOW);
- return value == 0 ? althrd_success : althrd_busy;
-}
+int semaphore::trywait() noexcept
+{ return dispatch_semaphore_wait(mSem, DISPATCH_TIME_NOW) == 0; }
#else /* !__APPLE__ */
-int alsem_init(alsem_t *sem, unsigned int initial)
-{
- if(sem_init(sem, 0, initial) == 0)
- return althrd_success;
- return althrd_error;
-}
-
-void alsem_destroy(alsem_t *sem)
-{
- sem_destroy(sem);
-}
-
-int alsem_post(alsem_t *sem)
-{
- if(sem_post(sem) == 0)
- return althrd_success;
- return althrd_error;
-}
-
-int alsem_wait(alsem_t *sem)
-{
- if(sem_wait(sem) == 0) return althrd_success;
- if(errno == EINTR) return -2;
- return althrd_error;
-}
-
-int alsem_trywait(alsem_t *sem)
-{
- if(sem_trywait(sem) == 0) return althrd_success;
- if(errno == EWOULDBLOCK) return althrd_busy;
- if(errno == EINTR) return -2;
- return althrd_error;
-}
-
-#endif /* __APPLE__ */
-
-#endif
-
-
-namespace al {
-
semaphore::semaphore(unsigned int initial)
{
- if(alsem_init(&mSem, initial) != althrd_success)
+ if(sem_init(&mSem, 0, initial) != 0)
throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
}
semaphore::~semaphore()
-{ alsem_destroy(&mSem); }
+{ sem_destroy(&mSem); }
void semaphore::post()
{
- if(alsem_post(&mSem) != althrd_success)
+ if(sem_post(&mSem) != 0)
throw std::system_error(std::make_error_code(std::errc::value_too_large));
}
void semaphore::wait() noexcept
{
- while(alsem_wait(&mSem) == -2) {
+ while(sem_wait(&mSem) == -1 && errno == EINTR) {
}
}
int semaphore::trywait() noexcept
-{ return alsem_wait(&mSem) == althrd_success; }
+{ return sem_trywait(&mSem) == 0; }
+
+#endif /* __APPLE__ */
} // namespace al
+
+#endif /* _WIN32 */