src/hotspot/share/utilities/globalCounter.cpp
author rehn
Wed, 18 Apr 2018 09:25:51 +0200
changeset 49800 69d7398038c5
child 50029 ea0a16ba6ac0
permissions -rw-r--r--
8195099: Concurrent safe-memory-reclamation mechanism Summary: This implement a globalcounter with RCU semantics. Reviewed-by: acorn, coleenp, dcubed, eosterlund, gziemski, mlarsson, kbarrett, 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"
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    27
#include "runtime/orderAccess.inline.hpp"
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) {
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    44
      uintx cnt = OrderAccess::load_acquire(thread->get_rcu_counter());
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.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    62
  volatile uintx gbl_cnt = Atomic::add((uintx)COUNTER_INCREMENT, &_global_counter._counter);
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    63
  // Do all RCU threads.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    64
  CounterThreadCheck ctc(gbl_cnt);
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    65
  for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    66
    ctc.do_thread(thread);
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    67
  }
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    68
  ctc.do_thread(VMThread::vm_thread());
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    69
}