Path: blob/master/src/hotspot/os/bsd/semaphore_bsd.cpp
40949 views
/*1* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "semaphore_bsd.hpp"26#include "runtime/os.hpp"27#include "utilities/debug.hpp"2829#include <semaphore.h>3031#ifdef __APPLE__32// OS X doesn't support unamed POSIX semaphores, so the implementation in os_posix.cpp can't be used.3334static const char* sem_init_strerror(kern_return_t value) {35switch (value) {36case KERN_INVALID_ARGUMENT: return "Invalid argument";37case KERN_RESOURCE_SHORTAGE: return "Resource shortage";38default: return "Unknown";39}40}4142OSXSemaphore::OSXSemaphore(uint value) {43kern_return_t ret = semaphore_create(mach_task_self(), &_semaphore, SYNC_POLICY_FIFO, value);4445guarantee(ret == KERN_SUCCESS, "Failed to create semaphore: %s", sem_init_strerror(ret));46}4748OSXSemaphore::~OSXSemaphore() {49semaphore_destroy(mach_task_self(), _semaphore);50}5152void OSXSemaphore::signal(uint count) {53for (uint i = 0; i < count; i++) {54kern_return_t ret = semaphore_signal(_semaphore);5556assert(ret == KERN_SUCCESS, "Failed to signal semaphore");57}58}5960void OSXSemaphore::wait() {61kern_return_t ret;62while ((ret = semaphore_wait(_semaphore)) == KERN_ABORTED) {63// Semaphore was interrupted. Retry.64}65assert(ret == KERN_SUCCESS, "Failed to wait on semaphore");66}6768bool OSXSemaphore::trywait() {69return timedwait(0);70}7172bool OSXSemaphore::timedwait(int64_t millis) {73kern_return_t kr = KERN_ABORTED;7475// kernel semaphores take a relative timeout76mach_timespec_t waitspec;77int secs = millis / MILLIUNITS;78int nsecs = millis_to_nanos(millis % MILLIUNITS);79waitspec.tv_sec = secs;80waitspec.tv_nsec = nsecs;8182int64_t starttime = os::javaTimeNanos();8384kr = semaphore_timedwait(_semaphore, waitspec);85while (kr == KERN_ABORTED) {86// reduce the timout and try again87int64_t totalwait = millis_to_nanos(millis);88int64_t current = os::javaTimeNanos();89int64_t passedtime = current - starttime;9091if (passedtime >= totalwait) {92waitspec.tv_sec = 0;93waitspec.tv_nsec = 0;94} else {95int64_t waittime = totalwait - (current - starttime);96waitspec.tv_sec = waittime / NANOSECS_PER_SEC;97waitspec.tv_nsec = waittime % NANOSECS_PER_SEC;98}99100kr = semaphore_timedwait(_semaphore, waitspec);101}102103return kr == KERN_SUCCESS;104}105#endif // __APPLE__106107108