Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/os/posix/vm/semaphore_posix.cpp
32285 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#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),*/ \35/*os::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() {48sem_destroy(&_semaphore);49}5051void PosixSemaphore::signal(uint count) {52for (uint i = 0; i < count; i++) {53int ret = sem_post(&_semaphore);5455assert_with_errno(ret == 0, "sem_post failed");56}57}5859void PosixSemaphore::wait() {60int ret;6162do {63ret = sem_wait(&_semaphore);64} while (ret != 0 && errno == EINTR);6566assert_with_errno(ret == 0, "sem_wait failed");67}6869bool PosixSemaphore::trywait() {70int ret;7172do {73ret = sem_trywait(&_semaphore);74} while (ret != 0 && errno == EINTR);7576assert_with_errno(ret == 0 || errno == EAGAIN, "trywait failed");7778return ret == 0;79}8081bool PosixSemaphore::timedwait(struct timespec ts) {82while (true) {83int result = sem_timedwait(&_semaphore, &ts);84if (result == 0) {85return true;86} else if (errno == EINTR) {87continue;88} else if (errno == ETIMEDOUT) {89return false;90} else {91assert_with_errno(false, "timedwait failed");92return false;93}94}95}96#endif // __APPLE__979899100