< prev index next >
src/os/linux/vm/os_linux.cpp
Print this page
*** 67,76 ****
--- 67,77 ----
#include "utilities/defaultStream.hpp"
#include "utilities/events.hpp"
#include "utilities/elfFile.hpp"
#include "utilities/growableArray.hpp"
#include "utilities/macros.hpp"
+ #include "utilities/semaphore.hpp"
#include "utilities/vmError.hpp"
// put OS-includes here
# include <sys/types.h>
# include <sys/mman.h>
*** 2391,2426 ****
void* os::user_handler() {
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() {
! sem_init(&_semaphore, 0, 0);
}
Semaphore::~Semaphore() {
sem_destroy(&_semaphore);
}
void Semaphore::signal() {
! sem_post(&_semaphore);
}
void Semaphore::wait() {
! sem_wait(&_semaphore);
}
bool Semaphore::trywait() {
return sem_trywait(&_semaphore) == 0;
}
--- 2392,2428 ----
void* os::user_handler() {
return CAST_FROM_FN_PTR(void*, UserHandler);
}
! 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");
! int ret = sem_init(&_semaphore, 0, value);
! guarantee(ret == 0, "Failed to initialize semaphore");
}
Semaphore::~Semaphore() {
sem_destroy(&_semaphore);
}
void Semaphore::signal() {
! 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() {
! while (sem_wait(&_semaphore) == -1 && errno == EINTR) {
! // Retry if the wait was interrupted by a signal.
! }
}
bool Semaphore::trywait() {
return sem_trywait(&_semaphore) == 0;
}
< prev index next >