Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/os/bsd/semaphore_bsd.cpp
40949 views
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
#include "semaphore_bsd.hpp"
27
#include "runtime/os.hpp"
28
#include "utilities/debug.hpp"
29
30
#include <semaphore.h>
31
32
#ifdef __APPLE__
33
// OS X doesn't support unamed POSIX semaphores, so the implementation in os_posix.cpp can't be used.
34
35
static const char* sem_init_strerror(kern_return_t value) {
36
switch (value) {
37
case KERN_INVALID_ARGUMENT: return "Invalid argument";
38
case KERN_RESOURCE_SHORTAGE: return "Resource shortage";
39
default: return "Unknown";
40
}
41
}
42
43
OSXSemaphore::OSXSemaphore(uint value) {
44
kern_return_t ret = semaphore_create(mach_task_self(), &_semaphore, SYNC_POLICY_FIFO, value);
45
46
guarantee(ret == KERN_SUCCESS, "Failed to create semaphore: %s", sem_init_strerror(ret));
47
}
48
49
OSXSemaphore::~OSXSemaphore() {
50
semaphore_destroy(mach_task_self(), _semaphore);
51
}
52
53
void OSXSemaphore::signal(uint count) {
54
for (uint i = 0; i < count; i++) {
55
kern_return_t ret = semaphore_signal(_semaphore);
56
57
assert(ret == KERN_SUCCESS, "Failed to signal semaphore");
58
}
59
}
60
61
void OSXSemaphore::wait() {
62
kern_return_t ret;
63
while ((ret = semaphore_wait(_semaphore)) == KERN_ABORTED) {
64
// Semaphore was interrupted. Retry.
65
}
66
assert(ret == KERN_SUCCESS, "Failed to wait on semaphore");
67
}
68
69
bool OSXSemaphore::trywait() {
70
return timedwait(0);
71
}
72
73
bool OSXSemaphore::timedwait(int64_t millis) {
74
kern_return_t kr = KERN_ABORTED;
75
76
// kernel semaphores take a relative timeout
77
mach_timespec_t waitspec;
78
int secs = millis / MILLIUNITS;
79
int nsecs = millis_to_nanos(millis % MILLIUNITS);
80
waitspec.tv_sec = secs;
81
waitspec.tv_nsec = nsecs;
82
83
int64_t starttime = os::javaTimeNanos();
84
85
kr = semaphore_timedwait(_semaphore, waitspec);
86
while (kr == KERN_ABORTED) {
87
// reduce the timout and try again
88
int64_t totalwait = millis_to_nanos(millis);
89
int64_t current = os::javaTimeNanos();
90
int64_t passedtime = current - starttime;
91
92
if (passedtime >= totalwait) {
93
waitspec.tv_sec = 0;
94
waitspec.tv_nsec = 0;
95
} else {
96
int64_t waittime = totalwait - (current - starttime);
97
waitspec.tv_sec = waittime / NANOSECS_PER_SEC;
98
waitspec.tv_nsec = waittime % NANOSECS_PER_SEC;
99
}
100
101
kr = semaphore_timedwait(_semaphore, waitspec);
102
}
103
104
return kr == KERN_SUCCESS;
105
}
106
#endif // __APPLE__
107
108