src/share/vm/runtime/park.hpp

Fri, 22 Oct 2010 15:59:34 -0400

author
acorn
date
Fri, 22 Oct 2010 15:59:34 -0400
changeset 2233
fa83ab460c54
child 2314
f95d63e2154a
permissions
-rw-r--r--

6988353: refactor contended sync subsystem
Summary: reduce complexity by factoring synchronizer.cpp
Reviewed-by: dholmes, never, coleenp

acorn@2233 1 /*
acorn@2233 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
acorn@2233 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
acorn@2233 4 *
acorn@2233 5 * This code is free software; you can redistribute it and/or modify it
acorn@2233 6 * under the terms of the GNU General Public License version 2 only, as
acorn@2233 7 * published by the Free Software Foundation.
acorn@2233 8 *
acorn@2233 9 * This code is distributed in the hope that it will be useful, but WITHOUT
acorn@2233 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
acorn@2233 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
acorn@2233 12 * version 2 for more details (a copy is included in the LICENSE file that
acorn@2233 13 * accompanied this code).
acorn@2233 14 *
acorn@2233 15 * You should have received a copy of the GNU General Public License version
acorn@2233 16 * 2 along with this work; if not, write to the Free Software Foundation,
acorn@2233 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
acorn@2233 18 *
acorn@2233 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
acorn@2233 20 * or visit www.oracle.com if you need additional information or have any
acorn@2233 21 * questions.
acorn@2233 22 *
acorn@2233 23 */
acorn@2233 24 /*
acorn@2233 25 * Per-thread blocking support for JSR166. See the Java-level
acorn@2233 26 * Documentation for rationale. Basically, park acts like wait, unpark
acorn@2233 27 * like notify.
acorn@2233 28 *
acorn@2233 29 * 6271289 --
acorn@2233 30 * To avoid errors where an os thread expires but the JavaThread still
acorn@2233 31 * exists, Parkers are immortal (type-stable) and are recycled across
acorn@2233 32 * new threads. This parallels the ParkEvent implementation.
acorn@2233 33 * Because park-unpark allow spurious wakeups it is harmless if an
acorn@2233 34 * unpark call unparks a new thread using the old Parker reference.
acorn@2233 35 *
acorn@2233 36 * In the future we'll want to think about eliminating Parker and using
acorn@2233 37 * ParkEvent instead. There's considerable duplication between the two
acorn@2233 38 * services.
acorn@2233 39 *
acorn@2233 40 */
acorn@2233 41
acorn@2233 42 class Parker : public os::PlatformParker {
acorn@2233 43 private:
acorn@2233 44 volatile int _counter ;
acorn@2233 45 Parker * FreeNext ;
acorn@2233 46 JavaThread * AssociatedWith ; // Current association
acorn@2233 47
acorn@2233 48 public:
acorn@2233 49 Parker() : PlatformParker() {
acorn@2233 50 _counter = 0 ;
acorn@2233 51 FreeNext = NULL ;
acorn@2233 52 AssociatedWith = NULL ;
acorn@2233 53 }
acorn@2233 54 protected:
acorn@2233 55 ~Parker() { ShouldNotReachHere(); }
acorn@2233 56 public:
acorn@2233 57 // For simplicity of interface with Java, all forms of park (indefinite,
acorn@2233 58 // relative, and absolute) are multiplexed into one call.
acorn@2233 59 void park(bool isAbsolute, jlong time);
acorn@2233 60 void unpark();
acorn@2233 61
acorn@2233 62 // Lifecycle operators
acorn@2233 63 static Parker * Allocate (JavaThread * t) ;
acorn@2233 64 static void Release (Parker * e) ;
acorn@2233 65 private:
acorn@2233 66 static Parker * volatile FreeList ;
acorn@2233 67 static volatile int ListLock ;
acorn@2233 68
acorn@2233 69 };
acorn@2233 70
acorn@2233 71 /////////////////////////////////////////////////////////////
acorn@2233 72 //
acorn@2233 73 // ParkEvents are type-stable and immortal.
acorn@2233 74 //
acorn@2233 75 // Lifecycle: Once a ParkEvent is associated with a thread that ParkEvent remains
acorn@2233 76 // associated with the thread for the thread's entire lifetime - the relationship is
acorn@2233 77 // stable. A thread will be associated at most one ParkEvent. When the thread
acorn@2233 78 // expires, the ParkEvent moves to the EventFreeList. New threads attempt to allocate from
acorn@2233 79 // the EventFreeList before creating a new Event. Type-stability frees us from
acorn@2233 80 // worrying about stale Event or Thread references in the objectMonitor subsystem.
acorn@2233 81 // (A reference to ParkEvent is always valid, even though the event may no longer be associated
acorn@2233 82 // with the desired or expected thread. A key aspect of this design is that the callers of
acorn@2233 83 // park, unpark, etc must tolerate stale references and spurious wakeups).
acorn@2233 84 //
acorn@2233 85 // Only the "associated" thread can block (park) on the ParkEvent, although
acorn@2233 86 // any other thread can unpark a reachable parkevent. Park() is allowed to
acorn@2233 87 // return spuriously. In fact park-unpark a really just an optimization to
acorn@2233 88 // avoid unbounded spinning and surrender the CPU to be a polite system citizen.
acorn@2233 89 // A degenerate albeit "impolite" park-unpark implementation could simply return.
acorn@2233 90 // See http://blogs.sun.com/dave for more details.
acorn@2233 91 //
acorn@2233 92 // Eventually I'd like to eliminate Events and ObjectWaiters, both of which serve as
acorn@2233 93 // thread proxies, and simply make the THREAD structure type-stable and persistent.
acorn@2233 94 // Currently, we unpark events associated with threads, but ideally we'd just
acorn@2233 95 // unpark threads.
acorn@2233 96 //
acorn@2233 97 // The base-class, PlatformEvent, is platform-specific while the ParkEvent is
acorn@2233 98 // platform-independent. PlatformEvent provides park(), unpark(), etc., and
acorn@2233 99 // is abstract -- that is, a PlatformEvent should never be instantiated except
acorn@2233 100 // as part of a ParkEvent.
acorn@2233 101 // Equivalently we could have defined a platform-independent base-class that
acorn@2233 102 // exported Allocate(), Release(), etc. The platform-specific class would extend
acorn@2233 103 // that base-class, adding park(), unpark(), etc.
acorn@2233 104 //
acorn@2233 105 // A word of caution: The JVM uses 2 very similar constructs:
acorn@2233 106 // 1. ParkEvent are used for Java-level "monitor" synchronization.
acorn@2233 107 // 2. Parkers are used by JSR166-JUC park-unpark.
acorn@2233 108 //
acorn@2233 109 // We'll want to eventually merge these redundant facilities and use ParkEvent.
acorn@2233 110
acorn@2233 111
acorn@2233 112 class ParkEvent : public os::PlatformEvent {
acorn@2233 113 private:
acorn@2233 114 ParkEvent * FreeNext ;
acorn@2233 115
acorn@2233 116 // Current association
acorn@2233 117 Thread * AssociatedWith ;
acorn@2233 118 intptr_t RawThreadIdentity ; // LWPID etc
acorn@2233 119 volatile int Incarnation ;
acorn@2233 120
acorn@2233 121 // diagnostic : keep track of last thread to wake this thread.
acorn@2233 122 // this is useful for construction of dependency graphs.
acorn@2233 123 void * LastWaker ;
acorn@2233 124
acorn@2233 125 public:
acorn@2233 126 // MCS-CLH list linkage and Native Mutex/Monitor
acorn@2233 127 ParkEvent * volatile ListNext ;
acorn@2233 128 ParkEvent * volatile ListPrev ;
acorn@2233 129 volatile intptr_t OnList ;
acorn@2233 130 volatile int TState ;
acorn@2233 131 volatile int Notified ; // for native monitor construct
acorn@2233 132 volatile int IsWaiting ; // Enqueued on WaitSet
acorn@2233 133
acorn@2233 134
acorn@2233 135 private:
acorn@2233 136 static ParkEvent * volatile FreeList ;
acorn@2233 137 static volatile int ListLock ;
acorn@2233 138
acorn@2233 139 // It's prudent to mark the dtor as "private"
acorn@2233 140 // ensuring that it's not visible outside the package.
acorn@2233 141 // Unfortunately gcc warns about such usage, so
acorn@2233 142 // we revert to the less desirable "protected" visibility.
acorn@2233 143 // The other compilers accept private dtors.
acorn@2233 144
acorn@2233 145 protected: // Ensure dtor is never invoked
acorn@2233 146 ~ParkEvent() { guarantee (0, "invariant") ; }
acorn@2233 147
acorn@2233 148 ParkEvent() : PlatformEvent() {
acorn@2233 149 AssociatedWith = NULL ;
acorn@2233 150 FreeNext = NULL ;
acorn@2233 151 ListNext = NULL ;
acorn@2233 152 ListPrev = NULL ;
acorn@2233 153 OnList = 0 ;
acorn@2233 154 TState = 0 ;
acorn@2233 155 Notified = 0 ;
acorn@2233 156 IsWaiting = 0 ;
acorn@2233 157 }
acorn@2233 158
acorn@2233 159 // We use placement-new to force ParkEvent instances to be
acorn@2233 160 // aligned on 256-byte address boundaries. This ensures that the least
acorn@2233 161 // significant byte of a ParkEvent address is always 0.
acorn@2233 162
acorn@2233 163 void * operator new (size_t sz) ;
acorn@2233 164 void operator delete (void * a) ;
acorn@2233 165
acorn@2233 166 public:
acorn@2233 167 static ParkEvent * Allocate (Thread * t) ;
acorn@2233 168 static void Release (ParkEvent * e) ;
acorn@2233 169 } ;

mercurial