1 /* src/threads/threadlist.cpp - thread list
3 Copyright (C) 2008, 2009
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 #include "threads/mutex.hpp"
33 #include "threads/threadlist.hpp"
34 #include "threads/thread.hpp"
36 #include "toolbox/list.hpp"
37 #include "toolbox/logging.hpp"
39 #include "vm/jit/stacktrace.hpp"
44 Mutex ThreadList::_mutex; // a mutex for all thread lists
46 List<threadobject*> ThreadList::_active_thread_list; // list of active threads
47 List<threadobject*> ThreadList::_free_thread_list; // list of free threads
48 List<int32_t> ThreadList::_free_index_list; // list of free thread indexes
50 int32_t ThreadList::_number_of_started_java_threads;
51 int32_t ThreadList::_number_of_active_java_threads;
52 int32_t ThreadList::_peak_of_active_java_threads;
53 int32_t ThreadList::_number_of_non_daemon_threads;
57 * Dumps info for all threads running in the VM. This function is
58 * called when SIGQUIT (<ctrl>-\) is sent to the VM.
60 void ThreadList::dump_threads()
62 // XXX we should stop the world here
63 // Lock the thread lists.
66 printf("Full thread dump CACAO "VERSION":\n");
68 // Iterate over all started threads.
69 for (List<threadobject*>::iterator it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
70 threadobject* t = *it;
72 // Ignore threads which are in state NEW.
73 if (t->state == THREAD_STATE_NEW)
76 #if defined(ENABLE_GC_CACAO)
77 /* Suspend the thread. */
78 /* XXX Is the suspend reason correct? */
80 if (threads_suspend_thread(t, SUSPEND_REASON_JNI) == false)
81 vm_abort("threads_dump: threads_suspend_thread failed");
84 /* Print thread info. */
90 /* Print trace of thread. */
92 stacktrace_print_of_thread(t);
94 #if defined(ENABLE_GC_CACAO)
95 /* Resume the thread. */
97 if (threads_resume_thread(t) == false)
98 vm_abort("threads_dump: threads_resume_thread failed");
102 // Unlock the thread lists.
108 * Fills the passed list with all currently active threads. Creating a copy
109 * of the thread list here, is the only way to ensure we do not end up in a
110 * dead-lock when iterating over the list.
112 * @param list list class to be filled
114 void ThreadList::get_active_threads(List<threadobject*> &list)
116 // Lock the thread lists.
119 // Use the assignment operator to create a copy of the thread list.
120 list = _active_thread_list;
122 // Unlock the thread lists.
128 * Fills the passed list with all currently active threads which should be
129 * visible to Java. Creating a copy of the thread list here, is the only way
130 * to ensure we do not end up in a dead-lock when iterating over the list.
132 * @param list list class to be filled
134 void ThreadList::get_active_java_threads(List<threadobject*> &list)
136 // Lock the thread lists.
139 // Iterate over all active threads.
140 for (List<threadobject*>::iterator it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
141 threadobject* t = *it;
143 // We skip internal threads.
144 if (t->flags & THREAD_FLAG_INTERNAL)
150 // Unlock the thread lists.
156 * Return a free thread object.
158 * @return free thread object or NULL if none available
160 threadobject* ThreadList::get_free_thread()
162 threadobject* t = NULL;
164 // Do we have free threads in the free-list?
165 if (_free_thread_list.empty() == false) {
166 // Yes, get the index and remove it from the free list.
167 threadobject* t = _free_thread_list.front();
168 _free_thread_list.remove(t);
176 * Return a free thread index.
178 * @return free thread index
180 int32_t ThreadList::get_free_thread_index()
184 // Do we have free indexes in the free-list?
185 if (_free_index_list.empty() == false) {
186 // Yes, get the index and remove it from the free list.
187 index = _free_index_list.front();
188 _free_index_list.remove(index);
191 // Get a new the thread index.
192 index = _active_thread_list.size() + 1;
200 * Return the number of daemon threads visible to Java.
202 * NOTE: This function does a linear-search over the threads list,
203 * because it is only used by the management interface.
205 * @return number of daemon threads
207 int32_t ThreadList::get_number_of_daemon_java_threads(void)
211 // Lock the thread lists.
214 // Iterate over all active threads.
215 for (List<threadobject*>::iterator it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
216 threadobject* t = *it;
218 // We skip internal threads.
219 if (t->flags & THREAD_FLAG_INTERNAL)
222 if (thread_is_daemon(t))
226 // Unlock the thread lists.
234 * Return the number of non-daemon threads.
236 * NOTE: This function does a linear-search over the threads list,
237 * because it is only used for joining the threads.
239 * @return number of non daemon threads
241 int32_t ThreadList::get_number_of_non_daemon_threads(void)
247 for (List<threadobject*>::iterator it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
248 threadobject* t = *it;
250 if (!thread_is_daemon(t))
261 * Return the thread object with the given index.
263 * @return thread object
265 threadobject* ThreadList::get_thread_by_index(int32_t index)
269 List<threadobject*>::iterator it = find_if(_active_thread_list.begin(), _active_thread_list.end(), std::bind2nd(comparator(), index));
272 if (it == _active_thread_list.end()) {
277 threadobject* t = *it;
279 // The thread found is in state new.
280 if (t->state == THREAD_STATE_NEW) {
291 * Return the Java thread object from the given thread object.
293 * @return Java thread object
295 threadobject* ThreadList::get_thread_from_java_object(java_handle_t* h)
297 List<threadobject*>::iterator it;
303 for (it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
306 LLNI_equals(t->object, h, equal);
321 * Release the thread.
323 * @return free thread index
325 void ThreadList::release_thread(threadobject* t)
329 // Move thread from active thread list to free thread list.
330 remove_from_active_thread_list(t);
331 add_to_free_thread_list(t);
333 // Add thread index to free index list.
334 add_to_free_index_list(t->index);
340 /* C interface functions ******************************************************/
343 void ThreadList_lock() { ThreadList::lock(); }
344 void ThreadList_unlock() { ThreadList::unlock(); }
345 void ThreadList_dump_threads() { ThreadList::dump_threads(); }
346 void ThreadList_release_thread(threadobject* t) { ThreadList::release_thread(t); }
347 threadobject* ThreadList_get_free_thread() { return ThreadList::get_free_thread(); }
348 int32_t ThreadList_get_free_thread_index() { return ThreadList::get_free_thread_index(); }
349 void ThreadList_add_to_active_thread_list(threadobject* t) { ThreadList::add_to_active_thread_list(t); }
350 threadobject* ThreadList_get_thread_by_index(int32_t index) { return ThreadList::get_thread_by_index(index); }
351 threadobject* ThreadList_get_main_thread() { return ThreadList::get_main_thread(); }
352 threadobject* ThreadList_get_thread_from_java_object(java_handle_t* h) { return ThreadList::get_thread_from_java_object(h); }
354 int32_t ThreadList_get_number_of_non_daemon_threads() { return ThreadList::get_number_of_non_daemon_threads(); }
358 * These are local overrides for various environment variables in Emacs.
359 * Please do not remove this and leave it at the end of the file, where
360 * Emacs will automagically detect them.
361 * ---------------------------------------------------------------------
364 * indent-tabs-mode: t
368 * vim:noexpandtab:sw=4:ts=4: