src/hotspot/share/runtime/mutex.hpp
author kbarrett
Thu, 18 Jul 2019 14:57:32 -0400
changeset 55740 b3ff56f955c8
parent 55479 80b27dc96ca3
child 57668 33b160ef735c
permissions -rw-r--r--
8227653: Add VM Global OopStorage Summary: Replaced conditional JVMCI global storage with VM global storage. Reviewed-by: tschatzl, lfoltan, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52913
diff changeset
     2
 * Copyright (c) 1998, 2019, 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
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52913
diff changeset
    25
#ifndef SHARE_RUNTIME_MUTEX_HPP
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52913
diff changeset
    26
#define SHARE_RUNTIME_MUTEX_HPP
7397
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"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 53244
diff changeset
    31
// A Mutex/Monitor is a simple wrapper around a native lock plus condition
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 53244
diff changeset
    32
// variable that supports lock ownership tracking, lock ranking for deadlock
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 53244
diff changeset
    33
// detection and coordinates with the safepoint protocol.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
41710
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
    35
// 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
    36
// 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
    37
// 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
    38
static const int MONITOR_NAME_LEN = 64;
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
    39
53849
46ef4dea49e5 8219370: NMT: Move synchronization primitives from mtInternal to mtSynchronizer
zgu
parents: 53775
diff changeset
    40
