* src/threads/threadlist.cpp: Fixed strategy for giving out thread indexes.
[cacao.git] / src / threads / threadlist.hpp
1 /* src/threads/threadlist.hpp - different thread-lists
2
3    Copyright (C) 1996-2011
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 #ifndef _THREADLIST_HPP
27 #define _THREADLIST_HPP
28
29 #include "config.h"
30
31 #include <stdint.h>
32
33 #include "threads/thread.hpp"
34
35 #include "toolbox/list.hpp"
36
37 #include "vm/global.h"
38
39
40 /* ThreadList *****************************************************************/
41
42 #ifdef __cplusplus
43
44 class ThreadList {
45 private:
46         static Mutex               _mutex;              // a mutex for all thread lists
47
48         static List<threadobject*> _active_thread_list; // list of active threads
49         static List<threadobject*> _free_thread_list;   // list of free threads
50         static List<int32_t>       _free_index_list;    // list of free thread indexes
51
52         // Thread counters visible to Java.
53         static int32_t             _number_of_started_java_threads;
54         static int32_t             _number_of_active_java_threads;
55         static int32_t             _peak_of_active_java_threads;
56
57         // Thread counters for internal usage.
58         static int32_t             _number_of_non_daemon_threads;
59
60         static int32_t             _last_index;
61
62         static void                 remove_from_active_thread_list(threadobject* t);
63         static void                 add_to_free_thread_list(threadobject* t);
64         static void                 add_to_free_index_list(int32_t index);
65
66 private:
67         // Comparator class.
68         class comparator : public std::binary_function<threadobject*, int32_t, bool> {
69         public:
70                 bool operator() (const threadobject* t, const int32_t index) const
71                 {
72                         return (t->index == index);
73                 }
74         };
75
76 public:
77         static void                 lock()   { _mutex.lock(); }
78         static void                 unlock() { _mutex.unlock(); }
79         static void                 wait_cond(Condition *cond) { cond->wait(_mutex); }
80
81         static void                 add_to_active_thread_list(threadobject* t);
82
83         // Thread management methods.
84         static threadobject*        get_main_thread();
85         static threadobject*        get_free_thread();
86         static int32_t              get_free_thread_index();
87         static threadobject*        get_thread_by_index(int32_t index);
88         static threadobject*        get_thread_from_java_object(java_handle_t* h);
89         static void                 release_thread(threadobject* t, bool needs_deactivate);
90         static void                 deactivate_thread(threadobject *t);
91
92         // Thread listing methods.
93         static void                 get_active_threads(List<threadobject*> &list);
94         static void                 get_active_java_threads(List<threadobject*> &list);
95
96         // Thread counting methods visible to Java.
97         static int32_t              get_number_of_started_java_threads();
98         static int32_t              get_number_of_active_java_threads();
99         static int32_t              get_number_of_daemon_java_threads();
100         static int32_t              get_peak_of_active_java_threads();
101         static void                 reset_peak_of_active_java_threads();
102
103         // Thread counting methods for internal use.
104         static int32_t              get_number_of_active_threads();
105         static int32_t              get_number_of_non_daemon_threads();
106
107         // Debugging methods.
108         static void                 dump_threads();
109 };
110
111 struct ThreadListLocker {
112         ThreadListLocker()  { ThreadList::lock(); }
113         ~ThreadListLocker()  { ThreadList::unlock(); }
114 };
115
116
117 inline void ThreadList::add_to_active_thread_list(threadobject* t)
118 {
119         lock();
120         _active_thread_list.push_back(t);
121         t->is_in_active_list = true;
122
123         // Update counter variables.
124         if ((t->flags & THREAD_FLAG_INTERNAL) == 0) {
125                 _number_of_started_java_threads++;
126                 _number_of_active_java_threads++;
127                 _peak_of_active_java_threads = MAX(_peak_of_active_java_threads, _number_of_active_java_threads);
128         }
129         unlock();
130 }
131
132 inline void ThreadList::remove_from_active_thread_list(threadobject* t)
133 {
134         lock();
135         _active_thread_list.remove(t);
136         t->is_in_active_list = false;
137
138         // Update counter variables.
139         if ((t->flags & THREAD_FLAG_INTERNAL) == 0) {
140                 _number_of_active_java_threads--;
141         }
142         unlock();
143 }
144
145 inline void ThreadList::add_to_free_thread_list(threadobject* t)
146 {
147         lock();
148         _free_thread_list.push_back(t);
149         unlock();
150 }
151
152 inline void ThreadList::add_to_free_index_list(int32_t index)
153 {
154         lock();
155         _free_index_list.push_back(index);
156         unlock();
157 }
158
159 inline threadobject* ThreadList::get_main_thread()
160 {
161         lock();
162         threadobject *r = _active_thread_list.front();
163         unlock();
164         return r;
165 }
166
167 inline int32_t ThreadList::get_number_of_active_threads()
168 {
169         lock();
170         int32_t size = _active_thread_list.size();
171         unlock();
172         return size;
173 }
174
175 inline int32_t ThreadList::get_number_of_started_java_threads()
176 {
177         lock();
178         int32_t num = _number_of_started_java_threads;
179         unlock();
180         return num;
181 }
182
183 inline int32_t ThreadList::get_number_of_active_java_threads()
184 {
185         lock();
186         int32_t num = _number_of_active_java_threads;
187         unlock();
188         return num;
189 }
190
191 inline int32_t ThreadList::get_peak_of_active_java_threads()
192 {
193         lock();
194         int32_t num = _peak_of_active_java_threads;
195         unlock();
196         return num;
197 }
198
199 inline void ThreadList::reset_peak_of_active_java_threads()
200 {
201         lock();
202         _peak_of_active_java_threads = _number_of_active_java_threads;
203         unlock();
204 }
205
206 #else
207
208 typedef struct ThreadList ThreadList;
209
210 void ThreadList_lock();
211 void ThreadList_unlock();
212 void ThreadList_dump_threads();
213 threadobject* ThreadList_get_free_thread();
214 int32_t ThreadList_get_free_thread_index();
215 void ThreadList_add_to_active_thread_list(threadobject* t);
216 threadobject* ThreadList_get_thread_by_index(int32_t index);
217 threadobject* ThreadList_get_main_thread();
218 threadobject* ThreadList_get_thread_from_java_object(java_handle_t* h);
219 int32_t ThreadList_get_number_of_non_daemon_threads();
220
221 #endif
222
223 #endif // _THREADLIST_HPP
224
225
226 /*
227  * These are local overrides for various environment variables in Emacs.
228  * Please do not remove this and leave it at the end of the file, where
229  * Emacs will automagically detect them.
230  * ---------------------------------------------------------------------
231  * Local variables:
232  * mode: c++
233  * indent-tabs-mode: t
234  * c-basic-offset: 4
235  * tab-width: 4
236  * End:
237  * vim:noexpandtab:sw=4:ts=4:
238  */