src/share/vm/runtime/park.hpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/runtime/park.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,177 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef SHARE_VM_RUNTIME_PARK_HPP
    1.29 +#define SHARE_VM_RUNTIME_PARK_HPP
    1.30 +
    1.31 +#include "utilities/debug.hpp"
    1.32 +#include "utilities/globalDefinitions.hpp"
    1.33 +/*
    1.34 + * Per-thread blocking support for JSR166. See the Java-level
    1.35 + * Documentation for rationale. Basically, park acts like wait, unpark
    1.36 + * like notify.
    1.37 + *
    1.38 + * 6271289 --
    1.39 + * To avoid errors where an os thread expires but the JavaThread still
    1.40 + * exists, Parkers are immortal (type-stable) and are recycled across
    1.41 + * new threads.  This parallels the ParkEvent implementation.
    1.42 + * Because park-unpark allow spurious wakeups it is harmless if an
    1.43 + * unpark call unparks a new thread using the old Parker reference.
    1.44 + *
    1.45 + * In the future we'll want to think about eliminating Parker and using
    1.46 + * ParkEvent instead.  There's considerable duplication between the two
    1.47 + * services.
    1.48 + *
    1.49 + */
    1.50 +
    1.51 +class Parker : public os::PlatformParker {
    1.52 +private:
    1.53 +  volatile int _counter ;
    1.54 +  Parker * FreeNext ;
    1.55 +  JavaThread * AssociatedWith ; // Current association
    1.56 +
    1.57 +public:
    1.58 +  Parker() : PlatformParker() {
    1.59 +    _counter       = 0 ;
    1.60 +    FreeNext       = NULL ;
    1.61 +    AssociatedWith = NULL ;
    1.62 +  }
    1.63 +protected:
    1.64 +  ~Parker() { ShouldNotReachHere(); }
    1.65 +public:
    1.66 +  // For simplicity of interface with Java, all forms of park (indefinite,
    1.67 +  // relative, and absolute) are multiplexed into one call.
    1.68 +  void park(bool isAbsolute, jlong time);
    1.69 +  void unpark();
    1.70 +
    1.71 +  // Lifecycle operators
    1.72 +  static Parker * Allocate (JavaThread * t) ;
    1.73 +  static void Release (Parker * e) ;
    1.74 +private:
    1.75 +  static Parker * volatile FreeList ;
    1.76 +  static volatile int ListLock ;
    1.77 +
    1.78 +};
    1.79 +
    1.80 +/////////////////////////////////////////////////////////////
    1.81 +//
    1.82 +// ParkEvents are type-stable and immortal.
    1.83 +//
    1.84 +// Lifecycle: Once a ParkEvent is associated with a thread that ParkEvent remains
    1.85 +// associated with the thread for the thread's entire lifetime - the relationship is
    1.86 +// stable. A thread will be associated at most one ParkEvent.  When the thread
    1.87 +// expires, the ParkEvent moves to the EventFreeList.  New threads attempt to allocate from
    1.88 +// the EventFreeList before creating a new Event.  Type-stability frees us from
    1.89 +// worrying about stale Event or Thread references in the objectMonitor subsystem.
    1.90 +// (A reference to ParkEvent is always valid, even though the event may no longer be associated
    1.91 +// with the desired or expected thread.  A key aspect of this design is that the callers of
    1.92 +// park, unpark, etc must tolerate stale references and spurious wakeups).
    1.93 +//
    1.94 +// Only the "associated" thread can block (park) on the ParkEvent, although
    1.95 +// any other thread can unpark a reachable parkevent.  Park() is allowed to
    1.96 +// return spuriously.  In fact park-unpark a really just an optimization to
    1.97 +// avoid unbounded spinning and surrender the CPU to be a polite system citizen.
    1.98 +// A degenerate albeit "impolite" park-unpark implementation could simply return.
    1.99 +// See http://blogs.sun.com/dave for more details.
   1.100 +//
   1.101 +// Eventually I'd like to eliminate Events and ObjectWaiters, both of which serve as
   1.102 +// thread proxies, and simply make the THREAD structure type-stable and persistent.
   1.103 +// Currently, we unpark events associated with threads, but ideally we'd just
   1.104 +// unpark threads.
   1.105 +//
   1.106 +// The base-class, PlatformEvent, is platform-specific while the ParkEvent is
   1.107 +// platform-independent.  PlatformEvent provides park(), unpark(), etc., and
   1.108 +// is abstract -- that is, a PlatformEvent should never be instantiated except
   1.109 +// as part of a ParkEvent.
   1.110 +// Equivalently we could have defined a platform-independent base-class that
   1.111 +// exported Allocate(), Release(), etc.  The platform-specific class would extend
   1.112 +// that base-class, adding park(), unpark(), etc.
   1.113 +//
   1.114 +// A word of caution: The JVM uses 2 very similar constructs:
   1.115 +// 1. ParkEvent are used for Java-level "monitor" synchronization.
   1.116 +// 2. Parkers are used by JSR166-JUC park-unpark.
   1.117 +//
   1.118 +// We'll want to eventually merge these redundant facilities and use ParkEvent.
   1.119 +
   1.120 +
   1.121 +class ParkEvent : public os::PlatformEvent {
   1.122 +  private:
   1.123 +    ParkEvent * FreeNext ;
   1.124 +
   1.125 +    // Current association
   1.126 +    Thread * AssociatedWith ;
   1.127 +    intptr_t RawThreadIdentity ;        // LWPID etc
   1.128 +    volatile int Incarnation ;
   1.129 +
   1.130 +    // diagnostic : keep track of last thread to wake this thread.
   1.131 +    // this is useful for construction of dependency graphs.
   1.132 +    void * LastWaker ;
   1.133 +
   1.134 +  public:
   1.135 +    // MCS-CLH list linkage and Native Mutex/Monitor
   1.136 +    ParkEvent * volatile ListNext ;
   1.137 +    ParkEvent * volatile ListPrev ;
   1.138 +    volatile intptr_t OnList ;
   1.139 +    volatile int TState ;
   1.140 +    volatile int Notified ;             // for native monitor construct
   1.141 +    volatile int IsWaiting ;            // Enqueued on WaitSet
   1.142 +
   1.143 +
   1.144 +  private:
   1.145 +    static ParkEvent * volatile FreeList ;
   1.146 +    static volatile int ListLock ;
   1.147 +
   1.148 +    // It's prudent to mark the dtor as "private"
   1.149 +    // ensuring that it's not visible outside the package.
   1.150 +    // Unfortunately gcc warns about such usage, so
   1.151 +    // we revert to the less desirable "protected" visibility.
   1.152 +    // The other compilers accept private dtors.
   1.153 +
   1.154 +  protected:        // Ensure dtor is never invoked
   1.155 +    ~ParkEvent() { guarantee (0, "invariant") ; }
   1.156 +
   1.157 +    ParkEvent() : PlatformEvent() {
   1.158 +       AssociatedWith = NULL ;
   1.159 +       FreeNext       = NULL ;
   1.160 +       ListNext       = NULL ;
   1.161 +       ListPrev       = NULL ;
   1.162 +       OnList         = 0 ;
   1.163 +       TState         = 0 ;
   1.164 +       Notified       = 0 ;
   1.165 +       IsWaiting      = 0 ;
   1.166 +    }
   1.167 +
   1.168 +    // We use placement-new to force ParkEvent instances to be
   1.169 +    // aligned on 256-byte address boundaries.  This ensures that the least
   1.170 +    // significant byte of a ParkEvent address is always 0.
   1.171 +
   1.172 +    void * operator new (size_t sz) throw();
   1.173 +    void operator delete (void * a) ;
   1.174 +
   1.175 +  public:
   1.176 +    static ParkEvent * Allocate (Thread * t) ;
   1.177 +    static void Release (ParkEvent * e) ;
   1.178 +} ;
   1.179 +
   1.180 +#endif // SHARE_VM_RUNTIME_PARK_HPP

mercurial