1 /*
  2  * Copyright (c) 1997, 2022, 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 "runtime/suspendedThreadTask.hpp"
 33 #include "utilities/macros.hpp"
 34 
 35 #if defined(LINUX) || defined(AIX) || defined(BSD)
 36 #include "suspendResume_posix.hpp"
 37 #endif
 38 
 39 class Monitor;
 40 
 41 // The OSThread class holds OS-specific thread information.  It is equivalent
 42 // to the sys_thread_t structure of the classic JVM implementation.
 43 
 44 // The thread states represented by the ThreadState values are platform-specific
 45 // and are likely to be only approximate, because most OSes don't give you access
 46 // to precise thread state information.
 47 
 48 // Note: the ThreadState is legacy code and is not correctly implemented.
 49 // Uses of ThreadState need to be replaced by the state in the JavaThread.
 50 
 51 enum ThreadState {
 52   ALLOCATED,                    // Memory has been allocated but not initialized
 53   INITIALIZED,                  // The thread has been initialized but yet started
 54   RUNNABLE,                     // Has been started and is runnable, but not necessarily running
 55   MONITOR_WAIT,                 // Waiting on a contended monitor lock
 56   CONDVAR_WAIT,                 // Waiting on a condition variable
 57   OBJECT_WAIT,                  // Waiting on an Object.wait() call
 58   BREAKPOINTED,                 // Suspended at breakpoint
 59   SLEEPING,                     // Thread.sleep()
 60   ZOMBIE                        // All done, but not reclaimed yet
 61 };
 62 
 63 typedef int (*OSThreadStartFunc)(void*);
 64 
 65 class OSThread: public CHeapObj<mtThread> {
 66   friend class VMStructs;
 67   friend class JVMCIVMStructs;
 68  private:
 69   volatile ThreadState _state;    // Thread state *hint*
 70 
 71   // Methods
 72  public:
 73   void set_state(ThreadState state)                { _state = state; }
 74   ThreadState get_state()                          { return _state; }
 75 
 76   OSThread();
 77   ~OSThread();
 78 
 79   // Printing
 80   void print_on(outputStream* st) const;
 81   void print() const;
 82 
 83   // Platform dependent stuff
 84 #include OS_HEADER(osThread)
 85 
 86  public:
 87 
 88   thread_id_t thread_id() const                   { return _thread_id; }
 89 
 90   void set_thread_id(thread_id_t id)              { _thread_id = id; }
 91 
 92  private:
 93   // _thread_id is kernel thread id (similar to LWP id on Solaris). Each
 94   // thread has a unique thread_id (BsdThreads or NPTL). It can be used
 95   // to access /proc.
 96   thread_id_t _thread_id;
 97 };
 98 
 99 
100 // Utility class for use with condition variables:
101 class OSThreadWaitState : public StackObj {
102   OSThread*   _osthread;
103   ThreadState _old_state;
104  public:
105   OSThreadWaitState(OSThread* osthread, bool is_object_wait) {
106     _osthread  = osthread;
107     _old_state = osthread->get_state();
108     if (is_object_wait) {
109       osthread->set_state(OBJECT_WAIT);
110     } else {
111       osthread->set_state(CONDVAR_WAIT);
112     }
113   }
114   ~OSThreadWaitState() {
115     _osthread->set_state(_old_state);
116   }
117 };
118 
119 
120 // Utility class for use with contended monitors:
121 class OSThreadContendState : public StackObj {
122   OSThread*   _osthread;
123   ThreadState _old_state;
124  public:
125   OSThreadContendState(OSThread* osthread) {
126     _osthread  = osthread;
127     _old_state = osthread->get_state();
128     osthread->set_state(MONITOR_WAIT);
129   }
130   ~OSThreadContendState() {
131     _osthread->set_state(_old_state);
132   }
133 };
134 
135 #endif // SHARE_RUNTIME_OSTHREAD_HPP