hotspot/src/share/vm/prims/jvmtiRawMonitor.cpp
author stefank
Mon, 01 Oct 2012 13:29:11 +0200
changeset 13922 ab7d352debe6
parent 13195 be27e1b6a4b9
child 13963 e5b53c306fb5
permissions -rw-r--r--
8000227: [obj|type]ArrayKlass::oop_print_on prints one line to tty instead of the provided output stream Reviewed-by: brutisso, sla, jmasa, coleenp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     1
/*
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     4
 *
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     8
 *
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    13
 * accompanied this code).
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    14
 *
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    18
 *
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    21
 * questions.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    22
 *
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    23
 */
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    26
#include "prims/jvmtiRawMonitor.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    27
#include "runtime/interfaceSupport.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    28
#include "runtime/thread.hpp"
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    29
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
    30
GrowableArray<JvmtiRawMonitor*> *JvmtiPendingMonitors::_monitors = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiRawMonitor*>(1,true);
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    31
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    32
void JvmtiPendingMonitors::transition_raw_monitors() {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    33
  assert((Threads::number_of_threads()==1),
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    34
         "Java thread has not created yet or more than one java thread \
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    35
is running. Raw monitor transition will not work");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    36
  JavaThread *current_java_thread = JavaThread::current();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    37
  assert(current_java_thread->thread_state() == _thread_in_vm, "Must be in vm");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    38
  {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    39
    ThreadBlockInVM __tbivm(current_java_thread);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    40
    for(int i=0; i< count(); i++) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    41
      JvmtiRawMonitor *rmonitor = monitors()->at(i);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    42
      int r = rmonitor->raw_enter(current_java_thread);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    43
      assert(r == ObjectMonitor::OM_OK, "raw_enter should have worked");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    44
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    45
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    46
  // pending monitors are converted to real monitor so delete them all.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    47
  dispose();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    48
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    49
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    50
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    51
// class JvmtiRawMonitor
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    52
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    53
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    54
JvmtiRawMonitor::JvmtiRawMonitor(const char *name) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    55
#ifdef ASSERT
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
    56
  _name = strcpy(NEW_C_HEAP_ARRAY(char, strlen(name) + 1, mtInternal), name);
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    57
#else
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    58
  _name = NULL;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    59
#endif
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    60
  _magic = JVMTI_RM_MAGIC;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    61
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    62
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    63
JvmtiRawMonitor::~JvmtiRawMonitor() {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    64
#ifdef ASSERT
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    65
  FreeHeap(_name);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    66
#endif
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    67
  _magic = 0;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    68
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    69
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    70
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    71
bool
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    72
JvmtiRawMonitor::is_valid() {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    73
  int value = 0;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    74
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    75
  // This object might not be a JvmtiRawMonitor so we can't assume
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    76
  // the _magic field is properly aligned. Get the value in a safe
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    77
  // way and then check against JVMTI_RM_MAGIC.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    78
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    79
  switch (sizeof(_magic)) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    80
  case 2:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    81
    value = Bytes::get_native_u2((address)&_magic);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    82
    break;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    83
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    84
  case 4:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    85
    value = Bytes::get_native_u4((address)&_magic);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    86
    break;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    87
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    88
  case 8:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    89
    value = Bytes::get_native_u8((address)&_magic);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    90
    break;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    91
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    92
  default:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    93
    guarantee(false, "_magic field is an unexpected size");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    94
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    95
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    96
  return value == JVMTI_RM_MAGIC;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    97
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    98
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    99
// -------------------------------------------------------------------------
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   100
// The raw monitor subsystem is entirely distinct from normal
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   101
// java-synchronization or jni-synchronization.  raw monitors are not
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   102
// associated with objects.  They can be implemented in any manner
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   103
// that makes sense.  The original implementors decided to piggy-back
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   104
// the raw-monitor implementation on the existing Java objectMonitor mechanism.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   105
// This flaw needs to fixed.  We should reimplement raw monitors as sui-generis.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   106
// Specifically, we should not implement raw monitors via java monitors.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   107
// Time permitting, we should disentangle and deconvolve the two implementations
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   108
// and move the resulting raw monitor implementation over to the JVMTI directories.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   109
// Ideally, the raw monitor implementation would be built on top of
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   110
// park-unpark and nothing else.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   111
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   112
// raw monitors are used mainly by JVMTI
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   113
// The raw monitor implementation borrows the ObjectMonitor structure,
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   114
// but the operators are degenerate and extremely simple.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   115
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   116
// Mixed use of a single objectMonitor instance -- as both a raw monitor
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   117
// and a normal java monitor -- is not permissible.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   118
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   119
// Note that we use the single RawMonitor_lock to protect queue operations for
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   120
// _all_ raw monitors.  This is a scalability impediment, but since raw monitor usage
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   121
// is deprecated and rare, this is not of concern.  The RawMonitor_lock can not
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   122
// be held indefinitely.  The critical sections must be short and bounded.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   123
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   124
// -------------------------------------------------------------------------
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   125
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   126
int JvmtiRawMonitor::SimpleEnter (Thread * Self) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   127
  for (;;) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   128
    if (Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   129
       return OS_OK ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   130
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   131
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   132
    ObjectWaiter Node (Self) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   133
    Self->_ParkEvent->reset() ;     // strictly optional
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   134
    Node.TState = ObjectWaiter::TS_ENTER ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   135
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   136
    RawMonitor_lock->lock_without_safepoint_check() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   137
    Node._next  = _EntryList ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   138
    _EntryList  = &Node ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   139
    OrderAccess::fence() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   140
    if (_owner == NULL && Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   141
        _EntryList = Node._next ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   142
        RawMonitor_lock->unlock() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   143
        return OS_OK ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   144
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   145
    RawMonitor_lock->unlock() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   146
    while (Node.TState == ObjectWaiter::TS_ENTER) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   147
       Self->_ParkEvent->park() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   148
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   149
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   150
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   151
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   152
int JvmtiRawMonitor::SimpleExit (Thread * Self) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   153
  guarantee (_owner == Self, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   154
  OrderAccess::release_store_ptr (&_owner, NULL) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   155
  OrderAccess::fence() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   156
  if (_EntryList == NULL) return OS_OK ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   157
  ObjectWaiter * w ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   158
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   159
  RawMonitor_lock->lock_without_safepoint_check() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   160
  w = _EntryList ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   161
  if (w != NULL) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   162
      _EntryList = w->_next ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   163
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   164
  RawMonitor_lock->unlock() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   165
  if (w != NULL) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   166
      guarantee (w ->TState == ObjectWaiter::TS_ENTER, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   167
      ParkEvent * ev = w->_event ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   168
      w->TState = ObjectWaiter::TS_RUN ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   169
      OrderAccess::fence() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   170
      ev->unpark() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   171
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   172
  return OS_OK ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   173
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   174
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   175
int JvmtiRawMonitor::SimpleWait (Thread * Self, jlong millis) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   176
  guarantee (_owner == Self  , "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   177
  guarantee (_recursions == 0, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   178
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   179
  ObjectWaiter Node (Self) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   180
  Node._notified = 0 ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   181
  Node.TState    = ObjectWaiter::TS_WAIT ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   182
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   183
  RawMonitor_lock->lock_without_safepoint_check() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   184
  Node._next     = _WaitSet ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   185
  _WaitSet       = &Node ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   186
  RawMonitor_lock->unlock() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   187
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   188
  SimpleExit (Self) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   189
  guarantee (_owner != Self, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   190
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   191
  int ret = OS_OK ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   192
  if (millis <= 0) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   193
    Self->_ParkEvent->park();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   194
  } else {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   195
    ret = Self->_ParkEvent->park(millis);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   196
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   197
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   198
  // If thread still resides on the waitset then unlink it.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   199
  // Double-checked locking -- the usage is safe in this context
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   200
  // as we TState is volatile and the lock-unlock operators are
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   201
  // serializing (barrier-equivalent).
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   202
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   203
  if (Node.TState == ObjectWaiter::TS_WAIT) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   204
    RawMonitor_lock->lock_without_safepoint_check() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   205
    if (Node.TState == ObjectWaiter::TS_WAIT) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   206
      // Simple O(n) unlink, but performance isn't critical here.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   207
      ObjectWaiter * p ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   208
      ObjectWaiter * q = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   209
      for (p = _WaitSet ; p != &Node; p = p->_next) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   210
         q = p ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   211
      }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   212
      guarantee (p == &Node, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   213
      if (q == NULL) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   214
        guarantee (p == _WaitSet, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   215
        _WaitSet = p->_next ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   216
      } else {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   217
        guarantee (p == q->_next, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   218
        q->_next = p->_next ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   219
      }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   220
      Node.TState = ObjectWaiter::TS_RUN ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   221
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   222
    RawMonitor_lock->unlock() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   223
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   224
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   225
  guarantee (Node.TState == ObjectWaiter::TS_RUN, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   226
  SimpleEnter (Self) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   227
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   228
  guarantee (_owner == Self, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   229
  guarantee (_recursions == 0, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   230
  return ret ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   231
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   232
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   233
int JvmtiRawMonitor::SimpleNotify (Thread * Self, bool All) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   234
  guarantee (_owner == Self, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   235
  if (_WaitSet == NULL) return OS_OK ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   236
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   237
  // We have two options:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   238
  // A. Transfer the threads from the WaitSet to the EntryList
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   239
  // B. Remove the thread from the WaitSet and unpark() it.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   240
  //
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   241
  // We use (B), which is crude and results in lots of futile
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   242
  // context switching.  In particular (B) induces lots of contention.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   243
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   244
  ParkEvent * ev = NULL ;       // consider using a small auto array ...
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   245
  RawMonitor_lock->lock_without_safepoint_check() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   246
  for (;;) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   247
      ObjectWaiter * w = _WaitSet ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   248
      if (w == NULL) break ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   249
      _WaitSet = w->_next ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   250
      if (ev != NULL) { ev->unpark(); ev = NULL; }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   251
      ev = w->_event ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   252
      OrderAccess::loadstore() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   253
      w->TState = ObjectWaiter::TS_RUN ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   254
      OrderAccess::storeload();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   255
      if (!All) break ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   256
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   257
  RawMonitor_lock->unlock() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   258
  if (ev != NULL) ev->unpark();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   259
  return OS_OK ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   260
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   261
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   262
// Any JavaThread will enter here with state _thread_blocked
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   263
int JvmtiRawMonitor::raw_enter(TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   264
  TEVENT (raw_enter) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   265
  void * Contended ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   266
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   267
  // don't enter raw monitor if thread is being externally suspended, it will
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   268
  // surprise the suspender if a "suspended" thread can still enter monitor
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   269
  JavaThread * jt = (JavaThread *)THREAD;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   270
  if (THREAD->is_Java_thread()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   271
    jt->SR_lock()->lock_without_safepoint_check();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   272
    while (jt->is_external_suspend()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   273
      jt->SR_lock()->unlock();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   274
      jt->java_suspend_self();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   275
      jt->SR_lock()->lock_without_safepoint_check();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   276
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   277
    // guarded by SR_lock to avoid racing with new external suspend requests.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   278
    Contended = Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   279
    jt->SR_lock()->unlock();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   280
  } else {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   281
    Contended = Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   282
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   283
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   284
  if (Contended == THREAD) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   285
     _recursions ++ ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   286
     return OM_OK ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   287
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   288
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   289
  if (Contended == NULL) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   290
     guarantee (_owner == THREAD, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   291
     guarantee (_recursions == 0, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   292
     return OM_OK ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   293
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   294
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   295
  THREAD->set_current_pending_monitor(this);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   296
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   297
  if (!THREAD->is_Java_thread()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   298
     // No other non-Java threads besides VM thread would acquire
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   299
     // a raw monitor.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   300
     assert(THREAD->is_VM_thread(), "must be VM thread");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   301
     SimpleEnter (THREAD) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   302
   } else {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   303
     guarantee (jt->thread_state() == _thread_blocked, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   304
     for (;;) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   305
       jt->set_suspend_equivalent();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   306
       // cleared by handle_special_suspend_equivalent_condition() or
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   307
       // java_suspend_self()
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   308
       SimpleEnter (THREAD) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   309
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   310
       // were we externally suspended while we were waiting?
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   311
       if (!jt->handle_special_suspend_equivalent_condition()) break ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   312
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   313
       // This thread was externally suspended
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   314
       //
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   315
       // This logic isn't needed for JVMTI raw monitors,
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   316
       // but doesn't hurt just in case the suspend rules change. This
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   317
           // logic is needed for the JvmtiRawMonitor.wait() reentry phase.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   318
           // We have reentered the contended monitor, but while we were
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   319
           // waiting another thread suspended us. We don't want to reenter
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   320
           // the monitor while suspended because that would surprise the
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   321
           // thread that suspended us.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   322
           //
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   323
           // Drop the lock -
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   324
       SimpleExit (THREAD) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   325
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   326
           jt->java_suspend_self();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   327
         }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   328
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   329
     assert(_owner == THREAD, "Fatal error with monitor owner!");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   330
     assert(_recursions == 0, "Fatal error with monitor recursions!");
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   331
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   332
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   333
  THREAD->set_current_pending_monitor(NULL);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   334
  guarantee (_recursions == 0, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   335
  return OM_OK;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   336
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   337
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   338
// Used mainly for JVMTI raw monitor implementation
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   339
// Also used for JvmtiRawMonitor::wait().
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   340
int JvmtiRawMonitor::raw_exit(TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   341
  TEVENT (raw_exit) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   342
  if (THREAD != _owner) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   343
    return OM_ILLEGAL_MONITOR_STATE;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   344
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   345
  if (_recursions > 0) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   346
    --_recursions ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   347
    return OM_OK ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   348
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   349
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   350
  void * List = _EntryList ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   351
  SimpleExit (THREAD) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   352
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   353
  return OM_OK;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   354
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   355
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   356
// Used for JVMTI raw monitor implementation.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   357
// All JavaThreads will enter here with state _thread_blocked
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   358
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   359
int JvmtiRawMonitor::raw_wait(jlong millis, bool interruptible, TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   360
  TEVENT (raw_wait) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   361
  if (THREAD != _owner) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   362
    return OM_ILLEGAL_MONITOR_STATE;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   363
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   364
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   365
  // To avoid spurious wakeups we reset the parkevent -- This is strictly optional.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   366
  // The caller must be able to tolerate spurious returns from raw_wait().
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   367
  THREAD->_ParkEvent->reset() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   368
  OrderAccess::fence() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   369
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   370
  // check interrupt event
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   371
  if (interruptible && Thread::is_interrupted(THREAD, true)) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   372
    return OM_INTERRUPTED;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   373
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   374
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   375
  intptr_t save = _recursions ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   376
  _recursions = 0 ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   377
  _waiters ++ ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   378
  if (THREAD->is_Java_thread()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   379
    guarantee (((JavaThread *) THREAD)->thread_state() == _thread_blocked, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   380
    ((JavaThread *)THREAD)->set_suspend_equivalent();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   381
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   382
  int rv = SimpleWait (THREAD, millis) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   383
  _recursions = save ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   384
  _waiters -- ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   385
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   386
  guarantee (THREAD == _owner, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   387
  if (THREAD->is_Java_thread()) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   388
     JavaThread * jSelf = (JavaThread *) THREAD ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   389
     for (;;) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   390
        if (!jSelf->handle_special_suspend_equivalent_condition()) break ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   391
        SimpleExit (THREAD) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   392
        jSelf->java_suspend_self();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   393
        SimpleEnter (THREAD) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   394
        jSelf->set_suspend_equivalent() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   395
     }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   396
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   397
  guarantee (THREAD == _owner, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   398
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   399
  if (interruptible && Thread::is_interrupted(THREAD, true)) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   400
    return OM_INTERRUPTED;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   401
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   402
  return OM_OK ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   403
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   404
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   405
int JvmtiRawMonitor::raw_notify(TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   406
  TEVENT (raw_notify) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   407
  if (THREAD != _owner) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   408
    return OM_ILLEGAL_MONITOR_STATE;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   409
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   410
  SimpleNotify (THREAD, false) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   411
  return OM_OK;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   412
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   413
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   414
int JvmtiRawMonitor::raw_notifyAll(TRAPS) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   415
  TEVENT (raw_notifyAll) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   416
  if (THREAD != _owner) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   417
    return OM_ILLEGAL_MONITOR_STATE;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   418
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   419
  SimpleNotify (THREAD, true) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   420
  return OM_OK;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   421
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   422