class Monitor : public CHeapObj<mtSynchronizer> {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  // 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
    44
  // 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
    45
  // 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
    46
  // 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
    47
  // that require special barriers, e.g. SATB GC barriers, that in turn uses locks.
52913
bf2f2560dd53 8214315: G1: fatal error: acquiring lock SATB_Q_FL_lock/1 out of order with lock tty_lock/0
kbarrett
parents: 52555
diff changeset
    48
  // The rank 'tty' is also similar to 'special' and has the same restrictions.
bf2f2560dd53 8214315: G1: fatal error: acquiring lock SATB_Q_FL_lock/1 out of order with lock tty_lock/0
kbarrett
parents: 52555
diff changeset
    49
  // It is reserved for the tty_lock.
46685
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    50
  // 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
    51
  // in the code, that requires locks required for performing accesses being
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    52
  // inherently a bit more special than even locks of the 'special' rank.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  // 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
    54
  // (except for "event" and "access") for the deadlock detection to work correctly.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  // The rank native is only for use in Mutex's created by JVM_RawMonitorCreate,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  // which being external to the VM are not subject to deadlock detection.
53775
5d20b085d893 8203469: Faster safepoints
rehn
parents: 53646
diff changeset
    57
  // While at a safepoint no mutexes of rank safepoint are held by any thread.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  // The rank named "leaf" is probably historical (and should
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  // be changed) -- mutexes of this rank aren't really leaf mutexes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  // at all.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  enum lock_types {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
       event,
46685
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    63
       access         = event          +   1,
52913
bf2f2560dd53 8214315: G1: fatal error: acquiring lock SATB_Q_FL_lock/1 out of order with lock tty_lock/0
kbarrett
parents: 52555
diff changeset
    64
       tty            = access         +   2,
55479
80b27dc96ca3 8226699: [BACKOUT] JDK-8221734 Deoptimize with handshakes
dcubed
parents: 55005
diff changeset
    65
       special        = tty            +   1,
46685
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    66
       suspend_resume = special        +   1,
55740
b3ff56f955c8 8227653: Add VM Global OopStorage
kbarrett
parents: 55479
diff changeset
    67
       oopstorage     = suspend_resume +   2,
b3ff56f955c8 8227653: Add VM Global OopStorage
kbarrett
parents: 55479
diff changeset
    68
       leaf           = oopstorage     +   2,
46685
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    69
       safepoint      = leaf           +  10,
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    70
       barrier        = safepoint      +   1,
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    71
       nonleaf        = barrier        +   1,
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    72
       max_nonleaf    = nonleaf        + 900,
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    73
       native         = max_nonleaf    +   1
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
 protected:                              // Monitor-Mutex metadata
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  Thread * volatile _owner;              // The owner of the lock
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 53244
diff changeset
    78
  os::PlatformMonitor _lock;             // Native monitor implementation
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 53244
diff changeset
    79
  char _name[MONITOR_NAME_LEN];          // Name of mutex/monitor
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  bool      _allow_vm_block;
52555
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 50280
diff changeset
    84
  DEBUG_ONLY(int _rank;)                 // rank (to avoid/detect potential deadlocks)
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 50280
diff changeset
    85
  DEBUG_ONLY(Monitor * _next;)           // Used by a Thread to link up owned locks
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 50280
diff changeset
    86
  DEBUG_ONLY(Thread* _last_owner;)       // the last thread to own the lock
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 50280
diff changeset
    87
  DEBUG_ONLY(static bool contains(Monitor * locks, Monitor * lock);)
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 50280
diff changeset
    88
  DEBUG_ONLY(static Monitor * get_least_ranked_lock(Monitor * locks);)
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 50280
diff changeset
    89
  DEBUG_ONLY(Monitor * get_least_ranked_lock_besides_this(Monitor * locks);)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  void set_owner_implementation(Thread* owner)                        PRODUCT_RETURN;
50280
5aaf3a471172 8203817: Monitor::try_lock() should not call check_prelock_state()
pliden
parents: 50203
diff changeset
    93
  void check_prelock_state     (Thread* thread, bool safepoint_check) PRODUCT_RETURN;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  void check_block_state       (Thread* thread)                       PRODUCT_RETURN;
54663
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
    95
  void check_safepoint_state   (Thread* thread, bool safepoint_check) NOT_DEBUG_RETURN;
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 53244
diff changeset
    96
  void assert_owner            (Thread* expected)                     NOT_DEBUG_RETURN;
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
    97
  void assert_wait_lock_state  (Thread* self)                         NOT_DEBUG_RETURN;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    _allow_vm_block_flag        = true,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    _as_suspend_equivalent_flag = true
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
54663
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   105
  // Locks can be acquired with or without a safepoint check. NonJavaThreads do not follow
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   106
  // the safepoint protocol when acquiring locks.
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   107
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   108
  // Each lock can be acquired by only JavaThreads, only NonJavaThreads, or shared between
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   109
  // Java and NonJavaThreads. When the lock is initialized with _safepoint_check_always,
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   110
  // that means that whenever the lock is acquired by a JavaThread, it will verify that
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   111
  // it is done with a safepoint check. In corollary, when the lock is initialized with
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   112
  // _safepoint_check_never, that means that whenever the lock is acquired by a JavaThread
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   113
  // it will verify that it is done without a safepoint check.
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   114
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   115
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   116
  // There are a couple of existing locks that will sometimes have a safepoint check and
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   117
  // sometimes not when acquired by a JavaThread, but these locks are set up carefully
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   118
  // to avoid deadlocks. TODO: Fix these locks and remove _safepoint_check_sometimes.
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   119
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   120
  // TODO: Locks that are shared between JavaThreads and NonJavaThreads
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   121
  // should never encounter a safepoint check while they are held, or else a
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   122
  // deadlock can occur. We should check this by noting which
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   123
  // locks are shared, and walk held locks during safepoint checking.
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   124
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   125
  enum SafepointCheckFlag {
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   126
    _safepoint_check_flag,
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   127
    _no_safepoint_check_flag
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   128
  };
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   129
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   130
  enum SafepointCheckRequired {
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   131
    _safepoint_check_never,       // Monitors with this value will cause errors
54663
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   132
                                  // when acquired by a JavaThread with a safepoint check.
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   133
    _safepoint_check_sometimes,   // A couple of special locks are acquired by JavaThreads sometimes
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   134
                                  // with and sometimes without safepoint checks. These
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   135
                                  // locks will not produce errors when locked.
54663
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   136
    _safepoint_check_always       // Monitors with this value will cause errors
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   137
                                  // when acquired by a JavaThread without a safepoint check.
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   138
  };
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   139
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   140
  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
   141
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
 protected:
228
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
   143
   static void ClearMonitor (Monitor * m, const char* name = NULL) ;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
   Monitor() ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
 public:
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   147
  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
   148
          SafepointCheckRequired safepoint_check_required = _safepoint_check_always);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  ~Monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  // Wait until monitor is notified (or times out).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  // Defaults are to make safepoint checks, wait time is forever (i.e.,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  // zero), and not a suspend-equivalent condition. Returns true if wait
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  // times out; otherwise returns false.
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   155
  bool wait(long timeout = 0,
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
            bool as_suspend_equivalent = !_as_suspend_equivalent_flag);
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   157
  bool wait_without_safepoint_check(long timeout = 0);
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 53244
diff changeset
   158
  void notify();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 53244
