src/hotspot/share/utilities/globalCounter.cpp
author stefank
Tue, 26 Nov 2019 10:47:46 +0100
changeset 59290 97d13893ec3c
parent 59249 29b0d0b61615
permissions -rw-r--r--
8234748: Clean up atomic and orderAccess includes Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49800
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
     1
/*
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
     4
 *
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
     8
 *
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    13
 * accompanied this code).
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    14
 *
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    18
 *
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    21
 * questions.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    22
 *
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    23
 */
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    24
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    25
#include "precompiled.hpp"
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    26
#include "utilities/globalCounter.hpp"
59290
97d13893ec3c 8234748: Clean up atomic and orderAccess includes
stefank
parents: 59249
diff changeset
    27
#include "runtime/atomic.hpp"
49800
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    28
#include "runtime/thread.hpp"
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    29
#include "runtime/threadSMR.inline.hpp"
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    30
#include "runtime/vmThread.hpp"
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    31
#include "utilities/spinYield.hpp"
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    32
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    33
GlobalCounter::PaddedCounter GlobalCounter::_global_counter;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    34
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    35
class GlobalCounter::CounterThreadCheck : public ThreadClosure {
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    36
 private:
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    37
  uintx _gbl_cnt;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    38
 public:
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    39
  CounterThreadCheck(uintx gbl_cnt) : _gbl_cnt(gbl_cnt) {}
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    40
  void do_thread(Thread* thread) {
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    41
    SpinYield yield;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    42
    // Loops on this thread until it has exited the critical read section.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    43
    while(true) {
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 52452
diff changeset
    44
      uintx cnt = Atomic::load_acquire(thread->get_rcu_counter());
49800
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    45
      // This checks if the thread's counter is active. And if so is the counter
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    46
      // for a pre-existing reader (belongs to this grace period). A pre-existing
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    47
      // reader will have a lower counter than the global counter version for this
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    48
      // generation. If the counter is larger than the global counter version this
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    49
      //  is a new reader and we can continue.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    50
      if (((cnt & COUNTER_ACTIVE) != 0) && (cnt - _gbl_cnt) > (max_uintx / 2)) {
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    51
        yield.wait();
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    52
      } else {
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    53
        break;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    54
      }
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    55
    }
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    56
  }
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    57
};
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    58
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    59
void GlobalCounter::write_synchronize() {
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    60
  assert((*Thread::current()->get_rcu_counter() & COUNTER_ACTIVE) == 0x0, "must be outside a critcal section");
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    61
  // Atomic::add must provide fence since we have storeload dependency.
59249
29b0d0b61615 8234737: Harmonize parameter order in Atomic - add
stefank
parents: 59247
diff changeset
    62
  uintx gbl_cnt = Atomic::add(&_global_counter._counter, COUNTER_INCREMENT);
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 51548
diff changeset
    63
49800
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    64
  // Do all RCU threads.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    65
  CounterThreadCheck ctc(gbl_cnt);
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    66
  for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    67
    ctc.do_thread(thread);
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    68
  }
51548
35a6956f4243 8209976: Improve iteration over non-JavaThreads
kbarrett
parents: 51511
diff changeset
    69
  for (NonJavaThread::Iterator njti; !njti.end(); njti.step()) {
35a6956f4243 8209976: Improve iteration over non-JavaThreads
kbarrett
parents: 51511
diff changeset
    70
    ctc.do_thread(njti.current());
51511
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents: 51405
diff changeset
    71
  }
49800
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    72
}