Path: blob/master/src/hotspot/os/linux/waitBarrier_linux.cpp
40951 views
/*1* Copyright (c) 2019, 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 "runtime/orderAccess.hpp"26#include "runtime/os.hpp"27#include "waitBarrier_linux.hpp"28#include <sys/syscall.h>29#include <linux/futex.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 guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg)3940static int futex(volatile int *addr, int futex_op, int op_arg) {41return syscall(SYS_futex, addr, futex_op, op_arg, NULL, NULL, 0);42}4344void LinuxWaitBarrier::arm(int barrier_tag) {45assert(_futex_barrier == 0, "Should not be already armed: "46"_futex_barrier=%d", _futex_barrier);47_futex_barrier = barrier_tag;48OrderAccess::fence();49}5051void LinuxWaitBarrier::disarm() {52assert(_futex_barrier != 0, "Should be armed/non-zero.");53_futex_barrier = 0;54int s = futex(&_futex_barrier,55FUTEX_WAKE_PRIVATE,56INT_MAX /* wake a max of this many threads */);57guarantee_with_errno(s > -1, "futex FUTEX_WAKE failed");58}5960void LinuxWaitBarrier::wait(int barrier_tag) {61assert(barrier_tag != 0, "Trying to wait on disarmed value");62if (barrier_tag == 0 ||63barrier_tag != _futex_barrier) {64OrderAccess::fence();65return;66}67do {68int s = futex(&_futex_barrier,69FUTEX_WAIT_PRIVATE,70barrier_tag /* should be this tag */);71guarantee_with_errno((s == 0) ||72(s == -1 && errno == EAGAIN) ||73(s == -1 && errno == EINTR),74"futex FUTEX_WAIT failed");75// Return value 0: woken up, but re-check in case of spurious wakeup.76// Error EINTR: woken by signal, so re-check and re-wait if necessary.77// Error EAGAIN: we are already disarmed and so will pass the check.78} while (barrier_tag == _futex_barrier);79}808182