author | alanb |
Thu, 21 Mar 2019 08:41:10 +0000 | |
branch | niosocketimpl-branch |
changeset 57275 | 222fa5ed1c91 |
parent 53461 | 08d6edeb3145 |
permissions | -rw-r--r-- |
48635 | 1 |
/* |
53461
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
48635 | 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 |
#include "semaphore_bsd.hpp" |
|
53461
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
27 |
#include "runtime/os.hpp" |
48635 | 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 |
||
53461
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
69 |
bool OSXSemaphore::trywait() { |
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
70 |
return timedwait(0); |
48635 | 71 |
} |
72 |
||
53461
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
73 |
bool OSXSemaphore::timedwait(int64_t millis) { |
48635 | 74 |
kern_return_t kr = KERN_ABORTED; |
53461
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
75 |
|
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
76 |
// kernel semaphores take a relative timeout |
48635 | 77 |
mach_timespec_t waitspec; |
53461
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
78 |
int secs = millis / MILLIUNITS; |
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
79 |
int nsecs = (millis % MILLIUNITS) * NANOSECS_PER_MILLISEC; |
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
80 |
waitspec.tv_sec = secs; |
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
81 |
waitspec.tv_nsec = nsecs; |
48635 | 82 |
|
53461
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
83 |
int64_t starttime = os::javaTimeMillis() * NANOSECS_PER_MILLISEC; |
48635 | 84 |
|
85 |
kr = semaphore_timedwait(_semaphore, waitspec); |
|
86 |
while (kr == KERN_ABORTED) { |
|
53461
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
87 |
// reduce the timout and try again |
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
88 |
int64_t totalwait = millis * NANOSECS_PER_MILLISEC; |
08d6edeb3145
8194860: Cleanup Semaphore timed-wait time calculations
dholmes
parents:
48635
diff
changeset
|
89 |
int64_t current = os::javaTimeMillis() * NANOSECS_PER_MILLISEC; |
48635 | 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__ |