src/hotspot/share/utilities/globalCounter.hpp
author rehn
Wed, 18 Apr 2018 09:25:51 +0200
changeset 49800 69d7398038c5
child 52227 eadd0abbfdf4
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
#ifndef SHARE_UTILITIES_GLOBAL_COUNTER_HPP
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    26
#define SHARE_UTILITIES_GLOBAL_COUNTER_HPP
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    27
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    28
#include "memory/allocation.hpp"
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    29
#include "memory/padded.hpp"
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    30
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    31
class Thread;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    32
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    33
// The GlobalCounter provides a synchronization mechanism between threads for
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    34
// safe memory reclamation and other ABA problems. All readers must call
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    35
// critical_section_begin before reading the volatile data and
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    36
// critical_section_end afterwards. The write side must call write_synchronize
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    37
// before reclaming the memory. The read-path only does an uncontented store
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    38
// to a thread-local-storage and fence to stop any loads from floating up, thus
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    39
// light weight and wait-free. The write-side is more heavy since it must check
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    40
// all readers and wait until they have left the generation. (a system memory
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    41
// barrier can be used on write-side to remove fence in read-side,
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    42
// not implemented).
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    43
class GlobalCounter : public AllStatic {
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    44
 private:
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    45
  // Since do not know what we will end up next to in BSS, we make sure the
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    46
  // counter is on a seperate cacheline.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    47
  struct PaddedCounter {
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    48
    DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE/2, 0);
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    49
    volatile uintx _counter;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    50
    DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE/2, sizeof(volatile uintx));
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    51
  };
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    52
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    53
  // The global counter
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    54
  static PaddedCounter _global_counter;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    55
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    56
  // Bit 0 is active bit.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    57
  static const uintx COUNTER_ACTIVE = 1;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    58
  // Thus we increase counter by 2.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    59
  static const uintx COUNTER_INCREMENT = 2;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    60
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    61
  // The per thread scanning closure.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    62
  class CounterThreadCheck;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    63
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    64
 public:
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    65
  // Must be called before accessing the data. Only threads accessible lock-free
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    66
  // can used this. Those included now are all Threads on SMR ThreadsList and
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    67
  // the VMThread. Nesting is not yet supported.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    68
  static void critical_section_begin(Thread *thread);
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    69
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    70
  // Must be called after finished accessing the data.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    71
  // Do not provide fence, allows load/stores moving into the critical section.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    72
  static void critical_section_end(Thread *thread);
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    73
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    74
  // Make the data inaccessible to readers before calling. When this call
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    75
  // returns it's safe to reclaim the data.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    76
  static void write_synchronize();
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    77
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    78
  // A scoped object for a reads-side critical-section.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    79
  class CriticalSection;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    80
};
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    81
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    82
#endif // include guard