#include <errno.h>
#include <sys/time.h>
#include "jthread/jsemaphore.h"
-#ifdef __MACH__
+#if defined(__MACH__) && defined(__APPLE__)
#include <unistd.h>
#endif
#define UNUSED(expr) do { (void)(expr); } while (0)
-#ifdef __MACH__
+#if defined(__MACH__) && defined(__APPLE__)
#undef sem_t
#undef sem_init
#undef sem_wait
int sem_init_retval = sem_init(&m_semaphore,0,0);
assert(sem_init_retval == 0);
UNUSED(sem_init_retval);
-#ifdef __MACH__
+#if defined(__MACH__) && defined(__APPLE__)
semcount = 0;
#endif
}
int sem_post_retval = sem_post(&m_semaphore);
assert(sem_post_retval == 0);
UNUSED(sem_post_retval);
-#ifdef __MACH__
+#if defined(__MACH__) && defined(__APPLE__)
pthread_mutex_lock(&semcount_mutex);
semcount++;
pthread_mutex_unlock(&semcount_mutex);
int sem_wait_retval = sem_wait(&m_semaphore);
assert(sem_wait_retval == 0);
UNUSED(sem_wait_retval);
-#ifdef __MACH__
+#if defined(__MACH__) && defined(__APPLE__)
pthread_mutex_lock(&semcount_mutex);
semcount--;
pthread_mutex_unlock(&semcount_mutex);
}
bool JSemaphore::Wait(unsigned int time_ms) {
-#ifdef __MACH__
+#if defined(__MACH__) && defined(__APPLE__)
mach_timespec_t waittime;
waittime.tv_sec = time_ms / 1000;
waittime.tv_nsec = 1000000 * (time_ms % 1000);
return false;
}
-#ifndef __MACH__
+#if !(defined(__MACH__) && defined(__APPLE__))
waittime.tv_nsec = ((time_ms % 1000) * 1000 * 1000) + (now.tv_usec * 1000);
waittime.tv_sec = (time_ms / 1000) + (waittime.tv_nsec / (1000*1000*1000)) + now.tv_sec;
waittime.tv_nsec %= 1000*1000*1000;
#endif
errno = 0;
-#ifdef __MACH__
+#if defined(__MACH__) && defined(__APPLE__)
int sem_wait_retval = semaphore_timedwait(m_semaphore, waittime);
if (sem_wait_retval == KERN_OPERATION_TIMED_OUT) {
errno = ETIMEDOUT;
if (sem_wait_retval == 0)
{
-#ifdef __MACH__
+#if defined(__MACH__) && defined(__APPLE__)
pthread_mutex_lock(&semcount_mutex);
semcount--;
pthread_mutex_unlock(&semcount_mutex);
int JSemaphore::GetValue() {
int retval = 0;
-#ifdef __MACH__
+#if defined(__MACH__) && defined(__APPLE__)
pthread_mutex_lock(&semcount_mutex);
retval = semcount;
pthread_mutex_unlock(&semcount_mutex);
#include <unistd.h>
#include <stdint.h> //for uintptr_t
- #if (defined(linux) || defined(__linux)) && !defined(_GNU_SOURCE)
+#if (defined(linux) || defined(__linux) || defined(__GNU__)) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#else // Posix
#include <sys/time.h>
#include <time.h>
-#ifdef __MACH__
+#if defined(__MACH__) && defined(__APPLE__)
#include <mach/clock.h>
#include <mach/mach.h>
#endif
{
struct timespec ts;
// from http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
-#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
+#if defined(__MACH__) && defined(__APPLE__) // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
inline void setThreadName(const char *name) {
pthread_setname_np(name);
}
-#elif defined(_WIN32)
+#elif defined(_WIN32) || defined(__GNU__)
inline void setThreadName(const char* name) {}
#else
#warning "Unrecognized platform, thread names will not be available."