1 /*
2 * Copyright (c) 2018, 2020, 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.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
PosixSemaphore(uint value)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
~PosixSemaphore()48 PosixSemaphore::~PosixSemaphore() {
49 int ret = sem_destroy(&_semaphore);
50 assert_with_errno(ret == 0, "sem_destroy failed");
51 }
52
signal(uint count)53 void PosixSemaphore::signal(uint count) {
54 for (uint i = 0; i < count; i++) {
55 int ret = sem_post(&_semaphore);
56
57 assert_with_errno(ret == 0, "sem_post failed");
58 }
59 }
60
wait()61 void PosixSemaphore::wait() {
62 int ret;
63
64 do {
65 ret = sem_wait(&_semaphore);
66 } while (ret != 0 && errno == EINTR);
67
68 assert_with_errno(ret == 0, "sem_wait failed");
69 }
70
trywait()71 bool PosixSemaphore::trywait() {
72 int ret;
73
74 do {
75 ret = sem_trywait(&_semaphore);
76 } while (ret != 0 && errno == EINTR);
77
78 assert_with_errno(ret == 0 || errno == EAGAIN, "trywait failed");
79
80 return ret == 0;
81 }
82
timedwait(int64_t millis)83 bool PosixSemaphore::timedwait(int64_t millis) {
84 struct timespec ts;
85 os::Posix::to_RTC_abstime(&ts, millis);
86 return timedwait(ts);
87 }
88
timedwait(struct timespec ts)89 bool PosixSemaphore::timedwait(struct timespec ts) {
90 while (true) {
91 int result = sem_timedwait(&_semaphore, &ts);
92 if (result == 0) {
93 return true;
94 } else if (errno == EINTR) {
95 continue;
96 } else if (errno == ETIMEDOUT) {
97 return false;
98 } else {
99 assert_with_errno(false, "timedwait failed");
100 return false;
101 }
102 }
103 }
104 #endif // __APPLE__
105
106