annotate src/share/vm/runtime/osThread.hpp @ 2233:083f13976b51

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