< 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,55 +1940,50 @@
 // 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;
-};
-
-Semaphore::Semaphore() : _semaphore(0) {
-  SEM_INIT(_semaphore, 0);
+Semaphore::Semaphore(uint value, uint max) : _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);
+#ifdef __APPLE__
+  while (SEM_WAIT(_semaphore) == KERN_ABORTED) {
+#else
+  while (SEM_WAIT(_semaphore) == -1 && errno == EINTR) {
+#endif
+    // Semaphore was interrupted. Retry.
+  }
 }
 
-jlong Semaphore::currenttime() const {
+static jlong semaphore_currenttime() {
   struct timeval tv;
   gettimeofday(&tv, NULL);
   return (tv.tv_sec * NANOSECS_PER_SEC) + (tv.tv_usec * 1000);
 }
 

@@ -2000,17 +1996,17 @@
   kern_return_t kr = KERN_ABORTED;
   mach_timespec_t waitspec;
   waitspec.tv_sec = sec;
   waitspec.tv_nsec = nsec;
 
-  jlong starttime = currenttime();
+  jlong starttime = semaphore_currenttime();
 
   kr = semaphore_timedwait(_semaphore, waitspec);
   while (kr == KERN_ABORTED) {
     jlong totalwait = (sec * NANOSECS_PER_SEC) + nsec;
 
-    jlong current = currenttime();
+    jlong current = semaphore_currenttime();
     jlong passedtime = current - starttime;
 
     if (passedtime >= totalwait) {
       waitspec.tv_sec = 0;
       waitspec.tv_nsec = 0;
< prev index next >