* configure.ac: Bump version, append hg revision.
[cacao.git] / src / threads / threadlist.cpp
1 /* src/threads/threadlist.cpp - thread list
2
3    Copyright (C) 2008, 2009
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
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.
12
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.
17
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
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29
30 #include <algorithm>
31
32 #include "threads/mutex.hpp"
33 #include "threads/threadlist.hpp"
34 #include "threads/thread.hpp"
35
36 #include "toolbox/list.hpp"
37 #include "toolbox/logging.hpp"
38
39 #include "vm/jit/stacktrace.hpp"
40
41
42 /* class variables */
43
44 Mutex               ThreadList::_mutex;                // a mutex for all thread lists
45
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
49
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;
54
55
56 /**
57  * Dumps info for all threads running in the VM.  This function is
58  * called when SIGQUIT (<ctrl>-\) is sent to the VM.
59  */
60 void ThreadList::dump_threads()
61 {
62         // XXX we should stop the world here and remove explicit
63         //     thread suspension from the loop below.
64         // Lock the thread lists.
65         lock();
66
67         printf("Full thread dump CACAO "VERSION_FULL":\n");
68
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;
73
74                 // Ignore threads which are in state NEW.
75                 if (t->state == THREAD_STATE_NEW)
76                         continue;
77
78                 /* Suspend the thread (and ignore return value). */
79
80                 if (t != self)
81                         (void) threads_suspend_thread(t, SUSPEND_REASON_DUMP);
82
83                 /* Print thread info. */
84
85                 printf("\n");
86                 thread_print_info(t);
87                 printf("\n");
88
89                 /* Print trace of thread. */
90
91                 stacktrace_print_of_thread(t);
92
93                 /* Resume the thread (and ignore return value). */
94
95                 if (t != self)
96                         (void) threads_resume_thread(t, SUSPEND_REASON_DUMP);
97         }
98
99         // Unlock the thread lists.
100         unlock();
101 }
102
103
104 /**
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.
108  *
109  * @param list list class to be filled
110  */
111 void ThreadList::get_active_threads(List<threadobject*> &list)
112 {
113         // Lock the thread lists.
114         lock();
115
116         // Use the assignment operator to create a copy of the thread list.
117         list = _active_thread_list;
118
119         // Unlock the thread lists.
120         unlock();
121 }
122
123
124 /**
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.
128  *
129  * @param list list class to be filled
130  */
131 void ThreadList::get_active_java_threads(List<threadobject*> &list)
132 {
133         // Lock the thread lists.
134         lock();
135
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;
139
140                 // We skip internal threads.
141                 if (t->flags & THREAD_FLAG_INTERNAL)
142                         continue;
143
144                 list.push_back(t);
145         }
146
147         // Unlock the thread lists.
148         unlock();
149 }
150
151
152 /**
153  * Return a free thread object.
154  *
155  * @return free thread object or NULL if none available
156  */
157 threadobject* ThreadList::get_free_thread()
158 {
159         threadobject* t = NULL;
160
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);
166         }
167
168         return t;
169 }
170
171
172 /**
173  * Return a free thread index.
174  *
175  * @return free thread index
176  */
177 int32_t ThreadList::get_free_thread_index()
178 {
179         int32_t index;
180
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);
186         }
187         else {
188                 // Get a new the thread index.
189                 index = _active_thread_list.size() + 1;
190         }
191
192         return index;
193 }
194
195
196 /**
197  * Return the number of daemon threads visible to Java.
198  *
199  * NOTE: This function does a linear-search over the threads list,
200  *       because it is only used by the management interface.
201  *
202  * @return number of daemon threads
203  */
204 int32_t ThreadList::get_number_of_daemon_java_threads(void)
205 {
206         int number = 0;
207
208         // Lock the thread lists.
209         lock();
210
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;
214
215                 // We skip internal threads.
216                 if (t->flags & THREAD_FLAG_INTERNAL)
217                         continue;
218
219                 if (thread_is_daemon(t))
220                         number++;
221         }
222
223         // Unlock the thread lists.
224         unlock();
225
226         return number;
227 }
228
229
230 /**
231  * Return the number of non-daemon threads.
232  *
233  * NOTE: This function does a linear-search over the threads list,
234  *       because it is only used for joining the threads.
235  *
236  * @return number of non daemon threads
237  */
238 int32_t ThreadList::get_number_of_non_daemon_threads(void)
239 {
240         int nondaemons = 0;
241
242         lock();
243
244         for (List<threadobject*>::iterator it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
245                 threadobject* t = *it;
246
247                 if (!thread_is_daemon(t))
248                         nondaemons++;
249         }
250
251         unlock();
252
253         return nondaemons;
254 }
255
256
257 /**
258  * Return the thread object with the given index.
259  *
260  * @return thread object
261  */
262 threadobject* ThreadList::get_thread_by_index(int32_t index)
263 {
264         lock();
265
266         List<threadobject*>::iterator it = find_if(_active_thread_list.begin(), _active_thread_list.end(), std::bind2nd(comparator(), index));
267
268         // No thread found.
269         if (it == _active_thread_list.end()) {
270                 unlock();
271                 return NULL;
272         }
273
274         threadobject* t = *it;
275
276         // The thread found is in state new.
277         if (t->state == THREAD_STATE_NEW) {
278                 unlock();
279                 return NULL;
280         }
281
282         unlock();
283         return t;
284 }
285
286
287 /**
288  * Return the Java thread object from the given thread object.
289  *
290  * @return Java thread object
291  */
292 threadobject* ThreadList::get_thread_from_java_object(java_handle_t* h)
293 {
294         List<threadobject*>::iterator it;
295         threadobject* t;
296         bool          equal;
297
298         lock();
299
300         for (it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
301                 t = *it;
302
303                 LLNI_equals(t->object, h, equal);
304
305                 if (equal == true) {
306                         unlock();
307                         return t;
308                 }
309         }
310
311         unlock();
312
313         return NULL;
314 }
315
316
317 /**
318  * Release the thread.
319  *
320  * @return free thread index
321  */
322 void ThreadList::release_thread(threadobject* t)
323 {
324         lock();
325
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);
329
330         // Add thread index to free index list.
331         add_to_free_index_list(t->index);
332
333         unlock();
334 }
335
336
337 /* C interface functions ******************************************************/
338
339 extern "C" {
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); }
350
351         int32_t ThreadList_get_number_of_non_daemon_threads() { return ThreadList::get_number_of_non_daemon_threads(); }
352 }
353
354 /*
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  * ---------------------------------------------------------------------
359  * Local variables:
360  * mode: c++
361  * indent-tabs-mode: t
362  * c-basic-offset: 4
363  * tab-width: 4
364  * End:
365  * vim:noexpandtab:sw=4:ts=4:
366  */