src/share/vm/runtime/osThread.hpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_RUNTIME_OSTHREAD_HPP
aoqi@0 26 #define SHARE_VM_RUNTIME_OSTHREAD_HPP
aoqi@0 27
aoqi@0 28 #include "runtime/frame.hpp"
aoqi@0 29 #include "runtime/handles.hpp"
aoqi@0 30 #include "runtime/javaFrameAnchor.hpp"
aoqi@0 31 #include "runtime/objectMonitor.hpp"
aoqi@0 32 #include "utilities/top.hpp"
aoqi@0 33
aoqi@0 34 // The OSThread class holds OS-specific thread information. It is equivalent
aoqi@0 35 // to the sys_thread_t structure of the classic JVM implementation.
aoqi@0 36
aoqi@0 37 // The thread states represented by the ThreadState values are platform-specific
aoqi@0 38 // and are likely to be only approximate, because most OSes don't give you access
aoqi@0 39 // to precise thread state information.
aoqi@0 40
aoqi@0 41 // Note: the ThreadState is legacy code and is not correctly implemented.
aoqi@0 42 // Uses of ThreadState need to be replaced by the state in the JavaThread.
aoqi@0 43
aoqi@0 44 enum ThreadState {
aoqi@0 45 ALLOCATED, // Memory has been allocated but not initialized
aoqi@0 46 INITIALIZED, // The thread has been initialized but yet started
aoqi@0 47 RUNNABLE, // Has been started and is runnable, but not necessarily running
aoqi@0 48 MONITOR_WAIT, // Waiting on a contended monitor lock
aoqi@0 49 CONDVAR_WAIT, // Waiting on a condition variable
aoqi@0 50 OBJECT_WAIT, // Waiting on an Object.wait() call
aoqi@0 51 BREAKPOINTED, // Suspended at breakpoint
aoqi@0 52 SLEEPING, // Thread.sleep()
aoqi@0 53 ZOMBIE // All done, but not reclaimed yet
aoqi@0 54 };
aoqi@0 55
aoqi@0 56 // I'd make OSThread a ValueObj embedded in Thread to avoid an indirection, but
aoqi@0 57 // the assembler test in java.cpp expects that it can install the OSThread of
aoqi@0 58 // the main thread into its own Thread at will.
aoqi@0 59
aoqi@0 60
aoqi@0 61 class OSThread: public CHeapObj<mtThread> {
aoqi@0 62 friend class VMStructs;
aoqi@0 63 private:
aoqi@0 64 OSThreadStartFunc _start_proc; // Thread start routine
aoqi@0 65 void* _start_parm; // Thread start routine parameter
aoqi@0 66 volatile ThreadState _state; // Thread state *hint*
aoqi@0 67 volatile jint _interrupted; // Thread.isInterrupted state
aoqi@0 68
aoqi@0 69 // Note: _interrupted must be jint, so that Java intrinsics can access it.
aoqi@0 70 // The value stored there must be either 0 or 1. It must be possible
aoqi@0 71 // for Java to emulate Thread.currentThread().isInterrupted() by performing
aoqi@0 72 // the double indirection Thread::current()->_osthread->_interrupted.
aoqi@0 73
aoqi@0 74 // Methods
aoqi@0 75 public:
aoqi@0 76 void set_state(ThreadState state) { _state = state; }
aoqi@0 77 ThreadState get_state() { return _state; }
aoqi@0 78
aoqi@0 79 OSThread(OSThreadStartFunc start_proc, void* start_parm);
aoqi@0 80 ~OSThread();
aoqi@0 81
aoqi@0 82 // Accessors
aoqi@0 83 OSThreadStartFunc start_proc() const { return _start_proc; }
aoqi@0 84 void set_start_proc(OSThreadStartFunc start_proc) { _start_proc = start_proc; }
aoqi@0 85 void* start_parm() const { return _start_parm; }
aoqi@0 86 void set_start_parm(void* start_parm) { _start_parm = start_parm; }
aoqi@0 87
aoqi@0 88 volatile bool interrupted() const { return _interrupted != 0; }
aoqi@0 89 void set_interrupted(bool z) { _interrupted = z ? 1 : 0; }
aoqi@0 90
aoqi@0 91 // Printing
aoqi@0 92 void print_on(outputStream* st) const;
aoqi@0 93 void print() const { print_on(tty); }
aoqi@0 94
aoqi@0 95 // For java intrinsics:
aoqi@0 96 static ByteSize interrupted_offset() { return byte_offset_of(OSThread, _interrupted); }
aoqi@0 97
aoqi@0 98 // Platform dependent stuff
aoqi@0 99 #ifdef TARGET_OS_FAMILY_linux
aoqi@0 100 # include "osThread_linux.hpp"
aoqi@0 101 #endif
aoqi@0 102 #ifdef TARGET_OS_FAMILY_solaris
aoqi@0 103 # include "osThread_solaris.hpp"
aoqi@0 104 #endif
aoqi@0 105 #ifdef TARGET_OS_FAMILY_windows
aoqi@0 106 # include "osThread_windows.hpp"
aoqi@0 107 #endif
aoqi@0 108 #ifdef TARGET_OS_FAMILY_aix
aoqi@0 109 # include "osThread_aix.hpp"
aoqi@0 110 #endif
aoqi@0 111 #ifdef TARGET_OS_FAMILY_bsd
aoqi@0 112 # include "osThread_bsd.hpp"
aoqi@0 113 #endif
aoqi@0 114
aoqi@0 115 public:
aoqi@0 116 static ByteSize thread_id_offset() { return byte_offset_of(OSThread, _thread_id); }
aoqi@0 117 static size_t thread_id_size() { return sizeof(thread_id_t); }
aoqi@0 118
aoqi@0 119 thread_id_t thread_id() const { return _thread_id; }
aoqi@0 120
aoqi@0 121 void set_thread_id(thread_id_t id) { _thread_id = id; }
aoqi@0 122
aoqi@0 123 private:
aoqi@0 124 // _thread_id is kernel thread id (similar to LWP id on Solaris). Each
aoqi@0 125 // thread has a unique thread_id (BsdThreads or NPTL). It can be used
aoqi@0 126 // to access /proc.
aoqi@0 127 thread_id_t _thread_id;
aoqi@0 128 };
aoqi@0 129
aoqi@0 130
aoqi@0 131 // Utility class for use with condition variables:
aoqi@0 132 class OSThreadWaitState : public StackObj {
aoqi@0 133 OSThread* _osthread;
aoqi@0 134 ThreadState _old_state;
aoqi@0 135 public:
aoqi@0 136 OSThreadWaitState(OSThread* osthread, bool is_object_wait) {
aoqi@0 137 _osthread = osthread;
aoqi@0 138 _old_state = osthread->get_state();
aoqi@0 139 if (is_object_wait) {
aoqi@0 140 osthread->set_state(OBJECT_WAIT);
aoqi@0 141 } else {
aoqi@0 142 osthread->set_state(CONDVAR_WAIT);
aoqi@0 143 }
aoqi@0 144 }
aoqi@0 145 ~OSThreadWaitState() {
aoqi@0 146 _osthread->set_state(_old_state);
aoqi@0 147 }
aoqi@0 148 };
aoqi@0 149
aoqi@0 150
aoqi@0 151 // Utility class for use with contended monitors:
aoqi@0 152 class OSThreadContendState : public StackObj {
aoqi@0 153 OSThread* _osthread;
aoqi@0 154 ThreadState _old_state;
aoqi@0 155 public:
aoqi@0 156 OSThreadContendState(OSThread* osthread) {
aoqi@0 157 _osthread = osthread;
aoqi@0 158 _old_state = osthread->get_state();
aoqi@0 159 osthread->set_state(MONITOR_WAIT);
aoqi@0 160 }
aoqi@0 161 ~OSThreadContendState() {
aoqi@0 162 _osthread->set_state(_old_state);
aoqi@0 163 }
aoqi@0 164 };
aoqi@0 165
aoqi@0 166 #endif // SHARE_VM_RUNTIME_OSTHREAD_HPP

mercurial