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