Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/os/bsd/vm/semaphore_bsd.cpp
32284 views
/*1* Copyright (c) 2018, 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/precompiled.hpp"25#include "semaphore_bsd.hpp"26#include "utilities/debug.hpp"2728#include <semaphore.h>2930#ifdef __APPLE__31// OS X doesn't support unamed POSIX semaphores, so the implementation in os_posix.cpp can't be used.3233static const char* sem_init_strerror(kern_return_t value) {34switch (value) {35case KERN_INVALID_ARGUMENT: return "Invalid argument";36case KERN_RESOURCE_SHORTAGE: return "Resource shortage";37default: return "Unknown";38}39}4041OSXSemaphore::OSXSemaphore(uint value) {42kern_return_t ret = semaphore_create(mach_task_self(), &_semaphore, SYNC_POLICY_FIFO, value);4344guarantee(ret == KERN_SUCCESS, err_msg("Failed to create semaphore: %s", sem_init_strerror(ret)));45}4647OSXSemaphore::~OSXSemaphore() {48semaphore_destroy(mach_task_self(), _semaphore);49}5051void OSXSemaphore::signal(uint count) {52for (uint i = 0; i < count; i++) {53kern_return_t ret = semaphore_signal(_semaphore);5455assert(ret == KERN_SUCCESS, "Failed to signal semaphore");56}57}5859void OSXSemaphore::wait() {60kern_return_t ret;61while ((ret = semaphore_wait(_semaphore)) == KERN_ABORTED) {62// Semaphore was interrupted. Retry.63}64assert(ret == KERN_SUCCESS, "Failed to wait on semaphore");65}6667int64_t OSXSemaphore::currenttime() {68struct timeval tv;69gettimeofday(&tv, NULL);70return (tv.tv_sec * NANOSECS_PER_SEC) + (tv.tv_usec * 1000);71}7273bool OSXSemaphore::trywait() {74return timedwait(0, 0);75}7677bool OSXSemaphore::timedwait(unsigned int sec, int nsec) {78kern_return_t kr = KERN_ABORTED;79mach_timespec_t waitspec;80waitspec.tv_sec = sec;81waitspec.tv_nsec = nsec;8283int64_t starttime = currenttime();8485kr = semaphore_timedwait(_semaphore, waitspec);86while (kr == KERN_ABORTED) {87int64_t totalwait = (sec * NANOSECS_PER_SEC) + nsec;8889int64_t current = currenttime();90int64_t passedtime = current - starttime;9192if (passedtime >= totalwait) {93waitspec.tv_sec = 0;94waitspec.tv_nsec = 0;95} else {96int64_t waittime = totalwait - (current - starttime);97waitspec.tv_sec = waittime / NANOSECS_PER_SEC;98waitspec.tv_nsec = waittime % NANOSECS_PER_SEC;99}100101kr = semaphore_timedwait(_semaphore, waitspec);102}103104return kr == KERN_SUCCESS;105}106#endif // __APPLE__107108109