--- old/src/os/bsd/vm/os_bsd.cpp 2015-06-17 19:47:26.328443645 +0200 +++ new/src/os/bsd/vm/os_bsd.cpp 2015-06-17 19:47:26.188438901 +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,22 @@ // 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; -}; +#ifdef __APPLE__ +// OS X doesn't support unamed POSIX semaphores, so the implementation in os_posix.cpp can't be used. -Semaphore::Semaphore() : _semaphore(0) { - SEM_INIT(_semaphore, 0); +Semaphore::Semaphore(uint value) : _semaphore(0) { + SEM_INIT(_semaphore, value); } Semaphore::~Semaphore() { @@ -1981,79 +1968,82 @@ SEM_POST(_semaphore); } -void Semaphore::wait() { - SEM_WAIT(_semaphore); +void Semaphore::signal(uint count) { + for (uint i = 0; i < count; i++) { + signal(); + } } -jlong Semaphore::currenttime() const { - struct timeval tv; - gettimeofday(&tv, NULL); - return (tv.tv_sec * NANOSECS_PER_SEC) + (tv.tv_usec * 1000); +void Semaphore::wait() { + while (SEM_WAIT(_semaphore) == KERN_ABORTED) { + // Semaphore was interrupted. Retry. + } } -#ifdef __APPLE__ -bool Semaphore::trywait() { - return timedwait(0, 0); -} +class BsdSemaphore : public Semaphore { + private: + static jlong currenttime() { + struct timeval tv; + gettimeofday(&tv, NULL); + return (tv.tv_sec * NANOSECS_PER_SEC) + (tv.tv_usec * 1000); + } -bool Semaphore::timedwait(unsigned int sec, int nsec) { - kern_return_t kr = KERN_ABORTED; - mach_timespec_t waitspec; - waitspec.tv_sec = sec; - waitspec.tv_nsec = nsec; + public: + BsdSemaphore(uint value = 0) : Semaphore(value) {} - jlong starttime = currenttime(); + bool trywait() { + return timedwait(0, 0); + } - kr = semaphore_timedwait(_semaphore, waitspec); - while (kr == KERN_ABORTED) { - jlong totalwait = (sec * NANOSECS_PER_SEC) + nsec; + bool timedwait(unsigned int sec, int nsec) { + kern_return_t kr = KERN_ABORTED; + mach_timespec_t waitspec; + waitspec.tv_sec = sec; + waitspec.tv_nsec = nsec; - jlong current = currenttime(); - jlong passedtime = current - starttime; + jlong starttime = currenttime(); - if (passedtime >= totalwait) { - waitspec.tv_sec = 0; - waitspec.tv_nsec = 0; - } else { - jlong waittime = totalwait - (current - starttime); - waitspec.tv_sec = waittime / NANOSECS_PER_SEC; - waitspec.tv_nsec = waittime % NANOSECS_PER_SEC; + kr = semaphore_timedwait(_semaphore, waitspec); + while (kr == KERN_ABORTED) { + jlong totalwait = (sec * NANOSECS_PER_SEC) + nsec; + + jlong current = currenttime(); + jlong passedtime = current - starttime; + + if (passedtime >= totalwait) { + waitspec.tv_sec = 0; + waitspec.tv_nsec = 0; + } else { + jlong waittime = totalwait - (current - starttime); + waitspec.tv_sec = waittime / NANOSECS_PER_SEC; + waitspec.tv_nsec = waittime % NANOSECS_PER_SEC; + } + + kr = semaphore_timedwait(_semaphore, waitspec); } - kr = semaphore_timedwait(_semaphore, waitspec); + return kr == KERN_SUCCESS; } - - return kr == KERN_SUCCESS; -} +}; #else -bool Semaphore::trywait() { - return sem_trywait(&_semaphore) == 0; -} +class BsdSemaphore : public os::PosixSemaphore { + public: + BsdSemaphore(uint value = 0) : os::PosixSemaphore(value) {} -bool Semaphore::timedwait(unsigned int sec, int nsec) { - struct timespec ts; - unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec); + bool BsdSemaphore::timedwait(unsigned int sec, int nsec) { + struct timespec ts; + unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec); - while (1) { - int result = sem_timedwait(&_semaphore, &ts); - if (result == 0) { - return true; - } else if (errno == EINTR) { - continue; - } else if (errno == ETIMEDOUT) { - return false; - } else { - return false; - } + return os::PosixSemaphore::timedwait(ts); } -} +}; #endif // __APPLE__ static os_semaphore_t sig_sem; -static Semaphore sr_semaphore; +static BsdSemaphore sr_semaphore; void os::signal_init_pd() { // Initialize signal structures