* src/threads/threadlist.cpp (ThreadList::get_thread_by_index): Fixed
[cacao.git] / src / threads / threadlist.cpp
1 /* src/threads/threadlist.cpp - thread list
2
3    Copyright (C) 2008
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.h"
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_non_daemon_threads;
51
52
53 /**
54  * Dumps info for all threads running in the VM.  This function is
55  * called when SIGQUIT (<ctrl>-\) is sent to the VM.
56  */
57 void ThreadList::dump_threads()
58 {
59         // XXX we should stop the world here
60         // Lock the thread lists.
61         lock();
62
63         printf("Full thread dump CACAO "VERSION":\n");
64
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;
68
69                 // Ignore threads which are in state NEW.
70                 if (t->state == THREAD_STATE_NEW)
71                         continue;
72
73 #if defined(ENABLE_GC_CACAO)
74                 /* Suspend the thread. */
75                 /* XXX Is the suspend reason correct? */
76
77                 if (threads_suspend_thread(t, SUSPEND_REASON_JNI) == false)
78                         vm_abort("threads_dump: threads_suspend_thread failed");
79 #endif
80
81                 /* Print thread info. */
82
83                 printf("\n");
84                 thread_print_info(t);
85                 printf("\n");
86
87                 /* Print trace of thread. */
88
89                 stacktrace_print_of_thread(t);
90
91 #if defined(ENABLE_GC_CACAO)
92                 /* Resume the thread. */
93
94                 if (threads_resume_thread(t) == false)
95                         vm_abort("threads_dump: threads_resume_thread failed");
96 #endif
97         }
98
99         // Unlock the thread lists.
100         unlock();
101 }
102
103
104 /**
105  * Return a free thread object.
106  *
107  * @return free thread object or NULL if none available
108  */
109 threadobject* ThreadList::get_free_thread()
110 {
111         threadobject* t = NULL;
112
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);
118         }
119
120         return t;
121 }
122
123
124 /**
125  * Return a free thread index.
126  *
127  * @return free thread index
128  */
129 int32_t ThreadList::get_free_thread_index()
130 {
131         int32_t index;
132
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);
138         }
139         else {
140                 // Get a new the thread index.
141                 index = _active_thread_list.size() + 1;
142         }
143
144         return index;
145 }
146
147
148 /**
149  * Return the number of non-daemon threads.
150  *
151  * NOTE: This function does a linear-search over the threads list,
152  *       because it is only used for joining the threads.
153  *
154  * @return number of non daemon threads
155  */
156 int32_t ThreadList::get_number_of_non_daemon_threads(void)
157 {
158         int nondaemons = 0;
159
160         lock();
161
162         for (List<threadobject*>::iterator it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
163                 threadobject* t = *it;
164
165                 if (!thread_is_daemon(t))
166                         nondaemons++;
167         }
168
169         unlock();
170
171         return nondaemons;
172 }
173
174
175 /**
176  * Return the thread object with the given index.
177  *
178  * @return thread object
179  */
180
181 class foo : public std::binary_function<threadobject*, int32_t, bool> {
182 public:
183         bool operator() (const threadobject* t, const int32_t index) const
184         {
185                 return (t->index == index);
186         }
187 };
188
189 threadobject* ThreadList::get_thread_by_index(int32_t index)
190 {
191         lock();
192
193         List<threadobject*>::iterator it = find_if(_active_thread_list.begin(), _active_thread_list.end(), std::bind2nd(foo(), index));
194
195         // No thread found.
196         if (it == _active_thread_list.end()) {
197                 unlock();
198                 return NULL;
199         }
200
201         threadobject* t = *it;
202
203         // The thread found is in state new.
204         if (t->state == THREAD_STATE_NEW) {
205                 unlock();
206                 return NULL;
207         }
208
209         unlock();
210         return t;
211 }
212
213
214 /**
215  * Return the Java thread object from the given thread object.
216  *
217  * @return Java thread object
218  */
219 threadobject* ThreadList::get_thread_from_java_object(java_handle_t* h)
220 {
221         List<threadobject*>::iterator it;
222         threadobject* t;
223         bool          equal;
224
225         lock();
226
227         for (it = _active_thread_list.begin(); it != _active_thread_list.end(); it++) {
228                 t = *it;
229
230                 LLNI_equals(t->object, h, equal);
231
232                 if (equal == true) {
233                         unlock();
234                         return t;
235                 }
236         }
237
238         unlock();
239
240         return NULL;
241 }
242
243
244 /**
245  * Release the thread.
246  *
247  * @return free thread index
248  */
249 void ThreadList::release_thread(threadobject* t)
250 {
251         lock();
252
253         // Move thread from active thread list to free thread list.
254         remove_from_active_thread_list(t);
255         add_to_free_thread_list(t);
256
257         // Add thread index to free index list.
258         add_to_free_index_list(t->index);
259
260         unlock();
261 }
262
263
264 /* C interface functions ******************************************************/
265
266 extern "C" {
267         void ThreadList_lock() { ThreadList::lock(); }
268         void ThreadList_unlock() { ThreadList::unlock(); }
269         void ThreadList_dump_threads() { ThreadList::dump_threads(); }
270         void ThreadList_release_thread(threadobject* t) { ThreadList::release_thread(t); }
271         threadobject* ThreadList_get_free_thread() { return ThreadList::get_free_thread(); }
272         int32_t ThreadList_get_free_thread_index() { return ThreadList::get_free_thread_index(); }
273         void ThreadList_add_to_active_thread_list(threadobject* t) { ThreadList::add_to_active_thread_list(t); }
274         threadobject* ThreadList_get_thread_by_index(int32_t index) { return ThreadList::get_thread_by_index(index); }
275         threadobject* ThreadList_get_main_thread() { return ThreadList::get_main_thread(); }
276         threadobject* ThreadList_get_thread_from_java_object(java_handle_t* h) { return ThreadList::get_thread_from_java_object(h); }
277
278         int32_t ThreadList_get_number_of_non_daemon_threads() { return ThreadList::get_number_of_non_daemon_threads(); }
279 }
280
281 /*
282  * These are local overrides for various environment variables in Emacs.
283  * Please do not remove this and leave it at the end of the file, where
284  * Emacs will automagically detect them.
285  * ---------------------------------------------------------------------
286  * Local variables:
287  * mode: c++
288  * indent-tabs-mode: t
289  * c-basic-offset: 4
290  * tab-width: 4
291  * End:
292  * vim:noexpandtab:sw=4:ts=4:
293  */