src/hotspot/share/gc/shared/gcLocker.hpp
author eosterlund
Tue, 16 Oct 2018 13:18:22 +0200
changeset 52142 ca0c25e01c5b
parent 49594 898ef81cbc0e
child 53244 9807daeb47c4
permissions -rw-r--r--
8210498: nmethod entry barriers Reviewed-by: kvn, pliden
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 25468
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 25468
diff changeset
    25
#ifndef SHARE_VM_GC_SHARED_GCLOCKER_HPP
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 25468
diff changeset
    26
#define SHARE_VM_GC_SHARED_GCLOCKER_HPP
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
49594
898ef81cbc0e 8200106: Move NoSafepointVerifier out from gcLocker.hpp
stefank
parents: 47216
diff changeset
    28
#include "memory/allocation.hpp"
898ef81cbc0e 8200106: Move NoSafepointVerifier out from gcLocker.hpp
stefank
parents: 47216
diff changeset
    29
#include "utilities/globalDefinitions.hpp"
898ef81cbc0e 8200106: Move NoSafepointVerifier out from gcLocker.hpp
stefank
parents: 47216
diff changeset
    30
#include "utilities/macros.hpp"
898ef81cbc0e 8200106: Move NoSafepointVerifier out from gcLocker.hpp
stefank
parents: 47216
diff changeset
    31
898ef81cbc0e 8200106: Move NoSafepointVerifier out from gcLocker.hpp
stefank
parents: 47216
diff changeset
    32
class JavaThread;
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    33
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// The direct lock/unlock calls do not force a collection if an unlock
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// decrements the count to zero. Avoid calling these if at all possible.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
35492
c8c0273e6b91 8146690: Make all classes in GC follow the naming convention.
david
parents: 35061
diff changeset
    37
