--- old/src/os/linux/vm/os_linux.cpp 2015-06-12 14:46:28.764148010 +0200 +++ new/src/os/linux/vm/os_linux.cpp 2015-06-12 14:46:28.620143302 +0200 @@ -69,6 +69,7 @@ #include "utilities/elfFile.hpp" #include "utilities/growableArray.hpp" #include "utilities/macros.hpp" +#include "utilities/semaphore.hpp" #include "utilities/vmError.hpp" // put OS-includes here @@ -2393,20 +2394,12 @@ return CAST_FROM_FN_PTR(void*, UserHandler); } -class Semaphore : public StackObj { - public: - Semaphore(); - ~Semaphore(); - void signal(); - void wait(); - bool trywait(); - bool timedwait(unsigned int sec, int nsec); - private: - sem_t _semaphore; -}; +Semaphore::Semaphore(uint value, uint max) { + guarantee(value <= max, "value lower than max"); + guarantee(max == Semaphore::NoMaxCount || max <= SEM_VALUE_MAX, "Max value set too high"); -Semaphore::Semaphore() { - sem_init(&_semaphore, 0, 0); + int ret = sem_init(&_semaphore, 0, value); + guarantee(ret == 0, "Failed to initialize semaphore"); } Semaphore::~Semaphore() { @@ -2414,11 +2407,20 @@ } void Semaphore::signal() { - sem_post(&_semaphore); + int ret = sem_post(&_semaphore); + guarantee(ret == 0, err_msg("sem_post failed: %d", ret)); +} + +void Semaphore::signal(uint count) { + for (uint i = 0; i < count; i++) { + signal(); + } } void Semaphore::wait() { - sem_wait(&_semaphore); + while (sem_wait(&_semaphore) == -1 && errno == EINTR) { + // Retry if the wait was interrupted by a signal. + } } bool Semaphore::trywait() {