|
1 /* |
|
2 * Copyright (c) 2018, 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(struct timespec ts) { |
|
83 while (true) { |
|
84 int result = sem_timedwait(&_semaphore, &ts); |
|
85 if (result == 0) { |
|
86 return true; |
|
87 } else if (errno == EINTR) { |
|
88 continue; |
|
89 } else if (errno == ETIMEDOUT) { |
|
90 return false; |
|
91 } else { |
|
92 assert_with_errno(false, "timedwait failed"); |
|
93 return false; |
|
94 } |
|
95 } |
|
96 } |
|
97 #endif // __APPLE__ |
|
98 |