Path: blob/master/src/hotspot/os/posix/semaphore_posix.cpp
40930 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#ifndef __APPLE__26#include "runtime/os.hpp"27// POSIX unamed semaphores are not supported on OS X.28#include "semaphore_posix.hpp"29#include <semaphore.h>3031#define check_with_errno(check_type, cond, msg) \32do { \33int err = errno; \34check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err), \35os::errno_name(err)); \36} while (false)3738#define assert_with_errno(cond, msg) check_with_errno(assert, cond, msg)39#define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg)4041PosixSemaphore::PosixSemaphore(uint value) {42int ret = sem_init(&_semaphore, 0, value);4344guarantee_with_errno(ret == 0, "Failed to initialize semaphore");45}4647PosixSemaphore::~PosixSemaphore() {48int ret = sem_destroy(&_semaphore);49assert_with_errno(ret == 0, "sem_destroy failed");50}5152void PosixSemaphore::signal(uint count) {53for (uint i = 0; i < count; i++) {54int ret = sem_post(&_semaphore);5556assert_with_errno(ret == 0, "sem_post failed");57}58}5960void PosixSemaphore::wait() {61int ret;6263do {64ret = sem_wait(&_semaphore);65} while (ret != 0 && errno == EINTR);6667assert_with_errno(ret == 0, "sem_wait failed");68}6970bool PosixSemaphore::trywait() {71int ret;7273do {74ret = sem_trywait(&_semaphore);75} while (ret != 0 && errno == EINTR);7677assert_with_errno(ret == 0 || errno == EAGAIN, "trywait failed");7879return ret == 0;80}8182bool PosixSemaphore::timedwait(int64_t millis) {83struct timespec ts;84os::Posix::to_RTC_abstime(&ts, millis);85return timedwait(ts);86}8788bool PosixSemaphore::timedwait(struct timespec ts) {89while (true) {90int result = sem_timedwait(&_semaphore, &ts);91if (result == 0) {92return true;93} else if (errno == EINTR) {94continue;95} else if (errno == ETIMEDOUT) {96return false;97} else {98assert_with_errno(false, "timedwait failed");99return false;100}101}102}103#endif // __APPLE__104105106107