< prev index next >
src/os/bsd/vm/os_bsd.cpp
Print this page
@@ -1951,10 +1951,13 @@
#define SEM_WAIT(sem) sem_wait(&sem)
#define SEM_POST(sem) sem_post(&sem)
#define SEM_DESTROY(sem) sem_destroy(&sem)
#endif
+#ifdef __APPLE__
+// OS X doesn't support unamed POSIX semaphores, so the implementation in os_posix.cpp can't be used.
+
Semaphore::Semaphore(uint value, uint max) : _semaphore(0) {
SEM_INIT(_semaphore, value);
}
Semaphore::~Semaphore() {
@@ -1970,43 +1973,43 @@
signal();
}
}
void Semaphore::wait() {
-#ifdef __APPLE__
while (SEM_WAIT(_semaphore) == KERN_ABORTED) {
-#else
- while (SEM_WAIT(_semaphore) == -1 && errno == EINTR) {
-#endif
// Semaphore was interrupted. Retry.
}
}
-static jlong semaphore_currenttime() {
+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;
- jlong starttime = semaphore_currenttime();
+ jlong starttime = currenttime();
kr = semaphore_timedwait(_semaphore, waitspec);
while (kr == KERN_ABORTED) {
jlong totalwait = (sec * NANOSECS_PER_SEC) + nsec;
- jlong current = semaphore_currenttime();
+ jlong current = currenttime();
jlong passedtime = current - starttime;
if (passedtime >= totalwait) {
waitspec.tv_sec = 0;
waitspec.tv_nsec = 0;
@@ -2018,40 +2021,35 @@
kr = semaphore_timedwait(_semaphore, waitspec);
}
return kr == KERN_SUCCESS;
-}
+ }
+};
#else
-bool Semaphore::trywait() {
+class BsdSemaphore : public os::PosixSemaphore {
+ public:
+ BsdSemaphore(uint value = 0) : os::PosixSemaphore(value) {}
+
+ bool BsdSemaphore::trywait() {
return sem_trywait(&_semaphore) == 0;
-}
+ }
-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 >