1 /*
  2  * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_RUNTIME_THREADS_HPP
 27 #define SHARE_RUNTIME_THREADS_HPP
 28 
 29 #include "jni.h"
 30 #include "utilities/exceptions.hpp"
 31 #include "utilities/globalDefinitions.hpp"
 32 #include "utilities/macros.hpp"
 33 
 34 class JavaThread;
 35 class Metadata;
 36 class MetadataClosure;
 37 class OopClosure;
 38 class Thread;
 39 class ThreadClosure;
 40 class ThreadsList;
 41 class outputStream;
 42 
 43 // The active thread queue. It also keeps track of the current used
 44 // thread priorities.
 45 class Threads: AllStatic {
 46   friend class VMStructs;
 47  private:
 48   static int         _number_of_threads;
 49   static int         _number_of_non_daemon_threads;
 50   static int         _return_code;
 51   static uintx       _thread_claim_token;
 52 #ifdef ASSERT
 53   static bool        _vm_complete;
 54 #endif
 55 
 56   static void initialize_java_lang_classes(JavaThread* main_thread, TRAPS);
 57   static bool initialize_compilation(TRAPS);
 58   static void initialize_jsr292_core_classes(TRAPS);
 59 
 60  public:
 61   // Thread management
 62   // force_daemon is a concession to JNI, where we may need to add a
 63   // thread to the thread list before allocating its thread object
 64   static void add(JavaThread* p, bool force_daemon = false);
 65   static void remove(JavaThread* p, bool is_daemon);
 66   static void non_java_threads_do(ThreadClosure* tc);
 67   static void java_threads_do(ThreadClosure* tc);
 68   static void threads_do(ThreadClosure* tc);
 69   static void possibly_parallel_threads_do(bool is_par, ThreadClosure* tc);
 70 
 71   // Initializes the vm and creates the vm thread
 72   static jint create_vm(JavaVMInitArgs* args, bool* canTryAgain);
 73   static void convert_vm_init_libraries_to_agents();
 74   static void create_vm_init_libraries();
 75   static void create_vm_init_agents();
 76   static void shutdown_vm_agents();
 77   static void destroy_vm();
 78   // Supported VM versions via JNI
 79   // Includes JNI_VERSION_1_1
 80   static jboolean is_supported_jni_version_including_1_1(jint version);
 81   // Does not include JNI_VERSION_1_1
 82   static jboolean is_supported_jni_version(jint version);
 83 
 84 private:
 85   // The "thread claim token" provides a way for threads to be claimed
 86   // by parallel worker tasks.
 87   //
 88   // Each thread contains a "token" field. A task will claim the
 89   // thread only if its token is different from the global token,
 90   // which is updated by calling change_thread_claim_token().  When
 91   // a thread is claimed, it's token is set to the global token value
 92   // so other threads in the same iteration pass won't claim it.
 93   //
 94   // For this to work change_thread_claim_token() needs to be called
 95   // exactly once in sequential code before starting parallel tasks
 96   // that should claim threads.
 97   //
 98   // New threads get their token set to 0 and change_thread_claim_token()
 99   // never sets the global token to 0.
100   static uintx thread_claim_token() { return _thread_claim_token; }
101 
102 public:
103   static void change_thread_claim_token();
104   static void assert_all_threads_claimed() NOT_DEBUG_RETURN;
105 
106   // Apply "f->do_oop" to all root oops in all threads.
107   // This version may only be called by sequential code.
108   static void oops_do(OopClosure* f, NMethodClosure* cf);
109   // This version may be called by sequential or parallel code.
110   static void possibly_parallel_oops_do(bool is_par, OopClosure* f, NMethodClosure* cf);
111 
112   // RedefineClasses support
113   static void metadata_do(MetadataClosure* f);
114   static void metadata_handles_do(void f(Metadata*));
115 
116 #ifdef ASSERT
117   static bool is_vm_complete() { return _vm_complete; }
118 #endif // ASSERT
119 
120   // Verification
121   static void verify();
122   static void print_on(outputStream* st, bool print_stacks, bool internal_format, bool print_concurrent_locks, bool print_extended_info);
123   static void print(bool print_stacks, bool internal_format) {
124     // this function is only used by debug.cpp
125     print_on(tty, print_stacks, internal_format, false /* no concurrent lock printed */, false /* simple format */);
126   }
127   static void print_on_error(outputStream* st, Thread* current, char* buf, int buflen);
128   static void print_on_error(Thread* this_thread, outputStream* st, Thread* current, char* buf,
129                              int buflen, bool* found_current);
130   // Print threads busy compiling, and returns the number of printed threads.
131   static unsigned print_threads_compiling(outputStream* st, char* buf, int buflen, bool short_form = false);
132 
133   // Get Java threads that are waiting to enter or re-enter the specified monitor.
134   // Java threads that are executing mounted virtual threads are not included.
135   static GrowableArray<JavaThread*>* get_pending_threads(ThreadsList * t_list,
136                                                          int count, address monitor);
137 
138   // Get owning Java thread from the monitor's owner field.
139   static JavaThread *owning_thread_from_monitor_owner(ThreadsList * t_list,
140                                                       address owner);
141 
142   static JavaThread* owning_thread_from_object(ThreadsList* t_list, oop obj);
143   static JavaThread* owning_thread_from_monitor(ThreadsList* t_list, ObjectMonitor* owner);
144 
145   // Number of threads on the active threads list
146   static int number_of_threads()                 { return _number_of_threads; }
147   // Number of non-daemon threads on the active threads list
148   static int number_of_non_daemon_threads()      { return _number_of_non_daemon_threads; }
149 
150   struct Test;                  // For private gtest access.
151 };
152 
153 #endif // SHARE_RUNTIME_THREADS_HPP