src/hotspot/share/runtime/mutex.hpp
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58454 d873ce07465d
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
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
57840
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
    35
// The default length of mutex name was originally chosen to be 64 to avoid
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
    36
// false sharing. Now, PaddedMutex and PaddedMonitor are available for this purpose.
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
    37
// TODO: Check if _name[MUTEX_NAME_LEN] should better get replaced by const char*.
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
    38
static const int MUTEX_NAME_LEN = 64;
228
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
    39
57840
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
    40
class Mutex : 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.
57751
7284b00e6db3 8228858: Reimplement JVM_RawMonitors to use PlatformMutex
dholmes
parents: 57668
diff changeset
    55
  // The rank native was only for use in Mutexes created by JVM_RawMonitorCreate,
7284b00e6db3 8228858: Reimplement JVM_RawMonitors to use PlatformMutex
dholmes
parents: 57668
diff changeset
    56
  // which being external to the VM are not subject to deadlock detection,
7284b00e6db3 8228858: Reimplement JVM_RawMonitors to use PlatformMutex
dholmes
parents: 57668
diff changeset
    57
  // however it has now been used by other locks that don't fit into the
7284b00e6db3 8228858: Reimplement JVM_RawMonitors to use PlatformMutex
dholmes
parents: 57668
diff changeset
    58
  // deadlock detection scheme.
53775
5d20b085d893 8203469: Faster safepoints
rehn
parents: 53646
diff changeset
    59
  // While at a safepoint no mutexes of rank safepoint are held by any thread.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  // The rank named "leaf" is probably historical (and should
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  // be changed) -- mutexes of this rank aren't really leaf mutexes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  // at all.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  enum lock_types {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
       event,
46685
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    65
       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
    66
       tty            = access         +   2,
58226
408c445d04e8 8226705: [REDO] Deoptimize with handshakes
rehn
parents: 57840
diff changeset
    67
       special        = tty            +   2,
46685
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    68
       suspend_resume = special        +   1,
55740
b3ff56f955c8 8227653: Add VM Global OopStorage
kbarrett
parents: 55479
diff changeset
    69
       oopstorage     = suspend_resume +   2,
b3ff56f955c8 8227653: Add VM Global OopStorage
kbarrett
parents: 55479
diff changeset
    70
       leaf           = oopstorage     +   2,
46685
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    71
       safepoint      = leaf           +  10,
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    72
       barrier        = safepoint      +   1,
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    73
       nonleaf        = barrier        +   1,
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    74
       max_nonleaf    = nonleaf        + 900,
b218dfc2853a 8182703: Correct G1 barrier queue lock orderings
eosterlund
parents: 41710
diff changeset
    75
       native         = max_nonleaf    +   1
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
 protected:                              // Monitor-Mutex metadata
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  Thread * volatile _owner;              // The owner of the lock
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 53244
diff changeset
    80
  os::PlatformMonitor _lock;             // Native monitor implementation
57840
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
    81
  char _name[MUTEX_NAME_LEN];            // Name of mutex/monitor
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode)
58291
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
    84
#ifdef ASSERT
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
    85
  bool    _allow_vm_block;
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
    86
  int     _rank;                 // rank (to avoid/detect potential deadlocks)
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
    87
  Mutex*  _next;                 // Used by a Thread to link up owned locks
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
    88
  Thread* _last_owner;           // the last thread to own the lock
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
    89
  static bool contains(Mutex* locks, Mutex* lock);
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
    90
  static Mutex* get_least_ranked_lock(Mutex* locks);
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
    91
  Mutex* get_least_ranked_lock_besides_this(Mutex* locks);
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
    92
#endif  // ASSERT
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
58291
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
    94
  void set_owner_implementation(Thread* owner)                        NOT_DEBUG({ _owner = owner;});
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
    95
  void check_block_state       (Thread* thread)                       NOT_DEBUG_RETURN;
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
    96
  void check_safepoint_state   (Thread* thread)                       NOT_DEBUG_RETURN;
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
    97
  void check_no_safepoint_state(Thread* thread)                       NOT_DEBUG_RETURN;
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 53244
diff changeset
    98
  void assert_owner            (Thread* expected)                     NOT_DEBUG_RETURN;
58291
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
    99
  void no_safepoint_verifier   (Thread* thread, bool enable)          NOT_DEBUG_RETURN;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    _allow_vm_block_flag        = true,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    _as_suspend_equivalent_flag = true
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
54663
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   107
  // 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
   108
  // the safepoint protocol when acquiring locks.
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   109
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   110
  // 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
   111
  // 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
   112
  // 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
   113
  // 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
   114
  // _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
   115
  // it will verify that it is done without a safepoint check.
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   116
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   117
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   118
  // 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
   119
  // 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
   120
  // 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
   121
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   122
  // TODO: Locks that are shared between JavaThreads and NonJavaThreads
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   123
  // 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
   124
  // deadlock can occur. We should check this by noting which
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   125
  // locks are shared, and walk held locks during safepoint checking.
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   126
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   127
  enum SafepointCheckFlag {
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   128
    _safepoint_check_flag,
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   129
    _no_safepoint_check_flag
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   130
  };
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54347
diff changeset
   131
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   132
  enum SafepointCheckRequired {
57840
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   133
    _safepoint_check_never,       // Mutexes with this value will cause errors
54663
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   134
                                  // when acquired by a JavaThread with a safepoint check.
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   135
    _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
   136
                                  // 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
   137
                                  // locks will not produce errors when locked.
57840
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   138
    _safepoint_check_always       // Mutexes with this value will cause errors
54663
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   139
                                  // 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
   140
  };
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   141
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 22551
diff changeset
   142
  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
   143
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
 public:
