< prev index next >
src/os/bsd/vm/os_bsd.cpp
Print this page
@@ -64,10 +64,11 @@
#include "services/runtimeService.hpp"
#include "utilities/decoder.hpp"
#include "utilities/defaultStream.hpp"
#include "utilities/events.hpp"
#include "utilities/growableArray.hpp"
+#include "utilities/semaphore.hpp"
#include "utilities/vmError.hpp"
// put OS-includes here
# include <sys/types.h>
# include <sys/mman.h>
@@ -1939,66 +1940,64 @@
// a counter for each possible signal value
static volatile jint pending_signals[NSIG+1] = { 0 };
// 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() {
SEM_DESTROY(_semaphore);
}
void Semaphore::signal() {
SEM_POST(_semaphore);
}
+void Semaphore::signal(uint count) {
+ for (uint i = 0; i < count; i++) {
+ signal();
+ }
+}
+
void Semaphore::wait() {
- SEM_WAIT(_semaphore);
+ while (SEM_WAIT(_semaphore) == KERN_ABORTED) {
+ // Semaphore was interrupted. Retry.
+ }
}
-jlong Semaphore::currenttime() const {
+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);
-}
+ }
-#ifdef __APPLE__
-bool Semaphore::trywait() {
+ public:
+ BsdSemaphore(uint value = 0) : Semaphore(value) {}
+
+ bool trywait() {
return timedwait(0, 0);
-}
+ }
-bool Semaphore::timedwait(unsigned int sec, int 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;
@@ -2022,40 +2021,31 @@
kr = semaphore_timedwait(_semaphore, waitspec);
}
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) {
+ 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
::memset((void*)pending_signals, 0, sizeof(pending_signals));
< prev index next >