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

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

mercurial