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 and remove explicit
63 // thread suspension from the loop below.
64 // Lock the thread lists.
67 printf("Full thread dump CACAO "VERSION_FULL":\n");
69 // Iterate over all started threads.
70 threadobject* self = THREADOBJECT;
71 for (List<threadobject*>::iterator it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
72 threadobject* t = *it;
74 // Ignore threads which are in state NEW.
75 if (t->state == THREAD_STATE_NEW)
78 /* Suspend the thread (and ignore return value). */
81 (void) threads_suspend_thread(t, SUSPEND_REASON_DUMP);
83 /* Print thread info. */
89 /* Print trace of thread. */
91 stacktrace_print_of_thread(t);
93 /* Resume the thread (and ignore return value). */
96 (void) threads_resume_thread(t, SUSPEND_REASON_DUMP);
99 // Unlock the thread lists.
105 * Fills the passed list with all currently active threads. Creating a copy
106 * of the thread list here, is the only way to ensure we do not end up in a
107 * dead-lock when iterating over the list.
109 * @param list list class to be filled
111 void ThreadList::get_active_threads(List<threadobject*> &list)
113 // Lock the thread lists.
116 // Use the assignment operator to create a copy of the thread list.
117 list = _active_thread_list;
119 // Unlock the thread lists.
125 * Fills the passed list with all currently active threads which should be
126 * visible to Java. Creating a copy of the thread list here, is the only way
127 * to ensure we do not end up in a dead-lock when iterating over the list.
129 * @param list list class to be filled
131 void ThreadList::get_active_java_threads(List<threadobject*> &list)
133 // Lock the thread lists.
136 // Iterate over all active threads.
137 for (List<threadobject*>::iterator it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
138 threadobject* t = *it;
140 // We skip internal threads.
141 if (t->flags & THREAD_FLAG_INTERNAL)
147 // Unlock the thread lists.
153 * Return a free thread object.
155 * @return free thread object or NULL if none available
157 threadobject* ThreadList::get_free_thread()
159 threadobject* t = NULL;
161 // Do we have free threads in the free-list?
162 if (_free_thread_list.empty() == false) {
163 // Yes, get the index and remove it from the free list.
164 threadobject* t = _free_thread_list.front();
165 _free_thread_list.remove(t);
173 * Return a free thread index.
175 * @return free thread index
177 int32_t ThreadList::get_free_thread_index()
181 // Do we have free indexes in the free-list?
182 if (_free_index_list.empty() == false) {
183 // Yes, get the index and remove it from the free list.
184 index = _free_index_list.front();
185 _free_index_list.remove(index);
188 // Get a new the thread index.
189 index = _active_thread_list.size() + 1;
197 * Return the number of daemon threads visible to Java.
199 * NOTE: This function does a linear-search over the threads list,
200 * because it is only used by the management interface.
202 * @return number of daemon threads
204 int32_t ThreadList::get_number_of_daemon_java_threads(void)
208 // Lock the thread lists.
211 // Iterate over all active threads.
212 for (List<threadobject*>::iterator it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
213 threadobject* t = *it;
215 // We skip internal threads.
216 if (t->flags & THREAD_FLAG_INTERNAL)
219 if (thread_is_daemon(t))
223 // Unlock the thread lists.
231 * Return the number of non-daemon threads.
233 * NOTE: This function does a linear-search over the threads list,
234 * because it is only used for joining the threads.
236 * @return number of non daemon threads
238 int32_t ThreadList::get_number_of_non_daemon_threads(void)
244 for (List<threadobject*>::iterator it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
245 threadobject* t = *it;
247 if (!thread_is_daemon(t))
258 * Return the thread object with the given index.
260 * @return thread object
262 threadobject* ThreadList::get_thread_by_index(int32_t index)
266 List<threadobject*>::iterator it = find_if(_active_thread_list.begin(), _active_thread_list.end(), std::bind2nd(comparator(), index));
269 if (it == _active_thread_list.end()) {
274 threadobject* t = *it;
276 // The thread found is in state new.
277 if (t->state == THREAD_STATE_NEW) {
288 * Return the Java thread object from the given thread object.
290 * @return Java thread object
292 threadobject* ThreadList::get_thread_from_java_object(java_handle_t* h)
294 List<threadobject*>::iterator it;
300 for (it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
303 LLNI_equals(t->object, h, equal);
318 * Release the thread.
320 * @return free thread index
322 void ThreadList::release_thread(threadobject* t)
326 // Move thread from active thread list to free thread list.
327 remove_from_active_thread_list(t);
328 add_to_free_thread_list(t);
330 // Add thread index to free index list.
331 add_to_free_index_list(t->index);
337 /* C interface functions ******************************************************/
340 void ThreadList_lock() { ThreadList::lock(); }
341 void ThreadList_unlock() { ThreadList::unlock(); }
342 void ThreadList_dump_threads() { ThreadList::dump_threads(); }
343 void ThreadList_release_thread(threadobject* t) { ThreadList::release_thread(t); }
344 threadobject* ThreadList_get_free_thread() { return ThreadList::get_free_thread(); }
345 int32_t ThreadList_get_free_thread_index() { return ThreadList::get_free_thread_index(); }
346 void ThreadList_add_to_active_thread_list(threadobject* t) { ThreadList::add_to_active_thread_list(t); }
347 threadobject* ThreadList_get_thread_by_index(int32_t index) { return ThreadList::get_thread_by_index(index); }
348 threadobject* ThreadList_get_main_thread() { return ThreadList::get_main_thread(); }
349 threadobject* ThreadList_get_thread_from_java_object(java_handle_t* h) { return ThreadList::get_thread_from_java_object(h); }
351 int32_t ThreadList_get_number_of_non_daemon_threads() { return ThreadList::get_number_of_non_daemon_threads(); }
355 * These are local overrides for various environment variables in Emacs.
356 * Please do not remove this and leave it at the end of the file, where
357 * Emacs will automagically detect them.
358 * ---------------------------------------------------------------------
361 * indent-tabs-mode: t
365 * vim:noexpandtab:sw=4:ts=4: