aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_RUNTIME_PARK_HPP aoqi@0: #define SHARE_VM_RUNTIME_PARK_HPP aoqi@0: aoqi@0: #include "utilities/debug.hpp" aoqi@0: #include "utilities/globalDefinitions.hpp" aoqi@0: /* aoqi@0: * Per-thread blocking support for JSR166. See the Java-level aoqi@0: * Documentation for rationale. Basically, park acts like wait, unpark aoqi@0: * like notify. aoqi@0: * aoqi@0: * 6271289 -- aoqi@0: * To avoid errors where an os thread expires but the JavaThread still aoqi@0: * exists, Parkers are immortal (type-stable) and are recycled across aoqi@0: * new threads. This parallels the ParkEvent implementation. aoqi@0: * Because park-unpark allow spurious wakeups it is harmless if an aoqi@0: * unpark call unparks a new thread using the old Parker reference. aoqi@0: * aoqi@0: * In the future we'll want to think about eliminating Parker and using aoqi@0: * ParkEvent instead. There's considerable duplication between the two aoqi@0: * services. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: class Parker : public os::PlatformParker { aoqi@0: private: aoqi@0: volatile int _counter ; aoqi@0: Parker * FreeNext ; aoqi@0: JavaThread * AssociatedWith ; // Current association aoqi@0: aoqi@0: public: aoqi@0: Parker() : PlatformParker() { aoqi@0: _counter = 0 ; aoqi@0: FreeNext = NULL ; aoqi@0: AssociatedWith = NULL ; aoqi@0: } aoqi@0: protected: aoqi@0: ~Parker() { ShouldNotReachHere(); } aoqi@0: public: aoqi@0: // For simplicity of interface with Java, all forms of park (indefinite, aoqi@0: // relative, and absolute) are multiplexed into one call. aoqi@0: void park(bool isAbsolute, jlong time); aoqi@0: void unpark(); aoqi@0: aoqi@0: // Lifecycle operators aoqi@0: static Parker * Allocate (JavaThread * t) ; aoqi@0: static void Release (Parker * e) ; aoqi@0: private: aoqi@0: static Parker * volatile FreeList ; aoqi@0: static volatile int ListLock ; aoqi@0: aoqi@0: }; aoqi@0: aoqi@0: ///////////////////////////////////////////////////////////// aoqi@0: // aoqi@0: // ParkEvents are type-stable and immortal. aoqi@0: // aoqi@0: // Lifecycle: Once a ParkEvent is associated with a thread that ParkEvent remains aoqi@0: // associated with the thread for the thread's entire lifetime - the relationship is aoqi@0: // stable. A thread will be associated at most one ParkEvent. When the thread aoqi@0: // expires, the ParkEvent moves to the EventFreeList. New threads attempt to allocate from aoqi@0: // the EventFreeList before creating a new Event. Type-stability frees us from aoqi@0: // worrying about stale Event or Thread references in the objectMonitor subsystem. aoqi@0: // (A reference to ParkEvent is always valid, even though the event may no longer be associated aoqi@0: // with the desired or expected thread. A key aspect of this design is that the callers of aoqi@0: // park, unpark, etc must tolerate stale references and spurious wakeups). aoqi@0: // aoqi@0: // Only the "associated" thread can block (park) on the ParkEvent, although aoqi@0: // any other thread can unpark a reachable parkevent. Park() is allowed to aoqi@0: // return spuriously. In fact park-unpark a really just an optimization to aoqi@0: // avoid unbounded spinning and surrender the CPU to be a polite system citizen. aoqi@0: // A degenerate albeit "impolite" park-unpark implementation could simply return. aoqi@0: // See http://blogs.sun.com/dave for more details. aoqi@0: // aoqi@0: // Eventually I'd like to eliminate Events and ObjectWaiters, both of which serve as aoqi@0: // thread proxies, and simply make the THREAD structure type-stable and persistent. aoqi@0: // Currently, we unpark events associated with threads, but ideally we'd just aoqi@0: // unpark threads. aoqi@0: // aoqi@0: // The base-class, PlatformEvent, is platform-specific while the ParkEvent is aoqi@0: // platform-independent. PlatformEvent provides park(), unpark(), etc., and aoqi@0: // is abstract -- that is, a PlatformEvent should never be instantiated except aoqi@0: // as part of a ParkEvent. aoqi@0: // Equivalently we could have defined a platform-independent base-class that aoqi@0: // exported Allocate(), Release(), etc. The platform-specific class would extend aoqi@0: // that base-class, adding park(), unpark(), etc. aoqi@0: // aoqi@0: // A word of caution: The JVM uses 2 very similar constructs: aoqi@0: // 1. ParkEvent are used for Java-level "monitor" synchronization. aoqi@0: // 2. Parkers are used by JSR166-JUC park-unpark. aoqi@0: // aoqi@0: // We'll want to eventually merge these redundant facilities and use ParkEvent. aoqi@0: aoqi@0: aoqi@0: class ParkEvent : public os::PlatformEvent { aoqi@0: private: aoqi@0: ParkEvent * FreeNext ; aoqi@0: aoqi@0: // Current association aoqi@0: Thread * AssociatedWith ; aoqi@0: intptr_t RawThreadIdentity ; // LWPID etc aoqi@0: volatile int Incarnation ; aoqi@0: aoqi@0: // diagnostic : keep track of last thread to wake this thread. aoqi@0: // this is useful for construction of dependency graphs. aoqi@0: void * LastWaker ; aoqi@0: aoqi@0: public: aoqi@0: // MCS-CLH list linkage and Native Mutex/Monitor aoqi@0: ParkEvent * volatile ListNext ; aoqi@0: ParkEvent * volatile ListPrev ; aoqi@0: volatile intptr_t OnList ; aoqi@0: volatile int TState ; aoqi@0: volatile int Notified ; // for native monitor construct aoqi@0: volatile int IsWaiting ; // Enqueued on WaitSet aoqi@0: aoqi@0: aoqi@0: private: aoqi@0: static ParkEvent * volatile FreeList ; aoqi@0: static volatile int ListLock ; aoqi@0: aoqi@0: // It's prudent to mark the dtor as "private" aoqi@0: // ensuring that it's not visible outside the package. aoqi@0: // Unfortunately gcc warns about such usage, so aoqi@0: // we revert to the less desirable "protected" visibility. aoqi@0: // The other compilers accept private dtors. aoqi@0: aoqi@0: protected: // Ensure dtor is never invoked aoqi@0: ~ParkEvent() { guarantee (0, "invariant") ; } aoqi@0: aoqi@0: ParkEvent() : PlatformEvent() { aoqi@0: AssociatedWith = NULL ; aoqi@0: FreeNext = NULL ; aoqi@0: ListNext = NULL ; aoqi@0: ListPrev = NULL ; aoqi@0: OnList = 0 ; aoqi@0: TState = 0 ; aoqi@0: Notified = 0 ; aoqi@0: IsWaiting = 0 ; aoqi@0: } aoqi@0: aoqi@0: // We use placement-new to force ParkEvent instances to be aoqi@0: // aligned on 256-byte address boundaries. This ensures that the least aoqi@0: // significant byte of a ParkEvent address is always 0. aoqi@0: aoqi@0: void * operator new (size_t sz) throw(); aoqi@0: void operator delete (void * a) ; aoqi@0: aoqi@0: public: aoqi@0: static ParkEvent * Allocate (Thread * t) ; aoqi@0: static void Release (ParkEvent * e) ; aoqi@0: } ; aoqi@0: aoqi@0: #endif // SHARE_VM_RUNTIME_PARK_HPP