--- old/src/os/bsd/vm/os_bsd.cpp 2015-06-12 14:46:28.424136896 +0200 +++ new/src/os/bsd/vm/os_bsd.cpp 2015-06-12 14:46:28.296132712 +0200 @@ -66,6 +66,7 @@ #include "utilities/defaultStream.hpp" #include "utilities/events.hpp" #include "utilities/growableArray.hpp" +#include "utilities/semaphore.hpp" #include "utilities/vmError.hpp" // put OS-includes here @@ -1941,36 +1942,19 @@ // Bsd(POSIX) specific hand shaking semaphore. #ifdef __APPLE__ -typedef semaphore_t os_semaphore_t; - #define SEM_INIT(sem, value) semaphore_create(mach_task_self(), &sem, SYNC_POLICY_FIFO, value) #define SEM_WAIT(sem) semaphore_wait(sem) #define SEM_POST(sem) semaphore_signal(sem) #define SEM_DESTROY(sem) semaphore_destroy(mach_task_self(), sem) #else -typedef sem_t os_semaphore_t; - #define SEM_INIT(sem, value) sem_init(&sem, 0, value) #define SEM_WAIT(sem) sem_wait(&sem) #define SEM_POST(sem) sem_post(&sem) #define SEM_DESTROY(sem) sem_destroy(&sem) #endif -class Semaphore : public StackObj { - public: - Semaphore(); - ~Semaphore(); - void signal(); - void wait(); - bool trywait(); - bool timedwait(unsigned int sec, int nsec); - private: - jlong currenttime() const; - os_semaphore_t _semaphore; -}; - -Semaphore::Semaphore() : _semaphore(0) { - SEM_INIT(_semaphore, 0); +Semaphore::Semaphore(uint value, uint max) : _semaphore(0) { + SEM_INIT(_semaphore, value); } Semaphore::~Semaphore() { @@ -1981,11 +1965,23 @@ SEM_POST(_semaphore); } +void Semaphore::signal(uint count) { + for (uint i = 0; i < count; i++) { + signal(); + } +} + void Semaphore::wait() { - SEM_WAIT(_semaphore); +#ifdef __APPLE__ + while (SEM_WAIT(_semaphore) == KERN_ABORTED) { +#else + while (SEM_WAIT(_semaphore) == -1 && errno == EINTR) { +#endif + // Semaphore was interrupted. Retry. + } } -jlong Semaphore::currenttime() const { +static jlong semaphore_currenttime() { struct timeval tv; gettimeofday(&tv, NULL); return (tv.tv_sec * NANOSECS_PER_SEC) + (tv.tv_usec * 1000); @@ -2002,13 +1998,13 @@ waitspec.tv_sec = sec; waitspec.tv_nsec = nsec; - jlong starttime = currenttime(); + jlong starttime = semaphore_currenttime(); kr = semaphore_timedwait(_semaphore, waitspec); while (kr == KERN_ABORTED) { jlong totalwait = (sec * NANOSECS_PER_SEC) + nsec; - jlong current = currenttime(); + jlong current = semaphore_currenttime(); jlong passedtime = current - starttime; if (passedtime >= totalwait) {