class GCLocker: public AllStatic {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
 private:
11631
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    39
  // The _jni_lock_count keeps track of the number of threads that are
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    40
  // currently in a critical region.  It's only kept up to date when
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    41
  // _needs_gc is true.  The current value is computed during
35492
c8c0273e6b91 8146690: Make all classes in GC follow the naming convention.
david
parents: 35061
diff changeset
    42
  // safepointing and decremented during the slow path of GCLocker
11631
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    43
  // unlocking.
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    44
  static volatile jint _jni_lock_count;  // number of jni active instances.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  static volatile bool _needs_gc;        // heap is filling, we need a GC
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
                                         // note: bool is typedef'd as jint
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  static volatile bool _doing_gc;        // unlock_critical() is doing a GC
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
11631
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    49
#ifdef ASSERT
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    50
  // This lock count is updated for all operations and is used to
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    51
  // validate the jni_lock_count that is computed during safepoints.
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    52
  static volatile jint _debug_jni_lock_count;
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    53
#endif
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    54
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    55
  // At a safepoint, visit all threads and count the number of active
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    56
  // critical sections.  This is used to ensure that all active
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    57
  // critical sections are exited before a new one is started.
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    58
  static void verify_critical_count() NOT_DEBUG_RETURN;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
11631
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    60
  static void jni_lock(JavaThread* thread);
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    61
  static void jni_unlock(JavaThread* thread);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
11793
49aa0b2124b6 7144318: GCLocker assert failure: assert(_needs_gc || SafepointSynchronize::is_at_safepoint(
never
parents: 11788
diff changeset
    63
  static bool is_active_internal() {
49aa0b2124b6 7144318: GCLocker assert failure: assert(_needs_gc || SafepointSynchronize::is_at_safepoint(
never
parents: 11788
diff changeset
    64
    verify_critical_count();
23540
06f7d6e1f654 8028498: runtime/SharedArchiveFile/CdsDifferentObjectAlignment.java asserts in RT_Baseline
sjohanss
parents: 22234
diff changeset
    65
    return _jni_lock_count > 0;
11793
49aa0b2124b6 7144318: GCLocker assert failure: assert(_needs_gc || SafepointSynchronize::is_at_safepoint(
never
parents: 11788
diff changeset
    66
  }
49aa0b2124b6 7144318: GCLocker assert failure: assert(_needs_gc || SafepointSynchronize::is_at_safepoint(
never
parents: 11788
diff changeset
    67
35061
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents: 30764
diff changeset
    68
  static void log_debug_jni(const char* msg);
49594
898ef81cbc0e 8200106: Move NoSafepointVerifier out from gcLocker.hpp
stefank
parents: 47216
diff changeset
    69
898ef81cbc0e 8200106: Move NoSafepointVerifier out from gcLocker.hpp
stefank
parents: 47216
diff changeset
    70
  static bool is_at_safepoint();
898ef81cbc0e 8200106: Move NoSafepointVerifier out from gcLocker.hpp
stefank
parents: 47216
diff changeset
    71
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // Accessors
11793
49aa0b2124b6 7144318: GCLocker assert failure: assert(_needs_gc || SafepointSynchronize::is_at_safepoint(
never
parents: 11788
diff changeset
    74
  static bool is_active() {
49594
898ef81cbc0e 8200106: Move NoSafepointVerifier out from gcLocker.hpp
stefank
parents: 47216
diff changeset
    75
    assert(GCLocker::is_at_safepoint(), "only read at safepoint");
11793
49aa0b2124b6 7144318: GCLocker assert failure: assert(_needs_gc || SafepointSynchronize::is_at_safepoint(
never
parents: 11788
diff changeset
    76
    return is_active_internal();
49aa0b2124b6 7144318: GCLocker assert failure: assert(_needs_gc || SafepointSynchronize::is_at_safepoint(
never
parents: 11788
diff changeset
    77
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  static bool needs_gc()       { return _needs_gc;                        }
11631
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    79
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  // Shorthand
11793
49aa0b2124b6 7144318: GCLocker assert failure: assert(_needs_gc || SafepointSynchronize::is_at_safepoint(
never
parents: 11788
diff changeset
    81
  static bool is_active_and_needs_gc() {
49aa0b2124b6 7144318: GCLocker assert failure: assert(_needs_gc || SafepointSynchronize::is_at_safepoint(
never
parents: 11788
diff changeset
    82
    // Use is_active_internal since _needs_gc can change from true to
49aa0b2124b6 7144318: GCLocker assert failure: assert(_needs_gc || SafepointSynchronize::is_at_safepoint(
never
parents: 11788
diff changeset
    83
    // false outside of a safepoint, triggering the assert in
49aa0b2124b6 7144318: GCLocker assert failure: assert(_needs_gc || SafepointSynchronize::is_at_safepoint(
never
parents: 11788
diff changeset
    84
    // is_active.
49aa0b2124b6 7144318: GCLocker assert failure: assert(_needs_gc || SafepointSynchronize::is_at_safepoint(
never
parents: 11788
diff changeset
    85
    return needs_gc() && is_active_internal();
49aa0b2124b6 7144318: GCLocker assert failure: assert(_needs_gc || SafepointSynchronize::is_at_safepoint(
never
parents: 11788
diff changeset
    86
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
11631
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    88
  // In debug mode track the locking state at all times
25351
7c198a690050 8044775: Improve usage of umbrella header atomic.inline.hpp.
goetz
parents: 23540
diff changeset
    89
  static void increment_debug_jni_lock_count() NOT_DEBUG_RETURN;
7c198a690050 8044775: Improve usage of umbrella header atomic.inline.hpp.
goetz
parents: 23540
diff changeset
    90
  static void decrement_debug_jni_lock_count() NOT_DEBUG_RETURN;
11631
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    91
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    92
  // Set the current lock count
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    93
  static void set_jni_lock_count(int count) {
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    94
    _jni_lock_count = count;
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    95
    verify_critical_count();
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    96
  }
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    97
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
    98
  // Sets _needs_gc if is_active() is true. Returns is_active().
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  static bool check_active_before_gc();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  // Stalls the caller (who should not be in a jni critical section)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  // until needs_gc() clears. Note however that needs_gc() may be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  // set at a subsequent safepoint and/or cleared under the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  // JNICritical_lock, so the caller may not safely assert upon
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  // return from this method that "!needs_gc()" since that is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  // not a stable predicate.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  static void stall_until_clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  // The following two methods are used for JNI critical regions.
35492
c8c0273e6b91 8146690: Make all classes in GC follow the naming convention.
david
parents: 35061
diff changeset
   110
  // If we find that we failed to perform a GC because the GCLocker
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  // was active, arrange for one as soon as possible by allowing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  // all threads in critical regions to complete, but not allowing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  // other critical regions to be entered. The reasons for that are:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  // 1) a GC request won't be starved by overlapping JNI critical
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  //    region activities, which can cause unnecessary OutOfMemory errors.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  // 2) even if allocation requests can still be satisfied before GC locker
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  //    becomes inactive, for example, in tenured generation possibly with
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  //    heap expansion, those allocations can trigger lots of safepointing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  //    attempts (ineffective GC attempts) and require Heap_lock which
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  //    slow down allocations tremendously.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  // Note that critical regions can be nested in a single thread, so
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  // we must allow threads already in critical regions to continue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  // JNI critical regions are the only participants in this scheme
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  // because they are, by spec, well bounded while in a critical region.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  //
11631
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
   128
  // Each of the following two method is split into a fast path and a
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
   129
  // slow path. JNICritical_lock is only grabbed in the slow path.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  // _needs_gc is initially false and every java thread will go
11631
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
   131
  // through the fast path, which simply increments or decrements the
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
   132
  // current thread's critical count.  When GC happens at a safepoint,
35492
c8c0273e6b91 8146690: Make all classes in GC follow the naming convention.
david
parents: 35061
diff changeset
   133
  // GCLocker::is_active() is checked. Since there is no safepoint in
11631
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
   134
  // the fast path of lock_critical() and unlock_critical(), there is
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
   135
  // no race condition between the fast path and GC. After _needs_gc
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
   136
  // is set at a safepoint, every thread will go through the slow path
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
   137
  // after the safepoint.  Since after a safepoint, each of the
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
   138
  // following two methods is either entered from the method entry and
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
   139
  // falls into the slow path, or is resumed from the safepoints in
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
   140
  // the method, which only exist in the slow path. So when _needs_gc
33813f69207b 7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents: 10565
diff changeset
   141
  // is set, the slow path is always taken, till _needs_gc is cleared.
49594
898ef81cbc0e 8200106: Move NoSafepointVerifier out from gcLocker.hpp
stefank
parents: 47216
diff changeset
   142
  inline static void lock_critical(JavaThread* thread);
898ef81cbc0e 8200106: Move NoSafepointVerifier out from gcLocker.hpp
stefank
parents: 47216
diff changeset
   143
  inline static void unlock_critical(JavaThread* thread);
11637
030466036615 7013347: allow crypto functions to be called inline to enhance performance
never
parents: 11631
diff changeset
   144
030466036615 7013347: allow crypto functions to be called inline to enhance performance
never
parents: 11631
diff changeset
   145
  static address needs_gc_address() { return (address) &_needs_gc; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 25468
diff changeset
   148
#endif // SHARE_VM_GC_SHARED_GCLOCKER_HPP