diff changeset
   159
  void notify_all();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  void lock(); // prints out warning if VM thread blocks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  void lock(Thread *thread); // overloaded with current thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  void unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  bool is_locked() const                     { return _owner != NULL; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  bool try_lock(); // Like lock(), but unblocking. It returns false instead
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 53244
diff changeset
   169
  void release_for_safepoint();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 53244
diff changeset
   170
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  // Lock without safepoint check. Should ONLY be used by safepoint code and other code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  // that is guaranteed not to block while running inside the VM.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  void lock_without_safepoint_check();
54663
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   174
  void lock_without_safepoint_check(Thread* self);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  // Current owner - not not MT-safe. Can only be used to guarantee that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  // the current running thread owns the lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  Thread* owner() const         { return _owner; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  bool owned_by_self() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  // Support for JVM_RawMonitorEnter & JVM_RawMonitorExit. These can be called by
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  // non-Java thread. (We should really have a RawMonitor abstraction)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  void jvm_raw_lock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  void jvm_raw_unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  const char *name() const                  { return _name; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  void print_on_error(outputStream* st) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  #ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
    void print_on(outputStream* st) const;
52913
bf2f2560dd53 8214315: G1: fatal error: acquiring lock SATB_Q_FL_lock/1 out of order with lock tty_lock/0
kbarrett
parents: 52555
diff changeset
   191
    void print() const                      { print_on(::tty); }
52555
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 50280
diff changeset
   192
    DEBUG_ONLY(int    rank() const          { return _rank; })
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    bool   allow_vm_block()                 { return _allow_vm_block; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
52555
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 50280
diff changeset
   195
    DEBUG_ONLY(Monitor *next()  const         { return _next; })
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 50280
diff changeset
   196
    DEBUG_ONLY(void   set_next(Monitor *next) { _next = next; })
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  void set_owner(Thread* owner) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  #ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    set_owner_implementation(owner);
52555
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 50280
diff changeset
   202
    DEBUG_ONLY(void verify_Monitor(Thread* thr);)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  #else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    _owner = owner;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  #endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
41710
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   210
class PaddedMonitor : public Monitor {
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   211
  enum {
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   212
    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
   213
    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
   214
  };
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   215
  char _padding[PADDING_LEN];
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   216
 public:
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   217
  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
   218
               SafepointCheckRequired safepoint_check_required = _safepoint_check_always) :
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   219
    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
   220
};
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   221
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
// Normally we'd expect Monitor to extend Mutex in the sense that a monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
// constructed from pthreads primitives might extend a mutex by adding
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
// a condvar and some extra metadata.  In fact this was the case until J2SE7.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
// Currently, however, the base object is a monitor.  Monitor contains all the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
// 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
   228
// visibility of wait(), notify(), and notify_all().
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
// Another viable alternative would have been to have Monitor extend Mutex and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
// implement all the normal mutex and wait()-notify() logic in Mutex base class.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
// The wait()-notify() facility would be exposed via special protected member functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
// (e.g., _Wait() and _Notify()) in Mutex.  Monitor would extend Mutex and expose wait()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
// as a call to _Wait().  That is, the public wait() would be a wrapper for the protected
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
// _Wait().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
// An even better alternative is to simply eliminate Mutex:: and use Monitor:: instead.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
// After all, monitors are sufficient for Java-level synchronization.   At one point in time
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
// there may have been some benefit to having distinct mutexes and monitors, but that time
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   240
// has passed.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
class Mutex : public Monitor {      // degenerate Monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
 public:
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   245
   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
   246
         SafepointCheckRequired safepoint_check_required = _safepoint_check_always);
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   247
   // default destructor
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
 private:
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   249
   void notify();
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   250
   void notify_all();
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   251
   bool wait(long timeout, bool as_suspend_equivalent);
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   252
   bool wait_without_safepoint_check(long timeout);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
41710
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   255
class PaddedMutex : public Mutex {
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   256
  enum {
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   257
    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
   258
    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
   259
  };
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   260
  char _padding[PADDING_LEN];
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   261
public:
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   262
  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
   263
              SafepointCheckRequired safepoint_check_required = _safepoint_check_always) :
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   264
    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
   265
};
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
   266
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52913
diff changeset
   267
#endif // SHARE_RUNTIME_MUTEX_HPP