| 1 | /* |
| 2 | * Copyright (c) 2018, 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, "%s; error='%s' (errno=%s)", msg, os::strerror(err), \ |
| 36 | os::errno_name(err)); \ |
| 37 | } while (false) |
| 38 | |
| 39 | #define assert_with_errno(cond, msg) check_with_errno(assert, cond, msg) |
| 40 | #define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg) |
| 41 | |
| 42 | PosixSemaphore::PosixSemaphore(uint value) { |
| 43 | int ret = sem_init(&_semaphore, 0, value); |
| 44 | |
| 45 | guarantee_with_errno(ret == 0, "Failed to initialize semaphore" ); |
| 46 | } |
| 47 | |
| 48 | PosixSemaphore::~PosixSemaphore() { |
| 49 | sem_destroy(&_semaphore); |
| 50 | } |
| 51 | |
| 52 | void PosixSemaphore::signal(uint count) { |
| 53 | for (uint i = 0; i < count; i++) { |
| 54 | int ret = sem_post(&_semaphore); |
| 55 | |
| 56 | assert_with_errno(ret == 0, "sem_post failed" ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void PosixSemaphore::wait() { |
| 61 | int ret; |
| 62 | |
| 63 | do { |
| 64 | ret = sem_wait(&_semaphore); |
| 65 | } while (ret != 0 && errno == EINTR); |
| 66 | |
| 67 | assert_with_errno(ret == 0, "sem_wait failed" ); |
| 68 | } |
| 69 | |
| 70 | bool PosixSemaphore::trywait() { |
| 71 | int ret; |
| 72 | |
| 73 | do { |
| 74 | ret = sem_trywait(&_semaphore); |
| 75 | } while (ret != 0 && errno == EINTR); |
| 76 | |
| 77 | assert_with_errno(ret == 0 || errno == EAGAIN, "trywait failed" ); |
| 78 | |
| 79 | return ret == 0; |
| 80 | } |
| 81 | |
| 82 | bool PosixSemaphore::timedwait(int64_t millis) { |
| 83 | struct timespec ts; |
| 84 | os::Posix::to_RTC_abstime(&ts, millis); |
| 85 | return timedwait(ts); |
| 86 | } |
| 87 | |
| 88 | bool PosixSemaphore::timedwait(struct timespec ts) { |
| 89 | while (true) { |
| 90 | int result = sem_timedwait(&_semaphore, &ts); |
| 91 | if (result == 0) { |
| 92 | return true; |
| 93 | } else if (errno == EINTR) { |
| 94 | continue; |
| 95 | } else if (errno == ETIMEDOUT) { |
| 96 | return false; |
| 97 | } else { |
| 98 | assert_with_errno(false, "timedwait failed" ); |
| 99 | return false; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | #endif // __APPLE__ |
| 104 | |
| 105 | |