src/hotspot/share/runtime/mutex.cpp
author coleenp
Wed, 10 Apr 2019 17:31:31 -0400
changeset 54495 941db9c0b5b5
parent 53775 5d20b085d893
child 54623 1126f0607c70
permissions -rw-r--r--
8222231: Clean up interfaceSupport.inline.hpp duplicated code Reviewed-by: dholmes, pchilanomate
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
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: 5403
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5403
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: 5403
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: 5547
diff changeset
    25
#include "precompiled.hpp"
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    26
#include "logging/log.hpp"
49449
ef5d5d343e2a 8199263: Split interfaceSupport.hpp to not require including .inline.hpp files
coleenp
parents: 48488
diff changeset
    27
#include "runtime/interfaceSupport.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "runtime/mutex.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "runtime/osThread.hpp"
47881
0ce0ac68ace7 8189941: Implementation JEP 312: Thread-local handshake
rehn
parents: 47634
diff changeset
    30
#include "runtime/safepointMechanism.inline.hpp"
14583
d70ee55535f4 8003935: Simplify the needed includes for using Thread::current()
stefank
parents: 11636
diff changeset
    31
#include "runtime/thread.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    32
#include "utilities/events.hpp"
40010
e32d5e545789 8161258: Simplify including platform files.
goetz
parents: 38308
diff changeset
    33
