/
githubmirror
/
panama-vector
Обзор
Документация
Войти
/
githubmirror
/
panama-vector
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/hotspot/os/bsd/semaphore_bsd.cpp
126 строк
4 KB
Axel Boldt-Christmas
8372247: OSX: Semaphore.trywait requires os::Bsd::clock_init
04 мар 2026, 10:07
04 мар 2026, 10:07
39b1e9d
Код
Авторство
О чём код?
/* * Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #include "semaphore_bsd.hpp" #include "runtime/os.hpp" #include "utilities/debug.hpp" #include <semaphore.h> #ifdef __APPLE__ // OS X doesn't support unnamed POSIX semaphores, so the implementation in os_posix.cpp can't be used. static const char* sem_strerror(kern_return_t value) { switch (value) { case KERN_INVALID_ARGUMENT: return "Invalid argument"; case KERN_RESOURCE_SHORTAGE: return "Resource shortage"; case KERN_TERMINATED: return "Resource has been destroyed"; case KERN_ABORTED: return "Operation was aborted"; case KERN_INVALID_VALUE: return "Invalid time specified"; case KERN_OPERATION_TIMED_OUT: return "Operation timed-out"; default: return "Unknown"; } } OSXSemaphore::OSXSemaphore(uint value) { kern_return_t ret = semaphore_create(mach_task_self(), &_semaphore, SYNC_POLICY_FIFO, value); guarantee(ret == KERN_SUCCESS, "Failed to create semaphore: %s (0x%x)", sem_strerror(ret), (uint)ret); } OSXSemaphore::~OSXSemaphore() { semaphore_destroy(mach_task_self(), _semaphore); } void OSXSemaphore::signal(uint count) { for (uint i = 0; i < count; i++) { kern_return_t ret = semaphore_signal(_semaphore); assert(ret == KERN_SUCCESS, "Failed to signal semaphore: %s (0x%x)", sem_strerror(ret), (uint)ret); } } void OSXSemaphore::wait() { kern_return_t ret; while ((ret = semaphore_wait(_semaphore)) == KERN_ABORTED) { // Semaphore was interrupted. Retry. } assert(ret == KERN_SUCCESS, "Failed to wait on semaphore: %s (0x%x)", sem_strerror(ret), (uint)ret); } bool OSXSemaphore::trywait() { return timedwait(0); } bool OSXSemaphore::timedwait(int64_t millis) { kern_return_t kr = KERN_ABORTED; // kernel semaphores take a relative timeout mach_timespec_t waitspec; int64_t starttime; const bool is_trywait = millis == 0; if (!is_trywait) { int secs = millis / MILLIUNITS; int nsecs = millis_to_nanos(millis % MILLIUNITS); waitspec.tv_sec = secs; waitspec.tv_nsec = nsecs; starttime = os::javaTimeNanos(); } else { waitspec.tv_sec = 0; waitspec.tv_nsec = 0; } kr = semaphore_timedwait(_semaphore, waitspec); while (kr == KERN_ABORTED) { if (!is_trywait) { // reduce the timeout and try again int64_t totalwait = millis_to_nanos(millis); int64_t current = os::javaTimeNanos(); int64_t passedtime = current - starttime; if (passedtime >= totalwait) { waitspec.tv_sec = 0; waitspec.tv_nsec = 0; } else { int64_t waittime = totalwait - (current - starttime); waitspec.tv_sec = waittime / NANOSECS_PER_SEC; waitspec.tv_nsec = waittime % NANOSECS_PER_SEC; } } kr = semaphore_timedwait(_semaphore, waitspec); } assert(kr == KERN_SUCCESS || kr == KERN_OPERATION_TIMED_OUT, "Failed to timed-wait on semaphore: %s (0x%x)", sem_strerror(kr), (uint)kr); return kr == KERN_SUCCESS; } #endif // __APPLE__