1 /* src/threads/threadlist.cpp - thread list
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.h"
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_non_daemon_threads;
54 * Dumps info for all threads running in the VM. This function is
55 * called when SIGQUIT (<ctrl>-\) is sent to the VM.
57 void ThreadList::dump_threads()
59 // XXX we should stop the world here
60 // Lock the thread lists.
63 printf("Full thread dump CACAO "VERSION":\n");
65 // Iterate over all started threads.
66 for (List<threadobject*>::iterator it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
67 threadobject* t = *it;
69 // Ignore threads which are in state NEW.
70 if (t->state == THREAD_STATE_NEW)
73 #if defined(ENABLE_GC_CACAO)
74 /* Suspend the thread. */
75 /* XXX Is the suspend reason correct? */
77 if (threads_suspend_thread(t, SUSPEND_REASON_JNI) == false)
78 vm_abort("threads_dump: threads_suspend_thread failed");
81 /* Print thread info. */
87 /* Print trace of thread. */
89 stacktrace_print_of_thread(t);
91 #if defined(ENABLE_GC_CACAO)
92 /* Resume the thread. */
94 if (threads_resume_thread(t) == false)
95 vm_abort("threads_dump: threads_resume_thread failed");
99 // Unlock the thread lists.
105 * Return a free thread object.
107 * @return free thread object or NULL if none available
109 threadobject* ThreadList::get_free_thread()
111 threadobject* t = NULL;
113 // Do we have free threads in the free-list?
114 if (_free_thread_list.empty() == false) {
115 // Yes, get the index and remove it from the free list.
116 threadobject* t = _free_thread_list.front();
117 _free_thread_list.remove(t);
125 * Return a free thread index.
127 * @return free thread index
129 int32_t ThreadList::get_free_thread_index()
133 // Do we have free indexes in the free-list?
134 if (_free_index_list.empty() == false) {
135 // Yes, get the index and remove it from the free list.
136 index = _free_index_list.front();
137 _free_index_list.remove(index);
140 // Get a new the thread index.
141 index = _active_thread_list.size() + 1;
149 * Return the number of non-daemon threads.
151 * NOTE: This function does a linear-search over the threads list,
152 * because it is only used for joining the threads.
154 * @return number of non daemon threads
156 int32_t ThreadList::get_number_of_non_daemon_threads(void)
162 for (List<threadobject*>::iterator it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
163 threadobject* t = *it;
165 if (!thread_is_daemon(t))
176 * Return the thread object with the given index.
178 * @return thread object
180 threadobject* ThreadList::get_thread_by_index(int32_t index)
184 List<threadobject*>::iterator it = find_if(_active_thread_list.begin(), _active_thread_list.end(), std::bind2nd(comparator(), index));
187 if (it == _active_thread_list.end()) {
192 threadobject* t = *it;
194 // The thread found is in state new.
195 if (t->state == THREAD_STATE_NEW) {
206 * Return the Java thread object from the given thread object.
208 * @return Java thread object
210 threadobject* ThreadList::get_thread_from_java_object(java_handle_t* h)
212 List<threadobject*>::iterator it;
218 for (it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
221 LLNI_equals(t->object, h, equal);
236 * Release the thread.
238 * @return free thread index
240 void ThreadList::release_thread(threadobject* t)
244 // Move thread from active thread list to free thread list.
245 remove_from_active_thread_list(t);
246 add_to_free_thread_list(t);
248 // Add thread index to free index list.
249 add_to_free_index_list(t->index);
255 /* C interface functions ******************************************************/
258 void ThreadList_lock() { ThreadList::lock(); }
259 void ThreadList_unlock() { ThreadList::unlock(); }
260 void ThreadList_dump_threads() { ThreadList::dump_threads(); }
261 void ThreadList_release_thread(threadobject* t) { ThreadList::release_thread(t); }
262 threadobject* ThreadList_get_free_thread() { return ThreadList::get_free_thread(); }
263 int32_t ThreadList_get_free_thread_index() { return ThreadList::get_free_thread_index(); }
264 void ThreadList_add_to_active_thread_list(threadobject* t) { ThreadList::add_to_active_thread_list(t); }
265 threadobject* ThreadList_get_thread_by_index(int32_t index) { return ThreadList::get_thread_by_index(index); }
266 threadobject* ThreadList_get_main_thread() { return ThreadList::get_main_thread(); }
267 threadobject* ThreadList_get_thread_from_java_object(java_handle_t* h) { return ThreadList::get_thread_from_java_object(h); }
269 int32_t ThreadList_get_number_of_non_daemon_threads() { return ThreadList::get_number_of_non_daemon_threads(); }
273 * These are local overrides for various environment variables in Emacs.
274 * Please do not remove this and leave it at the end of the file, where
275 * Emacs will automagically detect them.
276 * ---------------------------------------------------------------------
279 * indent-tabs-mode: t
283 * vim:noexpandtab:sw=4:ts=4: