diff options
Diffstat (limited to 'src/threading/semaphore.cpp')
-rw-r--r-- | src/threading/semaphore.cpp | 74 |
1 files changed, 36 insertions, 38 deletions
diff --git a/src/threading/semaphore.cpp b/src/threading/semaphore.cpp index 8226bd9a0..ce22dcd05 100644 --- a/src/threading/semaphore.cpp +++ b/src/threading/semaphore.cpp @@ -23,38 +23,36 @@ with this program; if not, write to the Free Software Foundation, Inc., #include <cstdlib> #include <cassert> -#define UNUSED(expr) \ - do { \ - (void)(expr); \ - } while (0) +#define UNUSED(expr) do { (void)(expr); } while (0) #ifdef _WIN32 -#include <climits> -#define MAX_SEMAPHORE_COUNT LONG_MAX - 1 + #include <climits> + #define MAX_SEMAPHORE_COUNT LONG_MAX - 1 #else -#include <cerrno> -#include <sys/time.h> -#include <pthread.h> -#if defined(__MACH__) && defined(__APPLE__) -#include <mach/mach.h> -#include <mach/task.h> -#include <mach/semaphore.h> -#include <sys/semaphore.h> -#include <unistd.h> - -#undef sem_t -#undef sem_init -#undef sem_wait -#undef sem_post -#undef sem_destroy -#define sem_t semaphore_t -#define sem_init(s, p, c) semaphore_create(mach_task_self(), (s), 0, (c)) -#define sem_wait(s) semaphore_wait(*(s)) -#define sem_post(s) semaphore_signal(*(s)) -#define sem_destroy(s) semaphore_destroy(mach_task_self(), *(s)) -#endif + #include <cerrno> + #include <sys/time.h> + #include <pthread.h> + #if defined(__MACH__) && defined(__APPLE__) + #include <mach/mach.h> + #include <mach/task.h> + #include <mach/semaphore.h> + #include <sys/semaphore.h> + #include <unistd.h> + + #undef sem_t + #undef sem_init + #undef sem_wait + #undef sem_post + #undef sem_destroy + #define sem_t semaphore_t + #define sem_init(s, p, c) semaphore_create(mach_task_self(), (s), 0, (c)) + #define sem_wait(s) semaphore_wait(*(s)) + #define sem_post(s) semaphore_signal(*(s)) + #define sem_destroy(s) semaphore_destroy(mach_task_self(), *(s)) + #endif #endif + Semaphore::Semaphore(int val) { #ifdef _WIN32 @@ -66,6 +64,7 @@ Semaphore::Semaphore(int val) #endif } + Semaphore::~Semaphore() { #ifdef _WIN32 @@ -82,6 +81,7 @@ Semaphore::~Semaphore() #endif } + void Semaphore::post(unsigned int num) { assert(num > 0); @@ -96,6 +96,7 @@ void Semaphore::post(unsigned int num) #endif } + void Semaphore::wait() { #ifdef _WIN32 @@ -107,6 +108,7 @@ void Semaphore::wait() #endif } + bool Semaphore::wait(unsigned int time_ms) { #ifdef _WIN32 @@ -119,7 +121,7 @@ bool Semaphore::wait(unsigned int time_ms) return false; } #else -#if defined(__MACH__) && defined(__APPLE__) +# if defined(__MACH__) && defined(__APPLE__) mach_timespec_t wait_time; wait_time.tv_sec = time_ms / 1000; wait_time.tv_nsec = 1000000 * (time_ms % 1000); @@ -137,33 +139,29 @@ bool Semaphore::wait(unsigned int time_ms) if (ret) errno = EINVAL; } -#else +# else int ret; if (time_ms > 0) { struct timespec wait_time; struct timeval now; if (gettimeofday(&now, NULL) == -1) { - std::cerr << "Semaphore::wait(ms): Unable to get time with " - "gettimeofday!" - << std::endl; + std::cerr << "Semaphore::wait(ms): Unable to get time with gettimeofday!" << std::endl; abort(); } - wait_time.tv_nsec = - ((time_ms % 1000) * 1000 * 1000) + (now.tv_usec * 1000); - wait_time.tv_sec = (time_ms / 1000) + - (wait_time.tv_nsec / (1000 * 1000 * 1000)) + - now.tv_sec; + wait_time.tv_nsec = ((time_ms % 1000) * 1000 * 1000) + (now.tv_usec * 1000); + wait_time.tv_sec = (time_ms / 1000) + (wait_time.tv_nsec / (1000 * 1000 * 1000)) + now.tv_sec; wait_time.tv_nsec %= 1000 * 1000 * 1000; ret = sem_timedwait(&semaphore, &wait_time); } else { ret = sem_trywait(&semaphore); } -#endif +# endif assert(!ret || (errno == ETIMEDOUT || errno == EINTR || errno == EAGAIN)); return !ret; #endif } + |