hotspot/src/share/vm/runtime/park.hpp
author mgerdin
Thu, 23 Feb 2012 14:58:35 +0100
changeset 12095 cc3d6f08a4c4
parent 7397 5b173b4ca846
child 19696 bd5a0131bde1
permissions -rw-r--r--
7148152: Add whitebox testing API to HotSpot Summary: Add an internal testing API to HotSpot to enable more targeted testing of vm functionality Reviewed-by: phh, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     1
/*
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
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
 */
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    24
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    25
#ifndef SHARE_VM_RUNTIME_PARK_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    26
#define SHARE_VM_RUNTIME_PARK_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    28
#include "utilities/debug.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    29
#include "utilities/globalDefinitions.hpp"
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    30
/*
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    31
 * Per-thread blocking support for JSR166. See the Java-level
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    32
 * Documentation for rationale. Basically, park acts like wait, unpark
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    33
 * like notify.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    34
 *
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    35
 * 6271289 --
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    36
 * To avoid errors where an os thread expires but the JavaThread still
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    37
 * exists, Parkers are immortal (type-stable) and are recycled across
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    38
 * new threads.  This parallels the ParkEvent implementation.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    39
 * Because park-unpark allow spurious wakeups it is harmless if an
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    40
 * unpark call unparks a new thread using the old Parker reference.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    41
 *
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    42
 * In the future we'll want to think about eliminating Parker and using
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    43
 * ParkEvent instead.  There's considerable duplication between the two
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    44
 * services.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    45
 *
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    46
 */
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    47
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    48
class Parker : public os::PlatformParker {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    49
private:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    50
  volatile int _counter ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    51
  Parker * FreeNext ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    52
  JavaThread * AssociatedWith ; // Current association
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    53
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    54
public:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    55
  Parker() : PlatformParker() {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    56
    _counter       = 0 ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    57
    FreeNext       = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    58
    AssociatedWith = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    59
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    60
protected:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    61
  ~Parker() { ShouldNotReachHere(); }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    62
public:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    63
  // For simplicity of interface with Java, all forms of park (indefinite,
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    64
  // relative, and absolute) are multiplexed into one call.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    65
  void park(bool isAbsolute, jlong time);
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    66
  void unpark();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    67
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    68
  // Lifecycle operators
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    69
  static Parker * Allocate (JavaThread * t) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    70
  static void Release (Parker * e) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    71
private:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    72
  static Parker * volatile FreeList ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    73
  static volatile int ListLock ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    74
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    75
};
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    76
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    77
/////////////////////////////////////////////////////////////
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    78
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    79
// ParkEvents are type-stable and immortal.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    80
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    81
// Lifecycle: Once a ParkEvent is associated with a thread that ParkEvent remains
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    82
// associated with the thread for the thread's entire lifetime - the relationship is
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    83
// stable. A thread will be associated at most one ParkEvent.  When the thread
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    84
// expires, the ParkEvent moves to the EventFreeList.  New threads attempt to allocate from
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    85
// the EventFreeList before creating a new Event.  Type-stability frees us from
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    86
// worrying about stale Event or Thread references in the objectMonitor subsystem.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    87
// (A reference to ParkEvent is always valid, even though the event may no longer be associated
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    88
// with the desired or expected thread.  A key aspect of this design is that the callers of
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    89
// park, unpark, etc must tolerate stale references and spurious wakeups).
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    90
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    91
// Only the "associated" thread can block (park) on the ParkEvent, although
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    92
// any other thread can unpark a reachable parkevent.  Park() is allowed to
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    93
// return spuriously.  In fact park-unpark a really just an optimization to
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    94
// avoid unbounded spinning and surrender the CPU to be a polite system citizen.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    95
// A degenerate albeit "impolite" park-unpark implementation could simply return.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    96
// See http://blogs.sun.com/dave for more details.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    97
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    98
// Eventually I'd like to eliminate Events and ObjectWaiters, both of which serve as
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    99
// thread proxies, and simply make the THREAD structure type-stable and persistent.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   100
// Currently, we unpark events associated with threads, but ideally we'd just
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   101
// unpark threads.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   102
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   103
// The base-class, PlatformEvent, is platform-specific while the ParkEvent is
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   104
// platform-independent.  PlatformEvent provides park(), unpark(), etc., and
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   105
// is abstract -- that is, a PlatformEvent should never be instantiated except
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   106
// as part of a ParkEvent.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   107
// Equivalently we could have defined a platform-independent base-class that
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   108
// exported Allocate(), Release(), etc.  The platform-specific class would extend
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   109
// that base-class, adding park(), unpark(), etc.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   110
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   111
// A word of caution: The JVM uses 2 very similar constructs:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   112
// 1. ParkEvent are used for Java-level "monitor" synchronization.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   113
// 2. Parkers are used by JSR166-JUC park-unpark.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   114
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   115
// We'll want to eventually merge these redundant facilities and use ParkEvent.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   116
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   117
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   118
class ParkEvent : public os::PlatformEvent {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   119
  private:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   120
    ParkEvent * FreeNext ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   121
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   122
    // Current association
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   123
    Thread * AssociatedWith ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   124
    intptr_t RawThreadIdentity ;        // LWPID etc
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   125
    volatile int Incarnation ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   126
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   127
    // diagnostic : keep track of last thread to wake this thread.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   128
    // this is useful for construction of dependency graphs.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   129
    void * LastWaker ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   130
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   131
  public:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   132
    // MCS-CLH list linkage and Native Mutex/Monitor
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   133
    ParkEvent * volatile ListNext ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   134
    ParkEvent * volatile ListPrev ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   135
    volatile intptr_t OnList ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   136
    volatile int TState ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   137
    volatile int Notified ;             // for native monitor construct
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   138
    volatile int IsWaiting ;            // Enqueued on WaitSet
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   139
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   140
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   141
  private:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   142
    static ParkEvent * volatile FreeList ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   143
    static volatile int ListLock ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   144
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   145
    // It's prudent to mark the dtor as "private"
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   146
    // ensuring that it's not visible outside the package.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   147
    // Unfortunately gcc warns about such usage, so
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   148
    // we revert to the less desirable "protected" visibility.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   149
    // The other compilers accept private dtors.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   150
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   151
  protected:        // Ensure dtor is never invoked
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   152
    ~ParkEvent() { guarantee (0, "invariant") ; }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   153
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   154
    ParkEvent() : PlatformEvent() {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   155
       AssociatedWith = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   156
       FreeNext       = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   157
       ListNext       = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   158
       ListPrev       = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   159
       OnList         = 0 ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   160
       TState         = 0 ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   161
       Notified       = 0 ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   162
       IsWaiting      = 0 ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   163
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   164
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   165
    // We use placement-new to force ParkEvent instances to be
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   166
    // aligned on 256-byte address boundaries.  This ensures that the least
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   167
    // significant byte of a ParkEvent address is always 0.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   168
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   169
    void * operator new (size_t sz) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   170
    void operator delete (void * a) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   171
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   172
  public:
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   173
    static ParkEvent * Allocate (Thread * t) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   174
    static void Release (ParkEvent * e) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   175
} ;
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
   176
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
   177
#endif // SHARE_VM_RUNTIME_PARK_HPP