osThread.hpp revision 0:a61af66fc99e
1198893Srdivacky/* 2198893Srdivacky * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. 3198893Srdivacky * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4198893Srdivacky * 5198893Srdivacky * This code is free software; you can redistribute it and/or modify it 6198893Srdivacky * under the terms of the GNU General Public License version 2 only, as 7198893Srdivacky * published by the Free Software Foundation. 8198893Srdivacky * 9198893Srdivacky * This code is distributed in the hope that it will be useful, but WITHOUT 10198893Srdivacky * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11198893Srdivacky * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12198893Srdivacky * version 2 for more details (a copy is included in the LICENSE file that 13198893Srdivacky * accompanied this code). 14198893Srdivacky * 15198893Srdivacky * You should have received a copy of the GNU General Public License version 16198893Srdivacky * 2 along with this work; if not, write to the Free Software Foundation, 17198893Srdivacky * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18198893Srdivacky * 19199990Srdivacky * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20198893Srdivacky * CA 95054 USA or visit www.sun.com if you need additional information or 21198893Srdivacky * have any questions. 22199482Srdivacky * 23198893Srdivacky */ 24198893Srdivacky 25198893Srdivacky// The OSThread class holds OS-specific thread information. It is equivalent 26218893Sdim// to the sys_thread_t structure of the classic JVM implementation. 27198893Srdivacky 28198893Srdivacky// The thread states represented by the ThreadState values are platform-specific 29198893Srdivacky// and are likely to be only approximate, because most OSes don't give you access 30198893Srdivacky// to precise thread state information. 31198893Srdivacky 32208600Srdivacky// Note: the ThreadState is legacy code and is not correctly implemented. 33198893Srdivacky// Uses of ThreadState need to be replaced by the state in the JavaThread. 34218893Sdim 35200583Srdivackyenum ThreadState { 36198893Srdivacky ALLOCATED, // Memory has been allocated but not initialized 37198893Srdivacky INITIALIZED, // The thread has been initialized but yet started 38198893Srdivacky RUNNABLE, // Has been started and is runnable, but not necessarily running 39198893Srdivacky MONITOR_WAIT, // Waiting on a contended monitor lock 40198893Srdivacky CONDVAR_WAIT, // Waiting on a condition variable 41218893Sdim OBJECT_WAIT, // Waiting on an Object.wait() call 42198893Srdivacky BREAKPOINTED, // Suspended at breakpoint 43199482Srdivacky SLEEPING, // Thread.sleep() 44199482Srdivacky ZOMBIE // All done, but not reclaimed yet 45198893Srdivacky}; 46198893Srdivacky 47198893Srdivacky// I'd make OSThread a ValueObj embedded in Thread to avoid an indirection, but 48199482Srdivacky// the assembler test in java.cpp expects that it can install the OSThread of 49199482Srdivacky// the main thread into its own Thread at will. 50199482Srdivacky 51199482Srdivacky 52199482Srdivackyclass OSThread: public CHeapObj { 53199482Srdivacky friend class VMStructs; 54199482Srdivacky private: 55199482Srdivacky //void* _start_proc; // Thread start routine 56199482Srdivacky OSThreadStartFunc _start_proc; // Thread start routine 57199482Srdivacky void* _start_parm; // Thread start routine parameter 58218893Sdim volatile ThreadState _state; // Thread state *hint* 59218893Sdim jint _interrupted; // Thread.isInterrupted state 60218893Sdim 61198893Srdivacky // Note: _interrupted must be jint, so that Java intrinsics can access it. 62198893Srdivacky // The value stored there must be either 0 or 1. It must be possible 63199482Srdivacky // for Java to emulate Thread.currentThread().isInterrupted() by performing 64198893Srdivacky // the double indirection Thread::current()->_osthread->_interrupted. 65198893Srdivacky 66199482Srdivacky // Methods 67218893Sdim public: 68198893Srdivacky void set_state(ThreadState state) { _state = state; } 69218893Sdim ThreadState get_state() { return _state; } 70218893Sdim 71218893Sdim // Constructor 72218893Sdim OSThread(OSThreadStartFunc start_proc, void* start_parm); 73218893Sdim 74218893Sdim // Destructor 75218893Sdim ~OSThread(); 76218893Sdim 77218893Sdim // Accessors 78218893Sdim OSThreadStartFunc start_proc() const { return _start_proc; } 79218893Sdim void set_start_proc(OSThreadStartFunc start_proc) { _start_proc = start_proc; } 80218893Sdim void* start_parm() const { return _start_parm; } 81218893Sdim void set_start_parm(void* start_parm) { _start_parm = start_parm; } 82218893Sdim 83218893Sdim bool interrupted() const { return _interrupted != 0; } 84218893Sdim void set_interrupted(bool z) { _interrupted = z ? 1 : 0; } 85218893Sdim 86218893Sdim // Printing 87218893Sdim void print_on(outputStream* st) const; 88218893Sdim void print() const { print_on(tty); } 89218893Sdim 90218893Sdim // For java intrinsics: 91218893Sdim static ByteSize interrupted_offset() { return byte_offset_of(OSThread, _interrupted); } 92198893Srdivacky 93218893Sdim // Platform dependent stuff 94198893Srdivacky #include "incls/_osThread_pd.hpp.incl" 95198893Srdivacky}; 96198893Srdivacky 97198893Srdivacky 98198893Srdivacky// Utility class for use with condition variables: 99198893Srdivackyclass OSThreadWaitState : public StackObj { 100198893Srdivacky OSThread* _osthread; 101198893Srdivacky ThreadState _old_state; 102198893Srdivacky public: 103198893Srdivacky OSThreadWaitState(OSThread* osthread, bool is_object_wait) { 104198893Srdivacky _osthread = osthread; 105198893Srdivacky _old_state = osthread->get_state(); 106198893Srdivacky if (is_object_wait) { 107198893Srdivacky osthread->set_state(OBJECT_WAIT); 108198893Srdivacky } else { 109199482Srdivacky osthread->set_state(CONDVAR_WAIT); 110218893Sdim } 111218893Sdim } 112198893Srdivacky ~OSThreadWaitState() { 113198893Srdivacky _osthread->set_state(_old_state); 114198893Srdivacky } 115198893Srdivacky}; 116199482Srdivacky 117199482Srdivacky 118199482Srdivacky// Utility class for use with contended monitors: 119199482Srdivackyclass OSThreadContendState : public StackObj { 120199482Srdivacky OSThread* _osthread; 121199482Srdivacky ThreadState _old_state; 122218893Sdim public: 123218893Sdim OSThreadContendState(OSThread* osthread) { 124218893Sdim _osthread = osthread; 125218893Sdim _old_state = osthread->get_state(); 126218893Sdim osthread->set_state(MONITOR_WAIT); 127218893Sdim } 128199482Srdivacky ~OSThreadContendState() { 129218893Sdim _osthread->set_state(_old_state); 130218893Sdim } 131218893Sdim}; 132218893Sdim