1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled/precompiled.hpp"
  26 #include "runtime/orderAccess.hpp"
  27 #include "runtime/os.hpp"
  28 #include "waitBarrier_linux.hpp"
  29 #include <sys/syscall.h>
  30 #include <linux/futex.h>
  31 
  32 #define check_with_errno(check_type, cond, msg)                             \
  33   do {                                                                      \
  34     int err = errno;                                                        \
  35     check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err),   \
  36                os::errno_name(err));                                        \
  37 } while (false)
  38 
  39 #define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg)
  40 
  41 static int futex(volatile int *uaddr, int futex_op, int val,
  42                  const struct timespec *timeout, int *uaddr2, int val3)
  43 {
  44   return syscall(SYS_futex, uaddr, futex_op, val, timeout, uaddr2, val3);
  45 }
  46 
  47 void LinuxWaitBarrier::arm(int barrier_tag) {
  48   assert(_futex_barrier == 0, "Already armed");
  49   _futex_barrier = barrier_tag;
  50   OrderAccess::fence();
  51 }
  52 
  53 void LinuxWaitBarrier::disarm() {
  54   assert(_futex_barrier != 0, "Not armed");
  55   _futex_barrier = 0;
  56   OrderAccess::fence();
  57 }
  58 
  59 void LinuxWaitBarrier::wake() {
  60   assert(_futex_barrier == 0, "Not disarmed");
  61   int s = futex(&_futex_barrier,
  62                 FUTEX_WAKE,
  63                 INT_MAX, /* wake a max of this many threads */
  64                 NULL /* ignored */,
  65                 NULL /* ignored */,
  66                 0 /* ignored */);
  67   guarantee_with_errno(s > -1, "futex FUTEX_WAKE");
  68 }
  69 
  70 void LinuxWaitBarrier::wait(int barrier_tag) {
  71   assert(barrier_tag != 0, "Trying to wait on disarmed value");
  72   if (barrier_tag == 0 ||
  73       barrier_tag != _futex_barrier) {
  74     OrderAccess::fence();
  75     return;
  76   }
  77   do {
  78     int s = futex(&_futex_barrier,
  79                   FUTEX_WAIT,
  80                   barrier_tag, /* should be this tag */
  81                   NULL, /* no timeout */
  82                   NULL, /* ignored */
  83                   0 /* ignored */);
  84     guarantee_with_errno((s == 0) ||
  85                          (s == -1 && errno == EAGAIN) ||
  86                          (s == -1 && errno == EINTR),
  87                          "futex FUTEX_WAIT");
  88     // Return value 0, re-check in case of spurious wake-up.
  89     // EINTR and re-check and go back to waiting.
  90     // EAGAIN we already are disarmed, we should pass the check,
  91     // if not re-armed with same tag.
  92   } while (barrier_tag == _futex_barrier);
  93 }