1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled/precompiled.hpp"
  26 #ifndef __APPLE__
  27 #include "runtime/os.hpp"
  28 // POSIX unamed semaphores are not supported on OS X.
  29 #include "semaphore_posix.hpp"
  30 #include <semaphore.h>
  31 
  32 #define check_with_errno(check_type, cond, msg)                             \
  33   do {                                                                      \
  34     int err = errno;                                                        \
  35     check_type(cond, msg);                                                  \
  36 } while (false)
  37 
  38 #define assert_with_errno(cond, msg)    check_with_errno(assert, cond, msg)
  39 #define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg)
  40 
  41 PosixSemaphore::PosixSemaphore(uint value) {
  42   int ret = sem_init(&_semaphore, 0, value);
  43 
  44   guarantee_with_errno(ret == 0, "Failed to initialize semaphore");
  45 }
  46 
  47 PosixSemaphore::~PosixSemaphore() {
  48   sem_destroy(&_semaphore);
  49 }
  50 
  51 void PosixSemaphore::signal(uint count) {
  52   for (uint i = 0; i < count; i++) {
  53     int ret = sem_post(&_semaphore);
  54 
  55     assert_with_errno(ret == 0, "sem_post failed");
  56   }
  57 }
  58 
  59 void PosixSemaphore::wait() {
  60   int ret;
  61 
  62   do {
  63     ret = sem_wait(&_semaphore);
  64   } while (ret != 0 && errno == EINTR);
  65 
  66   assert_with_errno(ret == 0, "sem_wait failed");
  67 }
  68 
  69 bool PosixSemaphore::trywait() {
  70   int ret;
  71 
  72   do {
  73     ret = sem_trywait(&_semaphore);
  74   } while (ret != 0 && errno == EINTR);
  75 
  76   assert_with_errno(ret == 0 || errno == EAGAIN, "trywait failed");
  77 
  78   return ret == 0;
  79 }
  80 
  81 bool PosixSemaphore::timedwait(struct timespec ts) {
  82   while (true) {
  83     int result = sem_timedwait(&_semaphore, &ts);
  84     if (result == 0) {
  85       return true;
  86     } else if (errno == EINTR) {
  87       continue;
  88     } else if (errno == ETIMEDOUT) {
  89       return false;
  90     } else {
  91       assert_with_errno(false, "timedwait failed");
  92       return false;
  93     }
  94   }
  95 }
  96 #endif // __APPLE__
  97