src/hotspot/share/utilities/globalCounter.hpp
author redestad
Thu, 20 Dec 2018 19:44:08 +0100
changeset 53091 5bf5b773fc41
parent 52332 d2a3503c72f7
child 53244 9807daeb47c4
permissions -rw-r--r--
8215555: TieredCompilation C2 threads can excessively block handshakes Reviewed-by: kvn, neliasso, rehn
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
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    36
// critical_section_end afterwards. Such read-side critical sections may
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    37
// be properly nested. The write side must call write_synchronize
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    38
// before reclaming the memory. The read-path only does an uncontended store
49800
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    39
// 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
    40
// 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
    41
// all readers and wait until they have left the generation. (a system memory
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    42
// barrier can be used on write-side to remove fence in read-side,
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    43
// not implemented).
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    44
class GlobalCounter : public AllStatic {
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    45
 private:
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    46
  // 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
    47
  // counter is on a seperate cacheline.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    48
  struct PaddedCounter {
52227
eadd0abbfdf4 8212707: GlobalCounter padding is too optimistic
rehn
parents: 49800
diff changeset
    49
    DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, 0);
49800
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    50
    volatile uintx _counter;
52227
eadd0abbfdf4 8212707: GlobalCounter padding is too optimistic
rehn
parents: 49800
diff changeset
    51
    DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile uintx));
49800
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    52
  };
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    53
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    54
  // The global counter
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    55
  static PaddedCounter _global_counter;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    56
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    57
  // Bit 0 is active bit.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    58
  static const uintx COUNTER_ACTIVE = 1;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    59
  // Thus we increase counter by 2.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    60
  static const uintx COUNTER_INCREMENT = 2;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    61
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    62
  // The per thread scanning closure.
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    63
  class CounterThreadCheck;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    64
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    65
 public:
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    66
  // The type of the critical section context passed from
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    67
  // critical_section_begin() to critical_section_end().
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    68
  typedef uintx CSContext;
49800
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    69
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    70
  // Must be called before accessing the data.  The result must be passed
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    71
  // to the associated call to critical_section_end().  Acts as a full
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    72
  // memory barrier before the code within the critical section.
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    73
  static CSContext critical_section_begin(Thread *thread);
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    74
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    75
  // Must be called after finished accessing the data.  The context
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    76
  // must be the result of the associated initiating critical_section_begin().
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    77
  // Acts as a release memory barrier after the code within the critical
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    78
  // section.
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    79
  static void critical_section_end(Thread *thread, CSContext context);
49800
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    80
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    81
  // Make the data inaccessible to readers before calling. When this call
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    82
  // returns it's safe to reclaim the data.  Acts as a full memory barrier.
49800
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    83
  static void write_synchronize();
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    84
52332
d2a3503c72f7 8212827: GlobalCounter should support nested critical sections
kbarrett
parents: 52227
diff changeset
    85
  // A scoped object for a read-side critical-section.
49800
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    86
  class CriticalSection;
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    87
};
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    88
69d7398038c5 8195099: Concurrent safe-memory-reclamation mechanism
rehn
parents:
diff changeset
    89
#endif // include guard