Line data Source code
1 : /* src/threads/threadlist.hpp - thread list maintenance
2 :
3 : Copyright (C) 1996-2014
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 : #include <stdint.h>
31 : #include "threads/condition.hpp"
32 : #include "threads/thread.hpp"
33 : #include "toolbox/list.hpp"
34 : #include "vm/global.hpp"
35 :
36 : /* ThreadList *****************************************************************/
37 :
38 : class ThreadList {
39 : private:
40 : static ThreadList *the_threadlist;
41 :
42 : Mutex _mutex; // a mutex for all thread lists
43 :
44 : List<threadobject*> _active_thread_list; // list of active threads
45 : List<threadobject*> _free_thread_list; // list of free threads
46 :
47 : // Thread counters visible to Java.
48 : int32_t _number_of_started_java_threads;
49 : int32_t _number_of_active_java_threads;
50 : int32_t _peak_of_active_java_threads;
51 :
52 : // Thread counter for internal usage.
53 : int32_t _last_index;
54 :
55 : void remove_from_active_thread_list(threadobject* t);
56 : public:
57 : ThreadList();
58 :
59 : /// Supposed to be called exactly once, early during initialization.
60 : static void create_object();
61 : /// Provides access to singleton.
62 2580 : static ThreadList *get() { assert(the_threadlist); return the_threadlist; }
63 :
64 413 : Mutex& mutex() { return _mutex; }
65 :
66 0 : void wait_cond(Condition *cond) { cond->wait(_mutex); }
67 :
68 : void add_to_active_thread_list(threadobject* t);
69 :
70 : // Thread management methods.
71 : threadobject* get_main_thread();
72 : void get_free_thread(threadobject **t, int32_t *index);
73 : threadobject* get_thread_by_index(int32_t index);
74 : threadobject* get_thread_from_java_object(java_handle_t* h);
75 : void release_thread(threadobject* t, bool needs_deactivate);
76 : void deactivate_thread(threadobject *t);
77 :
78 : // Thread listing methods.
79 : void get_active_threads(List<threadobject*> &list);
80 : void get_active_java_threads(List<threadobject*> &list);
81 :
82 : // Thread counting methods visible to Java.
83 : int32_t get_number_of_started_java_threads();
84 : int32_t get_number_of_active_java_threads();
85 : int32_t get_number_of_daemon_java_threads();
86 : int32_t get_peak_of_active_java_threads();
87 : void reset_peak_of_active_java_threads();
88 :
89 : // Thread counting methods for internal use.
90 : int32_t get_number_of_active_threads();
91 : int32_t get_number_of_non_daemon_threads();
92 :
93 : // Debugging methods.
94 : void dump_threads();
95 : };
96 :
97 163 : inline ThreadList::ThreadList():
98 : _number_of_started_java_threads(0),
99 : _number_of_active_java_threads(0),
100 : _peak_of_active_java_threads(0),
101 163 : _last_index(0)
102 : {
103 163 : }
104 :
105 163 : inline void ThreadList::create_object()
106 : {
107 163 : assert(!the_threadlist);
108 163 : the_threadlist = new ThreadList;
109 163 : }
110 :
111 793 : inline void ThreadList::add_to_active_thread_list(threadobject* t)
112 : {
113 : MutexLocker(mutex());
114 :
115 793 : _active_thread_list.push_back(t);
116 793 : t->is_in_active_list = true;
117 :
118 : // Update counter variables.
119 793 : if ((t->flags & THREAD_FLAG_INTERNAL) == 0) {
120 304 : _number_of_started_java_threads++;
121 304 : _number_of_active_java_threads++;
122 304 : _peak_of_active_java_threads = MAX(_peak_of_active_java_threads, _number_of_active_java_threads);
123 : }
124 793 : }
125 :
126 282 : inline void ThreadList::remove_from_active_thread_list(threadobject* t)
127 : {
128 : MutexLocker(mutex());
129 :
130 282 : _active_thread_list.remove(t);
131 282 : t->is_in_active_list = false;
132 :
133 : // Update counter variables.
134 282 : if ((t->flags & THREAD_FLAG_INTERNAL) == 0) {
135 141 : _number_of_active_java_threads--;
136 : }
137 282 : }
138 :
139 163 : inline threadobject* ThreadList::get_main_thread()
140 : {
141 : MutexLocker(mutex());
142 :
143 163 : return _active_thread_list.front();
144 : }
145 :
146 : inline int32_t ThreadList::get_number_of_active_threads()
147 : {
148 : MutexLocker(mutex());
149 :
150 : return _active_thread_list.size();
151 : }
152 :
153 0 : inline int32_t ThreadList::get_number_of_started_java_threads()
154 : {
155 : MutexLocker(mutex());
156 :
157 0 : return _number_of_started_java_threads;
158 : }
159 :
160 : inline int32_t ThreadList::get_number_of_active_java_threads()
161 : {
162 : MutexLocker(mutex());
163 :
164 : return _number_of_active_java_threads;
165 : }
166 :
167 0 : inline int32_t ThreadList::get_peak_of_active_java_threads()
168 : {
169 : MutexLocker(mutex());
170 :
171 0 : return _peak_of_active_java_threads;
172 : }
173 :
174 0 : inline void ThreadList::reset_peak_of_active_java_threads()
175 : {
176 : MutexLocker(mutex());
177 :
178 0 : _peak_of_active_java_threads = _number_of_active_java_threads;
179 0 : }
180 :
181 : #endif // _THREADLIST_HPP
182 :
183 :
184 : /*
185 : * These are local overrides for various environment variables in Emacs.
186 : * Please do not remove this and leave it at the end of the file, where
187 : * Emacs will automagically detect them.
188 : * ---------------------------------------------------------------------
189 : * Local variables:
190 : * mode: c++
191 : * indent-tabs-mode: t
192 : * c-basic-offset: 4
193 : * tab-width: 4
194 : * End:
195 : * vim:noexpandtab:sw=4:ts=4:
196 : */
|