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