thread.hpp revision 1472
1472N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 0N/A // Exception handling 0N/A // (Note: _pending_exception and friends are in ThreadShadow) 0N/A //oop _pending_exception; // pending exception for current thread 0N/A // const char* _exception_file; // file information for exception (debugging only) 0N/A // int _exception_line; // line information for exception (debugging only) 0N/A // Support for forcing alignment of thread objects for biased locking 0N/A void operator delete(
void* p);
0N/A // *************************************************************** 0N/A // Suspend and resume support 0N/A // *************************************************************** 0N/A // things including safepoints but was deprecated and finally removed 0N/A // in Java 7. Because VM suspension was considered "internal" Java-level 0N/A // suspension was considered "external", and this legacy naming scheme 0N/A // JVM_ResumeThread, JVMTI SuspendThread, and finally JVMTI 0N/A // ResumeThread. External 0N/A // suspend requests cause _external_suspend to be set and external 0N/A // resume requests cause _external_suspend to be cleared. 0N/A // External suspend requests do not nest on top of other external 0N/A // suspend requests. The higher level APIs reject suspend requests 0N/A // for already suspended threads. 0N/A // The external_suspend 0N/A // flag is checked by has_special_runtime_exit_condition() and java thread 0N/A // will self-suspend when handle_special_runtime_exit_condition() is 0N/A // called. Most uses of the _thread_blocked state in JavaThreads are 0N/A // considered the same as being externally suspended; if the blocking 0N/A // condition lifts, the JavaThread will self-suspend. Other places 0N/A // where VM checks for external_suspend include: 0N/A // + mutex granting (do not enter monitors when thread is suspended) 0N/A // + state transitions from _thread_in_native 0N/A // In general, java_suspend() does not wait for an external suspend 0N/A // request to complete. When it returns, the only guarantee is that 0N/A // the _external_suspend field is true. 0N/A // wait_for_ext_suspend_completion() is used to wait for an external 0N/A // suspend request to complete. External suspend requests are usually 0N/A // followed by some other interface call that requires the thread to 0N/A // be quiescent, e.g., GetCallTrace(). By moving the "wait time" into 0N/A // the interface that requires quiescence, we give the JavaThread a 0N/A // chance to self-suspend before we need it to be quiescent. This 0N/A // It must be set under the protection of SR_lock. Read from the flag is 0N/A // OK without SR_lock as long as the value is only used as a hint. 0N/A // (e.g., check _external_suspend first without lock and then recheck 0N/A // inside SR_lock and finish the suspension) 0N/A // _suspend_flags is also overloaded for other "special conditions" so 0N/A // that a single check indicates whether any special action is needed 0N/A // eg. for async exceptions. 0N/A // ------------------------------------------------------------------- 0N/A // but we still update its value to keep other part of the system (mainly 0N/A // JVMTI) happy. ThreadState is legacy code (see notes in 0N/A // 2. It would be more natural if set_external_suspend() is private and 0N/A // part of java_suspend(), but that probably would affect the suspend/query 0N/A // performance. Need more investigation on this. 0N/A // NOTE: avoid using the sign-bit as cc generates different test code 0N/A // when the sign-bit is used, and sometimes incorrectly - see CR 6398077 0N/A // various suspension related flags - atomically updated 0N/A // overloaded for async exception checking in check_special_condition_for_native_trans. 0N/A // Active_handles points to a block of handles 0N/A // One-element thread local free list 0N/A // Point to the last handle mark 0N/A // The parity of the last strong_roots iteration in which this thread was 0N/A // claimed as a task. 0N/A // debug support for checking if code does allow safepoints or not 0N/A // GC points in the VM can happen because of allocation, invoking a VM operation, or blocking on 0N/A // mutex, or blocking on an object synchronizer (Java locking). 0N/A // If !allow_safepoint(), then an assertion failure will happen in any of the above cases 0N/A // If !allow_allocation(), then an assertion failure will happen during allocation 0N/A // (Hence, !allow_safepoint() => !allow_allocation()). 0N/A // The two classes No_Safepoint_Verifier and No_Allocation_Verifier are used to set these counters. 806N/A // Used by SkipGCALot class. 0N/A // Record when GC is locked out via the GC_locker mechanism 0N/A // is waiting to lock 0N/A // ObjectMonitor on which this thread called Object.wait() 0N/A // Private thread-local objectmonitor list - a simple cache organized as a SLL. 0N/A // thread entry point 0N/A // Remove this ifdef when C1 is ported to the compiler interface. 0N/A // True iff the thread can perform GC operations at a safepoint. 0N/A // Generally will be true only of VM thread and parallel GC WorkGang 0N/A virtual char*
name()
const {
return (
char*)
"Unknown thread"; }
0N/A // Returns the current thread 0N/A // Common thread operations 0N/A // Support for Unhandled Oop detection 0N/A // Mark oop safe for gc. It may be stack allocated but won't move. 0N/A // Clear oops at safepoint so crashes point to unhandled oop violator 0N/A#
endif // CHECK_UNHANDLED_OOPS 0N/A // Installs a pending exception to be inserted later 0N/A // JNI handle support 0N/A // Internal handle support 0N/A // Thread-Local Allocation Buffer (TLAB) support 0N/A // VM operation support 0N/A // For tracking the heavyweight monitor the thread is pending on. 0N/A // For tracking the ObjectMonitor on which this thread called Object.wait() 0N/A // Apply "f->do_oop" to all root oops in "this". 989N/A // Apply "cf->do_code_blob" (if !NULL) to all code blobs active in frames 0N/A // Handles the parallel case for the method below. 0N/A // Requires that "collection_parity" is that of the current strong roots 0N/A // iteration. If "is_par" is false, sets the parity of "this" to 0N/A // "collection_parity", and returns "true". If "is_par" is true, 0N/A // uses an atomic instruction to set the current threads parity to 0N/A // "collection_parity", if it is not already. Returns "true" iff the 0N/A // calling thread does the update, this indicates that the calling thread 0N/A // has claimed the thread's stack as a root groop in the current 0N/A // Tells if adr belong to this thread. This is used 0N/A // for checking if a lock is owned by the running thread. 702N/A // Used by fast lock support 0N/A // Check if address is in the stack of the thread (not just for locks). 702N/A // Warning: the method can only be used on the running thread 0N/A // Sets this thread as starting thread. Returns failure if thread 0N/A // creation fails due to lack of memory, too many threads etc. 0N/A // OS data associated with the thread 0N/A // Thread local resource area for temporary allocation within the VM 0N/A // Thread local handle area for allocation of handles within the VM 0N/A // Support for stack overflow handling, get_thread, etc. 0N/A // Stack overflow support 0N/A // Deadlock detection support for Mutex locks. List of locks own by thread. 0N/A // Mutex::set_owner_implementation is the only place where _owned_locks is modified, 0N/A // thus the friendship 0N/A // Deadlock detection 0N/A // Low-level leaf-lock primitives used to implement synchronization 0N/A // and native monitor-mutex infrastructure. 0N/A // Not for general synchronization use. 0N/A// Inline implementation of Thread::current() 0N/A// Thread::current is "hot" it's called > 128K times in the 1st 500 msecs of 0N/A// ThreadLocalStorage::thread is warm -- it's called > 16K times in the same 0N/A// This function is very high traffic. Define PARANOID to enable expensive 0N/A // Signal handler should call ThreadLocalStorage::get_thread_slow() 0N/A "Don't use Thread::current() inside signal handler");
0N/A// Name support for threads. non-JavaThread subclasses with multiple 0N/A// uniquely named instances should derive from this. 1119N/A // log JavaThread being processed by oops_do 0N/A // May only be called once per thread. 0N/A// Worker threads are named and have an id of an assigned work. 0N/A// A single WatcherThread is used for simulating timer interrupts. 0N/A char*
name()
const {
return (
char*)
"VM Periodic Task Thread"; }
0N/A // Returns the single instance of WatcherThread 0N/A // Create and start the single instance of WatcherThread, or stop it on shutdown 0N/A private:
// restore original namespace restriction 0N/A#
endif // ifdef ASSERT 0N/A private:
// restore original namespace restriction 0N/A // transition out of native 0N/A // Because deoptimization is lazy we must save jvmti requests to set locals 0N/A // in compiled frames until we deoptimize and we have an interpreter frame. 0N/A // This holds the pointer to array (yeah like there might be more than one) of 0N/A // description of compiled vframes that have locals that need to be updated. 0N/A // Handshake value for fixing 6243940. We need a place for the i2c 0N/A // adapter to store the callee methodOop. This value is NEVER live 0N/A // across a gc point so it does NOT have to be gc'd 0N/A // The handshake is open ended since we can't be certain that it will 0N/A // be NULLed. This is because we rarely ever see the race and end up 0N/A // in handle_wrong_method which is the backend of the handshake. See 0N/A // code in i2c adapters and handle_wrong_method. 0N/A // Oop results of VM runtime calls 1027N/A // See ReduceInitialCardMarks: this holds the precise space interval of 1027N/A // the most recent slow path allocation for which compiled code has 1027N/A // elided card-marks for performance along the fast-path. 0N/A // allocated during deoptimization 0N/A // Async. requests support 0N/A // Safepoint support 0N/A public:
// Expose _thread_state for SafeFetchInt() 0N/A // JavaThread termination support 0N/A _vm_exited // JavaThread is still executing native code, but VM is terminated 0N/A // only VM_Exit can set _vm_exited 0N/A // In general a JavaThread's _terminated field transitions as follows: 0N/A // _not_terminated => _thread_exiting => _thread_terminated 0N/A // _vm_exited is a special value to cover the case of a JavaThread 0N/A // executing native code after the VM itself is terminated. 0N/A // handlers thread is in 0N/A // never locked) when throwing an exception. Used by interpreter only. 0N/A // Flag to mark a JNI thread in the process of attaching - See CR 6404306 0N/A // This flag is never set true other than at construction, and in that case 0N/A // is shortly thereafter set false 0N/A // State of the stack guard pages for this thread. 0N/A // Compiler exception handling (NOTE: The _exception_oop is *NOT* the same as _pending_exception. It is 0N/A // used to temp. parsing values into and out of the runtime system during exception handling for compiled 0N/A // support for compilation 0N/A bool _is_compiling;
// is true if a compilation is active inthis thread (one compilation per thread possible) 0N/A // support for JNI critical regions 0N/A // For deadlock detection. 0N/A // JVMTI PopFrame support 0N/A // This is set to popframe_pending to signal that top Java frame should be popped immediately 0N/A // We use intptr_t instead of address so debugger doesn't try and display strings 342N/A // Support for G1 barriers 342N/A // Set of all such queues. 342N/A // Set of all such queues. 0N/A // verify this JavaThread hasn't be published in the Threads::list yet 0N/A //JNI functiontable getter/setter for JVMTI jni function table interception API. 0N/A // Cleanup on thread exit 0N/A // Thread chain operations 0N/A // Thread oop. threadObj() can be NULL for initial JavaThread 0N/A // (or for threads attached via JNI) 0N/A // Prepare thread and add to priority queue. If a priority is 0N/A // not specified, use the priority of the thread object. Threads_lock 0N/A // must be held while this function is called. 0N/A // Allocates a new Java level thread object for this thread. thread_name may be NULL. 0N/A // Last frame anchor routines 0N/A // Safepoint support 0N/A // thread has called JavaThread::exit() or is terminated 0N/A // thread is terminated (no longer on the threads list); we compare 0N/A // against the two non-terminated values so that a freed JavaThread 0N/A // will also be considered terminated. 0N/A // special for Threads::remove() which is static: 0N/A // were we externally suspended while we were waiting? 0N/A // don't surprise the thread that suspended us by returning 0N/A // Check for async exception in addition to safepoint and suspend request. 0N/A // Warning: is_ext_suspend_completed() may temporarily drop the 0N/A // SR_lock to allow the thread to reach a stable thread state if 0N/A // it is currently in a transient thread state. 0N/A // We cannot allow wait_for_ext_suspend_completion() to run forever or 0N/A // we could hang. SuspendRetryCount and SuspendRetryDelay are normally 0N/A // passed as the count and delay parameters. Experiments with specific 0N/A // calls to wait_for_ext_suspend_completion() can be done by passing 0N/A // other values in the code. Experiments with all calls can be done 0N/A // via the appropriate -XX options. 0N/A // Whenever a thread transitions from native to vm/java it must suspend 0N/A // if external|deopt suspend is present. 0N/A // external suspend request is completed 0N/A // Special method to handle a pending external suspend request 0N/A // when a suspend equivalent condition lifts. 0N/A "should only be called in a suspend equivalence condition");
0N/A // not about to self-suspend so clear suspend equivalence 0N/A // We have a pending external suspend request so we leave the 0N/A // suspend_equivalent flag set until java_suspend_self() sets 0N/A // the ext_suspended flag and clears the suspend_equivalent 0N/A // flag. This insures that wait_for_ext_suspend_completion() 0N/A // will return consistent values. 0N/A // utility methods to see if we are doing some kind of suspension 0N/A // Are any async conditions present? 0N/A // these next two are also used for self-suspension and async exception support 0N/A // Return true if JavaThread has an asynchronous condition or 0N/A // if external suspension is requested. 0N/A // We call is_external_suspend() last since external suspend should 0N/A // be less common. Because we don't use is_external_suspend_with_lock 0N/A // it is possible that we won't see an asynchronous external suspend 0N/A // request that has just gotten started, i.e., SR_lock grabbed but 0N/A // _external_suspend field change either not made yet or not visible 0N/A // yet. However, this is okay because the request is asynchronous and 0N/A // we will see the new flag value the next time through. It's also 0N/A // possible that the external suspend request is dropped after 0N/A // we have checked is_external_suspend(), we will recheck its value 0N/A // under SR_lock in java_suspend_self(). 0N/A // Fast-locking support 0N/A // Accessors for vframe array top 0N/A // The linked list of vframe arrays are sorted on sp. This means when we 0N/A // unpack the head must contain the vframe array to unpack. 0N/A // Side structure for defering update of java frame locals until deopt occurs 0N/A // These only really exist to make debugging deopt problems simpler 0N/A // The special resourceMark used during deoptimization 0N/A // Oop results of vm runtime calls 0N/A // Exception handling for compiled methods 0N/A // Stack overflow support 0N/A // Attempt to reguard the stack after a stack overflow may have occurred. 0N/A // Returns true if (a) guard pages are not needed on this thread, (b) the 0N/A // pages are already guarded, or (c) the pages were successfully reguarded. 0N/A // Returns false if there is not enough stack space to reguard the pages, in 0N/A // which case the caller should unwind a frame and try again. The argument 0N/A // should be the caller's (approximate) sp. 0N/A // Similar to above but see if current stackpoint is out of the guard area 0N/A // and reguard if possible. 0N/A // For assembly stub generation 0N/A // Returns the jni environment for this thread 0N/A // Only return NULL if thread is off the thread list; starting to 0N/A // exit should not return NULL. 0N/A // JNI critical regions. These can nest. 0N/A "this must be current thread");
0N/A "this must be current thread");
0N/A "JNI critical nesting problem?"); }
0N/A // For deadlock detection 0N/A // Frame iteration; calls the function f for all frames on the stack 0N/A // Memory operations 0N/A // Sweeper operations 0N/A // Memory management operations 0N/A // factor out low-level mechanics for use in both normal and error cases 0N/A // Returns method at 'depth' java or native frames down the stack 0N/A // Used for security checks 0N/A // Print stack trace in external format 0N/A // Print stack traces in various internal formats 0N/A // Returns the number of stack frames on the stack 0N/A // Function for testing deoptimization 0N/A // Static operations 0N/A // Returns the running thread as a JavaThread 0N/A // Returns the active Java thread. Do not use this if you know you are calling 0N/A // from a JavaThread, as it's slower than JavaThread::current. If called from 0N/A // the VMThread, it also returns the JavaThread that instigated the VMThread's 0N/A // operation. You may not want that either. 0N/A // Returns the privileged_stack information. 0N/A // Thread local information maintained by JVMTI. 609N/A // A JvmtiThreadState is lazily allocated. This jvmti_thread_state() 609N/A // getter is used to get this JavaThread's JvmtiThreadState if it has 609N/A // one which means NULL can be returned. JvmtiThreadState::state_for() 609N/A // is used to get the specified JavaThread's JvmtiThreadState if it has 609N/A // one or it allocates a new JvmtiThreadState for the JavaThread and 609N/A // returns it. JvmtiThreadState::state_for() will return NULL only if 609N/A // the specified JavaThread is exiting. 0N/A // JVMTI PopFrame support 0N/A // Setting and clearing popframe_condition 0N/A // All of these enumerated values are bits. popframe_pending 0N/A // indicates that a PopFrame() has been requested and not yet been 0N/A // completed. popframe_processing indicates that that PopFrame() is in 0N/A // the process of being completed. popframe_force_deopt_reexecution_bit 0N/A // indicates that special handling is required when returning to a 0N/A // deoptimized caller. 0N/A // Saved incoming arguments to popped frame. 0N/A // Used only when popped interpreted frame returns to deoptimized frame. 0N/A // Used by the interpreter in fullspeed mode for frame pop, method 0N/A // entry, method exit and single stepping support. This field is 0N/A // only set to non-zero by the VM_EnterInterpOnlyMode VM operation. 0N/A // It can be set to zero asynchronously (i.e., without a VM operation 0N/A // or a lock) so we have to be very careful. 0N/A // used by the interpreter for fullspeed debugging support (see above) 1213N/A // support for cached flag that indicates whether exceptions need to be posted for this thread 1213N/A // if this is false, we can avoid deoptimizing when events are thrown 1213N/A // this gets set to reflect whether jvmtiExport::post_exception_throw would actually do anything 0N/A // Return a blocker object for which this thread is blocked parking. 342N/A // SATB marking queue support 342N/A // Dirty card queue support 0N/A // Machine dependent stuff 0N/A // JSR166 per-thread parker 0N/A // Biased locking support 342N/A // This field is used to determine if a thread has claimed 342N/A // a par_id: it is -1 if the thread has not claimed a par_id; 342N/A // otherwise its value is the par_id that has been claimed. 0N/A// Inline implementation of JavaThread::current 0N/A // This code assumes java stacks grow down 0N/A// A JavaThread for low memory detection support 0N/A // Hide this thread from external view. 0N/A// A thread used for Compilation. 0N/A // Hide this compiler thread from external view. 0N/A // Get/set the thread's compilation environment. 0N/A // Set once, for good. 0N/A// The active thread queue. It also keeps track of the current used 0N/A// thread priorities. 0N/A // Thread management 0N/A // force_daemon is a concession to JNI, where we may need to add a 0N/A // thread to the thread list before allocating its thread object 0N/A // Initializes the vm and creates the vm thread 0N/A // Supported VM versions via JNI 0N/A // Includes JNI_VERSION_1_1 0N/A // Does not include JNI_VERSION_1_1 0N/A // Garbage collection 0N/A // Apply "f->do_oop" to all root oops in all threads. 0N/A // This version may only be called by sequential code. 0N/A // This version may be called by sequential or parallel code. 0N/A // This creates a list of GCTasks, one per thread. 0N/A // This creates a list of GCTasks, one per thread, for marking objects. 0N/A // Apply "f->do_oop" to roots in all threads that 0N/A // are part of compiled frames 0N/A // Get Java threads that are waiting to enter a monitor. If doLock 0N/A // is true, then Threads_lock is grabbed as needed. Otherwise, the 0N/A // VM needs to be at a safepoint. 0N/A // Get owning Java thread from the monitor's owner field. If doLock 0N/A // is true, then Threads_lock is grabbed as needed. Otherwise, the 0N/A // VM needs to be at a safepoint. 0N/A // Number of threads on the active threads list 0N/A // Number of non-daemon threads on the active threads list 0N/A // Deoptimizes all frames tied to marked nmethods 0N/A// ParkEvents are type-stable and immortal. 0N/A// Lifecycle: Once a ParkEvent is associated with a thread that ParkEvent remains 0N/A// associated with the thread for the thread's entire lifetime - the relationship is 0N/A// stable. A thread will be associated at most one ParkEvent. When the thread 0N/A// expires, the ParkEvent moves to the EventFreeList. New threads attempt to allocate from 0N/A// the EventFreeList before creating a new Event. Type-stability frees us from 0N/A// worrying about stale Event or Thread references in the objectMonitor subsystem. 0N/A// (A reference to ParkEvent is always valid, even though the event may no longer be associated 0N/A// with the desired or expected thread. A key aspect of this design is that the callers of 0N/A// park, unpark, etc must tolerate stale references and spurious wakeups). 0N/A// Only the "associated" thread can block (park) on the ParkEvent, although 0N/A// any other thread can unpark a reachable parkevent. Park() is allowed to 0N/A// return spuriously. In fact park-unpark a really just an optimization to 0N/A// avoid unbounded spinning and surrender the CPU to be a polite system citizen. 0N/A// A degenerate albeit "impolite" park-unpark implementation could simply return. 0N/A// Eventually I'd like to eliminate Events and ObjectWaiters, both of which serve as 0N/A// thread proxies, and simply make the THREAD structure type-stable and persistent. 0N/A// Currently, we unpark events associated with threads, but ideally we'd just 0N/A// The base-class, PlatformEvent, is platform-specific while the ParkEvent is 0N/A// platform-independent. PlatformEvent provides park(), unpark(), etc., and 0N/A// is abstract -- that is, a PlatformEvent should never be instantiated except 0N/A// as part of a ParkEvent. 0N/A// Equivalently we could have defined a platform-independent base-class that 0N/A// exported Allocate(), Release(), etc. The platform-specific class would extend 0N/A// that base-class, adding park(), unpark(), etc. 0N/A// A word of caution: The JVM uses 2 very similar constructs: 0N/A// 1. ParkEvent are used for Java-level "monitor" synchronization. 0N/A// 2. Parkers are used by JSR166-JUC park-unpark. 0N/A// We'll want to eventually merge these redundant facilities and use ParkEvent. 0N/A // Current association 0N/A // diagnostic : keep track of last thread to wake this thread. 0N/A // this is useful for construction of dependency graphs. 0N/A volatile int Notified ;
// for native monitor construct 0N/A // It's prudent to mark the dtor as "private" 0N/A // ensuring that it's not visible outside the package. 0N/A // Unfortunately gcc warns about such usage, so 0N/A // we revert to the less desirable "protected" visibility. 0N/A // The other compilers accept private dtors. 0N/A protected:
// Ensure dtor is never invoked 0N/A // We use placement-new to force ParkEvent instances to be 0N/A // aligned on 256-byte address boundaries. This ensures that the least 0N/A // significant byte of a ParkEvent address is always 0. 0N/A void operator delete (
void * a) ;