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.hpp"
  26 #include "runtime/atomic.hpp"
  27 #include "runtime/orderAccess.hpp"
  28 #include "runtime/os.hpp"
  29 #include "utilities/waitBarrier_generic.hpp"
  30 #include "utilities/spinYield.hpp"
  31 
  32 void GenericWaitBarrier::arm(int barrier_tag) {
  33   assert(_barrier_tag == 0, "Already armed");
  34   assert(_waiters == 0, "We left a thread hanging");
  35   _barrier_tag = barrier_tag;
  36   _waiters = 0;
  37   OrderAccess::fence();
  38 }
  39 
  40 void GenericWaitBarrier::disarm() {
  41   assert(_barrier_tag != 0, "Not armed");
  42   _barrier_tag = 0;
  43   OrderAccess::fence();
  44 }
  45 
  46 int GenericWaitBarrier::wake_if_needed() {
  47   assert(_barrier_tag == 0, "Not disarmed");
  48   int w = _waiters;
  49   if (w == 0) {
  50     // Load of _barrier_threads in caller must not pass the load of _waiters.
  51     OrderAccess::loadload();
  52     return 0;
  53   }
  54   assert(w > 0, "Bad counting");
  55   // We need an exact count which never goes below zero,
  56   // otherwise the semaphore may be signalled too many times.
  57   if (Atomic::cmpxchg(w - 1, &_waiters, w) == w) {
  58     _sem_barrier.signal();
  59     return w - 1;
  60   }
  61   return w;
  62 }
  63 
  64 void GenericWaitBarrier::wake() {
  65   assert(_barrier_tag == 0, "Not disarmed");
  66   int left;
  67   SpinYield sp;
  68   do {
  69     left = GenericWaitBarrier::wake_if_needed();
  70     if (left == 0 && _barrier_threads > 0) {
  71       // There is no thread to wake but we still have barrier threads.
  72       sp.wait();
  73     }
  74     // We must loop here until there are no waiters or potential waiters.
  75   } while (left > 0 || _barrier_threads > 0);
  76   OrderAccess::fence();
  77 }
  78 
  79 void GenericWaitBarrier::wait(int barrier_tag) {
  80   assert(barrier_tag != 0, "Trying to wait on disarmed value");
  81   Atomic::add(1, &_barrier_threads);
  82   if (barrier_tag != 0 && barrier_tag == _barrier_tag) {
  83     Atomic::add(1, &_waiters);
  84     _sem_barrier.wait();
  85     // We help out with posting, but we need to do so before we decrement the
  86     // _barrier_threads otherwise we might wake threads up in next wait.
  87     GenericWaitBarrier::wake_if_needed();
  88   }
  89   Atomic::add(-1, &_barrier_threads);
  90 }