duke@435: /* rbackman@3709: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_RUNTIME_OSTHREAD_HPP stefank@2314: #define SHARE_VM_RUNTIME_OSTHREAD_HPP stefank@2314: stefank@2314: #include "runtime/frame.hpp" stefank@2314: #include "runtime/handles.hpp" stefank@2314: #include "runtime/javaFrameAnchor.hpp" stefank@2314: #include "runtime/objectMonitor.hpp" stefank@2314: #include "utilities/top.hpp" stefank@2314: duke@435: // The OSThread class holds OS-specific thread information. It is equivalent duke@435: // to the sys_thread_t structure of the classic JVM implementation. duke@435: duke@435: // The thread states represented by the ThreadState values are platform-specific duke@435: // and are likely to be only approximate, because most OSes don't give you access duke@435: // to precise thread state information. duke@435: duke@435: // Note: the ThreadState is legacy code and is not correctly implemented. duke@435: // Uses of ThreadState need to be replaced by the state in the JavaThread. duke@435: duke@435: enum ThreadState { duke@435: ALLOCATED, // Memory has been allocated but not initialized duke@435: INITIALIZED, // The thread has been initialized but yet started duke@435: RUNNABLE, // Has been started and is runnable, but not necessarily running duke@435: MONITOR_WAIT, // Waiting on a contended monitor lock duke@435: CONDVAR_WAIT, // Waiting on a condition variable duke@435: OBJECT_WAIT, // Waiting on an Object.wait() call duke@435: BREAKPOINTED, // Suspended at breakpoint duke@435: SLEEPING, // Thread.sleep() duke@435: ZOMBIE // All done, but not reclaimed yet duke@435: }; duke@435: duke@435: // I'd make OSThread a ValueObj embedded in Thread to avoid an indirection, but duke@435: // the assembler test in java.cpp expects that it can install the OSThread of duke@435: // the main thread into its own Thread at will. duke@435: duke@435: zgu@3900: class OSThread: public CHeapObj { duke@435: friend class VMStructs; duke@435: private: duke@435: OSThreadStartFunc _start_proc; // Thread start routine duke@435: void* _start_parm; // Thread start routine parameter duke@435: volatile ThreadState _state; // Thread state *hint* dholmes@2668: volatile jint _interrupted; // Thread.isInterrupted state duke@435: duke@435: // Note: _interrupted must be jint, so that Java intrinsics can access it. duke@435: // The value stored there must be either 0 or 1. It must be possible duke@435: // for Java to emulate Thread.currentThread().isInterrupted() by performing duke@435: // the double indirection Thread::current()->_osthread->_interrupted. duke@435: duke@435: // Methods duke@435: public: duke@435: void set_state(ThreadState state) { _state = state; } duke@435: ThreadState get_state() { return _state; } duke@435: duke@435: OSThread(OSThreadStartFunc start_proc, void* start_parm); duke@435: ~OSThread(); duke@435: duke@435: // Accessors duke@435: OSThreadStartFunc start_proc() const { return _start_proc; } duke@435: void set_start_proc(OSThreadStartFunc start_proc) { _start_proc = start_proc; } duke@435: void* start_parm() const { return _start_parm; } duke@435: void set_start_parm(void* start_parm) { _start_parm = start_parm; } duke@435: dholmes@2668: volatile bool interrupted() const { return _interrupted != 0; } duke@435: void set_interrupted(bool z) { _interrupted = z ? 1 : 0; } duke@435: duke@435: // Printing duke@435: void print_on(outputStream* st) const; duke@435: void print() const { print_on(tty); } duke@435: duke@435: // For java intrinsics: duke@435: static ByteSize interrupted_offset() { return byte_offset_of(OSThread, _interrupted); } duke@435: duke@435: // Platform dependent stuff stefank@2314: #ifdef TARGET_OS_FAMILY_linux stefank@2314: # include "osThread_linux.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_OS_FAMILY_solaris stefank@2314: # include "osThread_solaris.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_OS_FAMILY_windows stefank@2314: # include "osThread_windows.hpp" stefank@2314: #endif never@3156: #ifdef TARGET_OS_FAMILY_bsd never@3156: # include "osThread_bsd.hpp" never@3156: #endif stefank@2314: rbackman@3796: public: rbackman@3796: static ByteSize thread_id_offset() { return byte_offset_of(OSThread, _thread_id); } rbackman@3796: static size_t thread_id_size() { return sizeof(thread_id_t); } rbackman@3796: rbackman@3796: thread_id_t thread_id() const { return _thread_id; } rbackman@3796: rbackman@3796: void set_thread_id(thread_id_t id) { _thread_id = id; } rbackman@3796: rbackman@3796: private: rbackman@3796: // _thread_id is kernel thread id (similar to LWP id on Solaris). Each rbackman@3796: // thread has a unique thread_id (BsdThreads or NPTL). It can be used rbackman@3796: // to access /proc. rbackman@3796: thread_id_t _thread_id; duke@435: }; duke@435: duke@435: duke@435: // Utility class for use with condition variables: duke@435: class OSThreadWaitState : public StackObj { duke@435: OSThread* _osthread; duke@435: ThreadState _old_state; duke@435: public: duke@435: OSThreadWaitState(OSThread* osthread, bool is_object_wait) { duke@435: _osthread = osthread; duke@435: _old_state = osthread->get_state(); duke@435: if (is_object_wait) { duke@435: osthread->set_state(OBJECT_WAIT); duke@435: } else { duke@435: osthread->set_state(CONDVAR_WAIT); duke@435: } duke@435: } duke@435: ~OSThreadWaitState() { duke@435: _osthread->set_state(_old_state); duke@435: } duke@435: }; duke@435: duke@435: duke@435: // Utility class for use with contended monitors: duke@435: class OSThreadContendState : public StackObj { duke@435: OSThread* _osthread; duke@435: ThreadState _old_state; duke@435: public: duke@435: OSThreadContendState(OSThread* osthread) { duke@435: _osthread = osthread; duke@435: _old_state = osthread->get_state(); duke@435: osthread->set_state(MONITOR_WAIT); duke@435: } duke@435: ~OSThreadContendState() { duke@435: _osthread->set_state(_old_state); duke@435: } duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_RUNTIME_OSTHREAD_HPP