< prev index next >

src/os/linux/vm/os_linux.cpp

Print this page

        

@@ -67,10 +67,11 @@
 #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,36 +2392,37 @@
 
 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(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() {
   sem_destroy(&_semaphore);
 }
 
 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() {
   return sem_trywait(&_semaphore) == 0;
 }
< prev index next >