57840
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   145
  Mutex(int rank, const char *name, bool allow_vm_block = false,
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   146
        SafepointCheckRequired safepoint_check_required = _safepoint_check_always);
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   147
  ~Mutex();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  void lock(); // prints out warning if VM thread blocks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  void lock(Thread *thread); // overloaded with current thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  void unlock();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  bool is_locked() const                     { return _owner != NULL; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  bool try_lock(); // Like lock(), but unblocking. It returns false instead
58454
d873ce07465d 8231707: Improve Mutex inlining
redestad
parents: 58291
diff changeset
   155
 private:
d873ce07465d 8231707: Improve Mutex inlining
redestad
parents: 58291
diff changeset
   156
  void lock_contended(Thread *thread); // contended slow-path
d873ce07465d 8231707: Improve Mutex inlining
redestad
parents: 58291
diff changeset
   157
 public:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 53244
diff changeset
   159
  void release_for_safepoint();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 53244
diff changeset
   160
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  // Lock without safepoint check. Should ONLY be used by safepoint code and other code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  // that is guaranteed not to block while running inside the VM.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  void lock_without_safepoint_check();
54663
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   164
  void lock_without_safepoint_check(Thread* self);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  // Current owner - not not MT-safe. Can only be used to guarantee that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  // the current running thread owns the lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  Thread* owner() const         { return _owner; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  bool owned_by_self() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  const char *name() const                  { return _name; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  void print_on_error(outputStream* st) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  #ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    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
   177
    void print() const                      { print_on(::tty); }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  #endif
58291
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
   179
  #ifdef ASSERT
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
   180
    int    rank() const          { return _rank; }
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
   181
    bool   allow_vm_block()      { return _allow_vm_block; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
58291
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
   183
    Mutex *next()  const         { return _next; }
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
   184
    void   set_next(Mutex *next) { _next = next; }
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
   185
  #endif // ASSERT
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
   186
a013100f7a35 8213150: Add verification for locking by VMThread
coleenp
parents: 58226
diff changeset
   187
  void set_owner(Thread* owner)             { set_owner_implementation(owner); }
57840
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   188
};
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
57840
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   190
class Monitor : public Mutex {
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   191
  void assert_wait_lock_state  (Thread* self)                         NOT_DEBUG_RETURN;
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   192
 public:
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   193
   Monitor(int rank, const char *name, bool allow_vm_block = false,
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   194
         SafepointCheckRequired safepoint_check_required = _safepoint_check_always);
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   195
   // default destructor
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   196
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   197
  // Wait until monitor is notified (or times out).
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   198
  // Defaults are to make safepoint checks, wait time is forever (i.e.,
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   199
  // zero), and not a suspend-equivalent condition. Returns true if wait
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   200
  // times out; otherwise returns false.
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   201
  bool wait(long timeout = 0,
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   202
            bool as_suspend_equivalent = !_as_suspend_equivalent_flag);
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   203
  bool wait_without_safepoint_check(long timeout = 0);
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   204
  void notify();
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   205
  void notify_all();
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   206
};
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   207
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   208
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   209
class PaddedMutex : public Mutex {
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   210
  enum {
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   211
    CACHE_LINE_PADDING = (int)DEFAULT_CACHE_LINE_SIZE - (int)sizeof(Mutex),
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   212
    PADDING_LEN = CACHE_LINE_PADDING > 0 ? CACHE_LINE_PADDING : 1
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   213
  };
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   214
  char _padding[PADDING_LEN];
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   215
public:
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   216
  PaddedMutex(int rank, const char *name, bool allow_vm_block = false,
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   217
              SafepointCheckRequired safepoint_check_required = _safepoint_check_always) :
4863a802a7c1 8230003: Make Monitor inherit from Mutex
coleenp
parents: 57751
diff changeset
   218
    Mutex(rank, name, allow_vm_block, safepoint_check_required) {};
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
41710
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   221
class PaddedMonitor : public Monitor {
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   222
  enum {
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   223
    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
   224
    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
   225
  };
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   226
  char _padding[PADDING_LEN];
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   227
 public:
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   228
  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
   229
               SafepointCheckRequired safepoint_check_required = _safepoint_check_always) :
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   230
    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
   231
};
b830f5141dbb 8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents: 28163
diff changeset
   232
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52913
diff changeset
   233
#endif // SHARE_RUNTIME_MUTEX_HPP