#include "utilities/macros.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    36
void Monitor::lock(Thread * self) {
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
    37
  // Ensure that the Monitor requires/allows safepoint checks.
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
    38
  assert(_safepoint_check_required != Monitor::_safepoint_check_never,
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
    39
         "This lock should never have a safepoint check: %s", name());
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
    40
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
#ifdef CHECK_UNHANDLED_OOPS
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    42
  // Clear unhandled oops in JavaThreads so we get a crash right away.
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    43
  if (self->is_Java_thread()) {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    44
    self->clear_unhandled_oops();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
#endif // CHECK_UNHANDLED_OOPS
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    48
  DEBUG_ONLY(check_prelock_state(self, StrictSafepointChecks));
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    49
  assert(_owner != self, "invariant");
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    50
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    51
  Monitor* in_flight_monitor = NULL;
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    52
  DEBUG_ONLY(int retry_cnt = 0;)
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    53
  while (!_lock.try_lock()) {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    54
    // The lock is contended
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    55
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    56
  #ifdef ASSERT
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    57
    check_block_state(self);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    58
    if (retry_cnt++ > 3) {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    59
      log_trace(vmmonitor)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmonitor %s", p2i(self), retry_cnt, _name);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    60
    }
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    61
  #endif // ASSERT
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    63
    if (self->is_Java_thread()) {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    64
      assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex");
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    65
      { ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_monitor);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    66
        in_flight_monitor = this;  // save for ~ThreadBlockInVMWithDeadlockCheck
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    67
        _lock.lock();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    68
      }
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    69
      if (in_flight_monitor != NULL) {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    70
        // Not unlocked by ~ThreadBlockInVMWithDeadlockCheck
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    71
        break;
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    72
      }
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    73
    } else {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    74
      _lock.lock();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    75
      break;
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    76
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    79
  assert_owner(NULL);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    80
  set_owner(self);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
void Monitor::lock() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  this->lock(Thread::current());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    87
// Lock without safepoint check - a degenerate variant of lock() for use by
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    88
// JavaThreads when it is known to be safe to not check for a safepoint when
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    89
// acquiring this lock. If the thread blocks acquiring the lock it is not
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    90
// safepoint-safe and so will prevent a safepoint from being reached. If used
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    91
// in the wrong way this can lead to a deadlock with the safepoint code.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    93
void Monitor::lock_without_safepoint_check(Thread * self) {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    94
  // Ensure that the Monitor does not require safepoint checks.
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
    95
  assert(_safepoint_check_required != Monitor::_safepoint_check_always,
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
    96
         "This lock should always have a safepoint check: %s", name());
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    97
  assert(_owner != self, "invariant");
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    98
  _lock.lock();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
    99
  assert_owner(NULL);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   100
  set_owner(self);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   103
void Monitor::lock_without_safepoint_check() {
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   104
  lock_without_safepoint_check(Thread::current());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 22234
diff changeset
   108
// Returns true if thread succeeds in grabbing the lock, otherwise false.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
bool Monitor::try_lock() {
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   111
  Thread * const self = Thread::current();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   112
  DEBUG_ONLY(check_prelock_state(self, false);)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   114
  if (_lock.try_lock()) {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   115
    assert_owner(NULL);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   116
    set_owner(self);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   122
void Monitor::release_for_safepoint() {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   123
  assert_owner(NULL);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   124
  _lock.unlock();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   127
void Monitor::unlock() {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   128
  assert_owner(Thread::current());
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   129
  set_owner(NULL);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   130
  _lock.unlock();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   133
void Monitor::notify() {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   134
  assert_owner(Thread::current());
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   135
  _lock.notify();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   136
}
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   137
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   138
void Monitor::notify_all() {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   139
  assert_owner(Thread::current());
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   140
  _lock.notify_all();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   143
bool Monitor::wait(bool no_safepoint_check, long timeout,
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   144
                   bool as_suspend_equivalent) {
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   145
  // Make sure safepoint checking is used properly.
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   146
  assert(!(_safepoint_check_required == Monitor::_safepoint_check_never && no_safepoint_check == false),
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
   147
         "This lock should never have a safepoint check: %s", name());
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   148
  assert(!(_safepoint_check_required == Monitor::_safepoint_check_always && no_safepoint_check == true),
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
   149
         "This lock should always have a safepoint check: %s", name());
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   150
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   151
  // timeout is in milliseconds - with zero meaning never timeout
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   152
  assert(timeout >= 0, "negative timeout");
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   153
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   154
  Thread * const self = Thread::current();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   155
  assert_owner(self);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  // as_suspend_equivalent logically implies !no_safepoint_check
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   158
  guarantee(!as_suspend_equivalent || !no_safepoint_check, "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  // !no_safepoint_check logically implies java_thread
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   160
  guarantee(no_safepoint_check || self->is_Java_thread(), "invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
54495
941db9c0b5b5 8222231: Clean up interfaceSupport.inline.hpp duplicated code
coleenp
parents: 53775
diff changeset
   162
#ifdef ASSERT
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   163
  Monitor * least = get_least_ranked_lock_besides_this(self->owned_locks());
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   164
  assert(least != this, "Specification of get_least_... call above");
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   165
  if (least != NULL && least->rank() <= special) {
52913
bf2f2560dd53 8214315: G1: fatal error: acquiring lock SATB_Q_FL_lock/1 out of order with lock tty_lock/0
kbarrett
parents: 52581
diff changeset
   166
    ::tty->print("Attempting to wait on monitor %s/%d while holding"
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   167
               " lock %s/%d -- possible deadlock",
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   168
               name(), rank(), least->name(), least->rank());
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   169
    assert(false, "Shouldn't block(wait) while holding a lock of rank special");
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   170
  }
54495
941db9c0b5b5 8222231: Clean up interfaceSupport.inline.hpp duplicated code
coleenp
parents: 53775
diff changeset
   171
#endif // ASSERT
941db9c0b5b5 8222231: Clean up interfaceSupport.inline.hpp duplicated code
coleenp
parents: 53775
diff changeset
   172
941db9c0b5b5 8222231: Clean up interfaceSupport.inline.hpp duplicated code
coleenp
parents: 53775
diff changeset
   173
#ifdef CHECK_UNHANDLED_OOPS
941db9c0b5b5 8222231: Clean up interfaceSupport.inline.hpp duplicated code
coleenp
parents: 53775
diff changeset
   174
  // Clear unhandled oops in JavaThreads so we get a crash right away.
941db9c0b5b5 8222231: Clean up interfaceSupport.inline.hpp duplicated code
coleenp
parents: 53775
diff changeset
   175
  if (self->is_Java_thread() && !no_safepoint_check) {
941db9c0b5b5 8222231: Clean up interfaceSupport.inline.hpp duplicated code
coleenp
parents: 53775
diff changeset
   176
    self->clear_unhandled_oops();
941db9c0b5b5 8222231: Clean up interfaceSupport.inline.hpp duplicated code
coleenp
parents: 53775
diff changeset
   177
  }
941db9c0b5b5 8222231: Clean up interfaceSupport.inline.hpp duplicated code
coleenp
parents: 53775
diff changeset
   178
#endif // CHECK_UNHANDLED_OOPS
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   180
  int wait_status;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  // conceptually set the owner to NULL in anticipation of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  // abdicating the lock in wait
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  set_owner(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  if (no_safepoint_check) {
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   185
    wait_status = _lock.wait(timeout);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   186
    set_owner(self);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  } else {
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   188
    assert(self->is_Java_thread(), "invariant");
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   189
    JavaThread *jt = (JavaThread *)self;
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   190
    Monitor* in_flight_monitor = NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   192
    {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   193
      ThreadBlockInVMWithDeadlockCheck tbivmdc(jt, &in_flight_monitor);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   194
      OSThreadWaitState osts(self->osthread(), false /* not Object.wait() */);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   195
      if (as_suspend_equivalent) {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   196
        jt->set_suspend_equivalent();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   197
        // cleared by handle_special_suspend_equivalent_condition() or
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   198
        // java_suspend_self()
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   199
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   201
      wait_status = _lock.wait(timeout);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   202
      in_flight_monitor = this;  // save for ~ThreadBlockInVMWithDeadlockCheck
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   203
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   204
      // were we externally suspended while we were waiting?
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   205
      if (as_suspend_equivalent && jt->handle_special_suspend_equivalent_condition()) {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   206
        // Our event wait has finished and we own the lock, but
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   207
        // while we were waiting another thread suspended us. We don't
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   208
        // want to hold the lock while suspended because that
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   209
        // would surprise the thread that suspended us.
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   210
        _lock.unlock();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   211
        jt->java_suspend_self();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   212
        _lock.lock();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   213
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   216
    if (in_flight_monitor != NULL) {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   217
      // Not unlocked by ~ThreadBlockInVMWithDeadlockCheck
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   218
      assert_owner(NULL);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   219
      // Conceptually reestablish ownership of the lock.
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   220
      set_owner(self);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   221
    } else {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   222
      lock(self);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  }
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   225
  return wait_status != 0;          // return true IFF timeout
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   228
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   229
// Temporary JVM_RawMonitor* support.
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   230
// Yet another degenerate version of Monitor::lock() or lock_without_safepoint_check()
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   231
// jvm_raw_lock() and _unlock() can be called by non-Java threads via JVM_RawMonitorEnter.
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   232
// There's no expectation that JVM_RawMonitors will interoperate properly with the native
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   233
// Mutex-Monitor constructs.  We happen to implement JVM_RawMonitors in terms of
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   234
// native Mutex-Monitors simply as a matter of convenience.
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   235
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   236
void Monitor::jvm_raw_lock() {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   237
  _lock.lock();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   238
  assert_owner(NULL);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   239
}
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   240
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   241
void Monitor::jvm_raw_unlock() {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   242
  assert_owner(NULL);
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   243
  _lock.unlock();
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   244
}
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   245
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
Monitor::~Monitor() {
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   247
  assert_owner(NULL);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   250
void Monitor::ClearMonitor(Monitor * m, const char *name) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   251
  m->_owner             = NULL;
228
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
   252
  if (name == NULL) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   253
    strcpy(m->_name, "UNKNOWN");
228
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
   254
  } else {
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
   255
    strncpy(m->_name, name, MONITOR_NAME_LEN - 1);
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
   256
    m->_name[MONITOR_NAME_LEN - 1] = '\0';
69939fa91efd 6610420: Debug VM crashes during monitor lock rank checking
xlu
parents: 1
diff changeset
   257
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
52581
d402a406bbc3 8213723: More Monitor/mutex initialization management
dholmes
parents: 52555
diff changeset
   260
Monitor::Monitor() {
d402a406bbc3 8213723: More Monitor/mutex initialization management
dholmes
parents: 52555
diff changeset
   261
  assert(os::mutex_init_done(), "Too early!");
d402a406bbc3 8213723: More Monitor/mutex initialization management
dholmes
parents: 52555
diff changeset
   262
  ClearMonitor(this);
d402a406bbc3 8213723: More Monitor/mutex initialization management
dholmes
parents: 52555
diff changeset
   263
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   265
Monitor::Monitor(int Rank, const char * name, bool allow_vm_block,
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   266
                 SafepointCheckRequired safepoint_check_required) {
52581
d402a406bbc3 8213723: More Monitor/mutex initialization management
dholmes
parents: 52555
diff changeset
   267
  assert(os::mutex_init_done(), "Too early!");
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   268
  ClearMonitor(this, name);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  _allow_vm_block  = allow_vm_block;
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   271
  _rank            = Rank;
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   272
  NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   276
Mutex::Mutex(int Rank, const char * name, bool allow_vm_block,
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   277
             SafepointCheckRequired safepoint_check_required) {
25069
c937c5e883c5 8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents: 24424
diff changeset
   278
  ClearMonitor((Monitor *) this, name);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
#ifdef ASSERT
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   280
  _allow_vm_block   = allow_vm_block;
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   281
  _rank             = Rank;
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 26684
diff changeset
   282
  NOT_PRODUCT(_safepoint_check_required = safepoint_check_required;)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
bool Monitor::owned_by_self() const {
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   287
  return _owner == Thread::current();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
void Monitor::print_on_error(outputStream* st) const {
33148
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 33105
diff changeset
   291
  st->print("[" PTR_FORMAT, p2i(this));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  st->print("] %s", _name);
33148
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 33105
diff changeset
   293
  st->print(" - owner thread: " PTR_FORMAT, p2i(_owner));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
// ----------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
// Non-product code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
void Monitor::print_on(outputStream* st) const {
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   301
  st->print_cr("Mutex: [" PTR_FORMAT "] %s - owner: " PTR_FORMAT,
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   302
               p2i(this), _name, p2i(_owner));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
#ifdef ASSERT
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   308
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   309
void Monitor::assert_owner(Thread * expected) {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   310
  const char* msg = "invalid owner";
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   311
  if (expected == NULL) {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   312
    msg = "should be un-owned";
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   313
  }
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   314
  else if (expected == Thread::current()) {
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   315
    msg = "should be owned by current thread";
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   316
  }
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   317
  assert(_owner == expected,
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   318
         "%s: owner=" INTPTR_FORMAT ", should be=" INTPTR_FORMAT,
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   319
         msg, p2i(_owner), p2i(expected));
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   320
}
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   321
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
Monitor * Monitor::get_least_ranked_lock(Monitor * locks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  Monitor *res, *tmp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  for (res = tmp = locks; tmp != NULL; tmp = tmp->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
    if (tmp->rank() < res->rank()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
      res = tmp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
  if (!SafepointSynchronize::is_at_safepoint()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
    // In this case, we expect the held locks to be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    // in increasing rank order (modulo any native ranks)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
    for (tmp = locks; tmp != NULL; tmp = tmp->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
      if (tmp->next() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
        assert(tmp->rank() == Mutex::native ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
               tmp->rank() <= tmp->next()->rank(), "mutex rank anomaly?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
Monitor* Monitor::get_least_ranked_lock_besides_this(Monitor* locks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
  Monitor *res, *tmp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  for (res = NULL, tmp = locks; tmp != NULL; tmp = tmp->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
    if (tmp != this && (res == NULL || tmp->rank() < res->rank())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
      res = tmp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  if (!SafepointSynchronize::is_at_safepoint()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
    // In this case, we expect the held locks to be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
    // in increasing rank order (modulo any native ranks)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
    for (tmp = locks; tmp != NULL; tmp = tmp->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
      if (tmp->next() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
        assert(tmp->rank() == Mutex::native ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
               tmp->rank() <= tmp->next()->rank(), "mutex rank anomaly?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
bool Monitor::contains(Monitor* locks, Monitor * lock) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  for (; locks != NULL; locks = locks->next()) {
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   365
    if (locks == lock) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
      return true;
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   367
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
// Called immediately after lock acquisition or release as a diagnostic
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
// to track the lock-set of the thread and test for rank violations that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
// might indicate exposure to deadlock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
// Rather like an EventListener for _owner (:>).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
void Monitor::set_owner_implementation(Thread *new_owner) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
  // This function is solely responsible for maintaining
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
  // and checking the invariant that threads and locks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
  // are in a 1/N relation, with some some locks unowned.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
  // It uses the Mutex::_owner, Mutex::_next, and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
  // Thread::_owned_locks fields, and no other function
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
  // changes those fields.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
  // It is illegal to set the mutex from one non-NULL
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
  // owner to another--it must be owned by NULL as an
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
  // intermediate state.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
  if (new_owner != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
    // the thread is acquiring this lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
    assert(new_owner == Thread::current(), "Should I be doing this?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
    assert(_owner == NULL, "setting the owner thread of an already owned mutex");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
    _owner = new_owner; // set the owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
    // link "this" into the owned locks list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   398
#ifdef ASSERT  // Thread::_owned_locks is under the same ifdef
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   399
    Monitor* locks = get_least_ranked_lock(new_owner->owned_locks());
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   400
    // Mutex::set_owner_implementation is a friend of Thread
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   402
    assert(this->rank() >= 0, "bad lock rank");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   404
    // Deadlock avoidance rules require us to acquire Mutexes only in
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   405
    // a global total order. For example m1 is the lowest ranked mutex
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   406
    // that the thread holds and m2 is the mutex the thread is trying
53646
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   407
    // to acquire, then deadlock avoidance rules require that the rank
043ae846819f 8210832: Remove sneaky locking in class Monitor
pchilanomate
parents: 52913
diff changeset
   408
    // of m2 be less than the rank of m1.
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   409
    // The rank Mutex::native  is an exception in that it is not subject
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   410
    // to the verification rules.
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   411
    if (this->rank() != Mutex::native &&
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   412
        this->rank() != Mutex::suspend_resume &&
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   413
        locks != NULL && locks->rank() <= this->rank() &&
53775
5d20b085d893 8203469: Faster safepoints
rehn
parents: 53646
diff changeset
   414
        !SafepointSynchronize::is_at_safepoint()) {
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   415
      new_owner->print_owned_locks();
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
   416
      fatal("acquiring lock %s/%d out of order with lock %s/%d -- "
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
   417
            "possible deadlock", this->name(), this->rank(),
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
   418
            locks->name(), locks->rank());
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   419
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   421
    this->_next = new_owner->_owned_locks;
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   422
    new_owner->_owned_locks = this;
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   423
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
    // the thread is releasing this lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
    Thread* old_owner = _owner;
52555
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 52450
diff changeset
   429
    DEBUG_ONLY(_last_owner = old_owner;)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
    assert(old_owner != NULL, "removing the owner thread of an unowned mutex");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
    assert(old_owner == Thread::current(), "removing the owner thread of an unowned mutex");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
    _owner = NULL; // set the owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   436
#ifdef ASSERT
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   437
    Monitor *locks = old_owner->owned_locks();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   439
    // remove "this" from the owned locks list
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   441
    Monitor *prev = NULL;
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   442
    bool found = false;
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   443
    for (; locks != NULL; prev = locks, locks = locks->next()) {
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   444
      if (locks == this) {
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   445
        found = true;
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   446
        break;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
      }
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   448
    }
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   449
    assert(found, "Removing a lock not owned");
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   450
    if (prev == NULL) {
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   451
      old_owner->_owned_locks = _next;
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   452
    } else {
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   453
      prev->_next = _next;
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   454
    }
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   455
    _next = NULL;
26684
d1221849ea3d 8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 26683
diff changeset
   456
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
// Factored out common sanity checks for locking mutex'es. Used by lock() and try_lock()
50280
5aaf3a471172 8203817: Monitor::try_lock() should not call check_prelock_state()
pliden
parents: 50203
diff changeset
   462
void Monitor::check_prelock_state(Thread *thread, bool safepoint_check) {
5aaf3a471172 8203817: Monitor::try_lock() should not call check_prelock_state()
pliden
parents: 50203
diff changeset
   463
  if (safepoint_check) {
5aaf3a471172 8203817: Monitor::try_lock() should not call check_prelock_state()
pliden
parents: 50203
diff changeset
   464
    assert((!thread->is_Java_thread() || ((JavaThread *)thread)->thread_state() == _thread_in_vm)
5aaf3a471172 8203817: Monitor::try_lock() should not call check_prelock_state()
pliden
parents: 50203
diff changeset
   465
           || rank() == Mutex::special, "wrong thread state for using locks");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
    if (thread->is_VM_thread() && !allow_vm_block()) {
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 28163
diff changeset
   467
      fatal("VM thread using lock %s (not allowed to block on)", name());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
    }
52555
3b2d22602c16 8213708: Different #ifdef guards cause incorrect use of Monitor::check_block_state()
pchilanomate
parents: 52450
diff changeset
   469
    DEBUG_ONLY(if (rank() != Mutex::special) \
26683
a02753d5a0b2 8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents: 25472
diff changeset
   470
               thread->check_for_valid_safepoint_state(false);)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  }
46644
a5813fb66270 8183925: Decouple crash protection from watcher thread
rehn
parents: 41704
diff changeset
   472
  assert(!os::ThreadCrashProtection::is_crash_protected(thread),
a5813fb66270 8183925: Decouple crash protection from watcher thread
rehn
parents: 41704
diff changeset
   473
         "locking not allowed when crash protection is set");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
void Monitor::check_block_state(Thread *thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
  if (!_allow_vm_block && thread->is_VM_thread()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
    warning("VM thread blocked on lock");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
    print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
    BREAKPOINT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  assert(_owner != thread, "deadlock: blocking on monitor owned by current thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
#endif // PRODUCT