src/hotspot/share/runtime/mutex.hpp
author stefank
Sun, 20 May 2018 22:10:45 +0200
changeset 50192 8bc79d2d1568
parent 49818 e57e6addb978
child 50203 39d88709b138
permissions -rw-r--r--
8203341: Add a safepoint-aware Semaphore Reviewed-by: pliden, eosterlund
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
49818
e57e6addb978 8201505: Use WeakHandle for ProtectionDomainCacheTable and ResolvedMethodTable
coleenp
parents: 47634
diff changeset
     2
 * Copyright (c) 1998, 2018, 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: 4013
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4013
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: 4013
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
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    25
#ifndef SHARE_VM_RUNTIME_MUTEX_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    26
#define SHARE_VM_RUNTIME_MUTEX_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    28
#include "memory/allocation.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    29
#include "runtime/os.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    30
#include "utilities/histogram.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    31
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// The SplitWord construct allows us to colocate the contention queue
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
// (cxq) with the lock-byte.  The queue elements are ParkEvents, which are
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// always aligned on 256-byte addresses - the least significant byte of
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// a ParkEvent is always 0.  Colocating the lock-byte with the queue
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// allows us to easily avoid what would otherwise be a race in lock()
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// if we were to use two completely separate fields for the contention queue
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// and the lock indicator.  Specifically, colocation renders us immune
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
// from the race where a thread might enqueue itself in the lock() slow-path
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
// immediately after the lock holder drops the outer lock in the unlock()
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
// fast-path.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
// Colocation allows us to use a fast-path unlock() form that uses
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
// A MEMBAR instead of a CAS.  MEMBAR has lower local latency than CAS
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
// on many platforms.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
// See:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
// +  http://blogs.sun.com/dave/entry/biased_locking_in_hotspot
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
// +  http://blogs.sun.com/dave/resource/synchronization-public2.pdf
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
// Note that we're *not* using word-tearing the classic sense.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
// The lock() fast-path will CAS the lockword and the unlock()
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
// fast-path will store into the lock-byte colocated within the lockword.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
// We depend on the fact that all our reference platforms have
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
// coherent and atomic byte accesses.  More precisely, byte stores
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
// interoperate in a safe, sane, and expected manner with respect to
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
// CAS, ST and LDs to the full-word containing the byte.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
// If you're porting HotSpot to a platform where that isn't the case
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
// then you'll want change the unlock() fast path from:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
//    STB;MEMBAR #storeload; LDN
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
// to a full-word CAS of the lockword.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
union SplitWord {   // full-word with separately addressable LSB
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  volatile intptr_t FullWord ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  volatile void * Address ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  volatile jbyte Bytes [sizeof(intptr_t)] ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
} ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
class ParkEvent ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
// See orderAccess.hpp.  We assume throughout the VM that mutex lock and
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
// try_lock do fence-lock-acquire, and that unlock does a release-unlock,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
// *in that order*.  If their implementations change such that these
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
// assumptions are violated, a whole lot of code will break.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
41710
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
    77
// The default length of monitor name was originally chosen to be 64 to avoid
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
    78
// false sharing. Now, PaddedMonitor is available for this purpose.
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
    79
// TODO: Check if _name[MONITOR_NAME_LEN] should better get replaced by const char*.
228
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
    80
static const int MONITOR_NAME_LEN = 64;
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
    81
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
    82
class Monitor : public CHeapObj<mtInternal> {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  // A special lock: Is a lock where you are guaranteed not to block while you are
46685
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    86
  // holding it, i.e., no vm operation can happen, taking other (blocking) locks, etc.
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    87
  // The rank 'access' is similar to 'special' and has the same restrictions on usage.
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    88
  // It is reserved for locks that may be required in order to perform memory accesses
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    89
  // that require special barriers, e.g. SATB GC barriers, that in turn uses locks.
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    90
  // Since memory accesses should be able to be performed pretty much anywhere
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    91
  // in the code, that requires locks required for performing accesses being
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    92
  // inherently a bit more special than even locks of the 'special' rank.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  // NOTE: It is critical that the rank 'special' be the lowest (earliest)
46685
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    94
  // (except for "event" and "access") for the deadlock detection to work correctly.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  // The rank native is only for use in Mutex's created by JVM_RawMonitorCreate,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  // which being external to the VM are not subject to deadlock detection.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  // The rank safepoint is used only for synchronization in reaching a
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  // safepoint and leaving a safepoint.  It is only used for the Safepoint_lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  // currently.  While at a safepoint no mutexes of rank safepoint are held
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  // by any thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  // The rank named "leaf" is probably historical (and should
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  // be changed) -- mutexes of this rank aren't really leaf mutexes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  // at all.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  enum lock_types {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
       event,
46685
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
   106
       access         = event          +   1,
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
   107
       special        = access         +   2,
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
   108
       suspend_resume = special        +   1,
49818
e57e6addb978 8201505: Use WeakHandle for ProtectionDomainCacheTable and ResolvedMethodTable
coleenp
parents: 47634
diff changeset
   109
       vmweak         = suspend_resume +   2,
e57e6addb978 8201505: Use WeakHandle for ProtectionDomainCacheTable and ResolvedMethodTable
coleenp
parents: 47634
diff changeset
   110
       leaf           = vmweak         +   2,
46685
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
   111
       safepoint      = leaf           +  10,
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
   112
       barrier        = safepoint      +   1,
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
   113
       nonleaf        = barrier        +   1,
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
   114
       max_nonleaf    = nonleaf        + 900,
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
   115
       native         = max_nonleaf    +   1
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  // The WaitSet and EntryList linked lists are composed of ParkEvents.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  // I use ParkEvent instead of threads as ParkEvents are immortal and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  // type-stable, meaning we can safely unpark() a possibly stale
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  // list element in the unlock()-path.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
 protected:                              // Monitor-Mutex metadata
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  SplitWord _LockWord ;                  // Contention queue (cxq) colocated with Lock-byte
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  Thread * volatile _owner;              // The owner of the lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
                                         // Consider sequestering _owner on its own $line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
                                         // to aid future synchronization mechanisms.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  ParkEvent * volatile _EntryList ;      // List of threads waiting for entry
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  ParkEvent * volatile _OnDeck ;         // heir-presumptive
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  volatile intptr_t _WaitLock [1] ;      // Protects _WaitSet
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  ParkEvent * volatile  _WaitSet ;       // LL of ParkEvents
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  volatile bool     _snuck;              // Used for sneaky locking (evil).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  int NotifyCount ;                      // diagnostic assist
228
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
   134
  char _name[MONITOR_NAME_LEN];          // Name of mutex
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  bool      _allow_vm_block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  debug_only(int _rank;)                 // rank (to avoid/detect potential deadlocks)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  debug_only(Monitor * _next;)           // Used by a Thread to link up owned locks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  debug_only(Thread* _last_owner;)       // the last thread to own the lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  debug_only(static bool contains(Monitor * locks, Monitor * lock);)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  debug_only(static Monitor * get_least_ranked_lock(Monitor * locks);)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  debug_only(Monitor * get_least_ranked_lock_besides_this(Monitor * locks);)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  void set_owner_implementation(Thread* owner)                        PRODUCT_RETURN;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  void check_prelock_state     (Thread* thread)                       PRODUCT_RETURN;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  void check_block_state       (Thread* thread)                       PRODUCT_RETURN;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  // platform-dependent support code can go here (in os_<os_family>.cpp)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
    _no_safepoint_check_flag    = true,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    _allow_vm_block_flag        = true,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    _as_suspend_equivalent_flag = true
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   159
  // Locks can be acquired with or without safepoint check.
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   160
  // Monitor::lock and Monitor::lock_without_safepoint_check
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   161
  // checks these flags when acquiring a lock to ensure
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   162
  // consistent checking for each lock.
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   163
  // A few existing locks will sometimes have a safepoint check and
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   164
  // sometimes not, but these locks are set up in such a way to avoid deadlocks.
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   165
  enum SafepointCheckRequired {
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   166
    _safepoint_check_never,       // Monitors with this value will cause errors
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   167
                                  // when acquired with a safepoint check.
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   168
    _safepoint_check_sometimes,   // Certain locks are called sometimes with and
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   169
                                  // sometimes without safepoint checks. These
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   170
                                  // locks will not produce errors when locked.
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   171
    _safepoint_check_always       // Causes error if locked without a safepoint
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   172
                                  // check.
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   173
  };
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   174
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   175
  NOT_PRODUCT(SafepointCheckRequired _safepoint_check_required;)
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   176
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  enum WaitResults {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    CONDVAR_EVENT,         // Wait returned because of condition variable notification
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    INTERRUPT_EVENT,       // Wait returned because waiting thread was interrupted
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    NUMBER_WAIT_RESULTS
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
   int  TrySpin (Thread * Self) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
   int  TryLock () ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
   int  TryFast () ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
   int  AcquireOrPush (ParkEvent * ev) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
   void IUnlock (bool RelaxAssert) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
   void ILock (Thread * Self) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
   int  IWait (Thread * Self, jlong timo);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
   int  ILocked () ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
 protected:
228
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
   194
   static void ClearMonitor (Monitor * m, const char* name = NULL) ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
   Monitor() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
 public:
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   198
  Monitor(int rank, const char *name, bool allow_vm_block = false,
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   199
          SafepointCheckRequired safepoint_check_required = _safepoint_check_always);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  ~Monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  // Wait until monitor is notified (or times out).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  // Defaults are to make safepoint checks, wait time is forever (i.e.,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  // zero), and not a suspend-equivalent condition. Returns true if wait
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  // times out; otherwise returns false.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  bool wait(bool no_safepoint_check = !_no_safepoint_check_flag,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
            long timeout = 0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
            bool as_suspend_equivalent = !_as_suspend_equivalent_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  bool notify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  bool notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  void lock(); // prints out warning if VM thread blocks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  void lock(Thread *thread); // overloaded with current thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  void unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  bool is_locked() const                     { return _owner != NULL; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  bool try_lock(); // Like lock(), but unblocking. It returns false instead
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  // Lock without safepoint check. Should ONLY be used by safepoint code and other code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  // that is guaranteed not to block while running inside the VM.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  void lock_without_safepoint_check();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  void lock_without_safepoint_check (Thread * Self) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  // Current owner - not not MT-safe. Can only be used to guarantee that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  // the current running thread owns the lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  Thread* owner() const         { return _owner; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  bool owned_by_self() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  // Support for JVM_RawMonitorEnter & JVM_RawMonitorExit. These can be called by
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  // non-Java thread. (We should really have a RawMonitor abstraction)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  void jvm_raw_lock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  void jvm_raw_unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
  const char *name() const                  { return _name; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  void print_on_error(outputStream* st) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  #ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    void print_on(outputStream* st) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
    void print() const                      { print_on(tty); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    debug_only(int    rank() const          { return _rank; })
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
    bool   allow_vm_block()                 { return _allow_vm_block; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
    debug_only(Monitor *next()  const         { return _next; })
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
    debug_only(void   set_next(Monitor *next) { _next = next; })
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
  void set_owner(Thread* owner) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
  #ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
    set_owner_implementation(owner);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
    debug_only(void verify_Monitor(Thread* thr));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  #else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
    _owner = owner;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
41710
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   259
class PaddedMonitor : public Monitor {
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   260
  enum {
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   261
    CACHE_LINE_PADDING = (int)DEFAULT_CACHE_LINE_SIZE - (int)sizeof(Monitor),
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   262
    PADDING_LEN = CACHE_LINE_PADDING > 0 ? CACHE_LINE_PADDING : 1
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   263
  };
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   264
  char _padding[PADDING_LEN];
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   265
 public:
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   266
  PaddedMonitor(int rank, const char *name, bool allow_vm_block = false,
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   267
               SafepointCheckRequired safepoint_check_required = _safepoint_check_always) :
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   268
    Monitor(rank, name, allow_vm_block, safepoint_check_required) {};
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   269
};
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   270
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
// Normally we'd expect Monitor to extend Mutex in the sense that a monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
// constructed from pthreads primitives might extend a mutex by adding
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
// a condvar and some extra metadata.  In fact this was the case until J2SE7.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
// Currently, however, the base object is a monitor.  Monitor contains all the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
// logic for wait(), notify(), etc.   Mutex extends monitor and restricts the
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 13963
diff changeset
   277
// visibility of wait(), notify(), and notify_all().
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
// Another viable alternative would have been to have Monitor extend Mutex and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
// implement all the normal mutex and wait()-notify() logic in Mutex base class.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
// The wait()-notify() facility would be exposed via special protected member functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
// (e.g., _Wait() and _Notify()) in Mutex.  Monitor would extend Mutex and expose wait()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
// as a call to _Wait().  That is, the public wait() would be a wrapper for the protected
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
// _Wait().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
// An even better alternative is to simply eliminate Mutex:: and use Monitor:: instead.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
// After all, monitors are sufficient for Java-level synchronization.   At one point in time
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
// there may have been some benefit to having distinct mutexes and monitors, but that time
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
// has past.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
// The Mutex/Monitor design parallels that of Java-monitors, being based on
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
// thread-specific park-unpark platform-specific primitives.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
class Mutex : public Monitor {      // degenerate Monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
 public:
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   297
   Mutex(int rank, const char *name, bool allow_vm_block = false,
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   298
         SafepointCheckRequired safepoint_check_required = _safepoint_check_always);
46767
e2bb2b8ff65a 8185746: Remove Mutex destructor assertion
kbarrett
parents: 46685
diff changeset
   299
  // default destructor
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
   bool notify ()    { ShouldNotReachHere(); return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
   bool notify_all() { ShouldNotReachHere(); return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
   bool wait (bool no_safepoint_check, long timeout, bool as_suspend_equivalent) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
     ShouldNotReachHere() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
     return false ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
41710
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   309
class PaddedMutex : public Mutex {
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   310
  enum {
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   311
    CACHE_LINE_PADDING = (int)DEFAULT_CACHE_LINE_SIZE - (int)sizeof(Mutex),
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   312
    PADDING_LEN = CACHE_LINE_PADDING > 0 ? CACHE_LINE_PADDING : 1
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   313
  };
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   314
  char _padding[PADDING_LEN];
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   315
public:
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   316
  PaddedMutex(int rank, const char *name, bool allow_vm_block = false,
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   317
              SafepointCheckRequired safepoint_check_required = _safepoint_check_always) :
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   318
    Mutex(rank, name, allow_vm_block, safepoint_check_required) {};
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   319
};
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
   320
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
   321
#endif // SHARE_VM_RUNTIME_MUTEX_HPP