1 /* src/threads/threadlist.hpp - different thread-lists
3 Copyright (C) 2008, 2009
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
26 #ifndef _THREADLIST_HPP
27 #define _THREADLIST_HPP
33 #include "threads/thread.hpp"
35 #include "toolbox/list.hpp"
37 #include "vm/global.h"
40 /* ThreadList *****************************************************************/
46 static Mutex _mutex; // a mutex for all thread lists
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
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;
57 // Thread counters for internal usage.
58 static int32_t _number_of_non_daemon_threads;
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);
66 class comparator : public std::binary_function<threadobject*, int32_t, bool> {
68 bool operator() (const threadobject* t, const int32_t index) const
70 return (t->index == index);
75 static void lock() { _mutex.lock(); }
76 static void unlock() { _mutex.unlock(); }
79 static void add_to_active_thread_list(threadobject* t);
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);
89 // Thread listing methods.
90 static void get_active_threads(List<threadobject*> &list);
91 static void get_active_java_threads(List<threadobject*> &list);
93 // Thread counting methods visible to Java.
94 static int32_t get_number_of_started_java_threads();
95 static int32_t get_number_of_active_java_threads();
96 static int32_t get_number_of_daemon_java_threads();
97 static int32_t get_peak_of_active_java_threads();
98 static void reset_peak_of_active_java_threads();
100 // Thread counting methods for internal use.
101 static int32_t get_number_of_active_threads();
102 static int32_t get_number_of_non_daemon_threads();
104 // Debugging methods.
105 static void dump_threads();
108 struct ThreadListLocker {
109 ThreadListLocker() { ThreadList::lock(); }
110 ~ThreadListLocker() { ThreadList::unlock(); }
114 inline void ThreadList::add_to_active_thread_list(threadobject* t)
116 _active_thread_list.push_back(t);
118 // Update counter variables.
119 if ((t->flags & THREAD_FLAG_INTERNAL) == 0) {
120 _number_of_started_java_threads++;
121 _number_of_active_java_threads++;
122 _peak_of_active_java_threads = MAX(_peak_of_active_java_threads, _number_of_active_java_threads);
126 inline void ThreadList::remove_from_active_thread_list(threadobject* t)
128 _active_thread_list.remove(t);
130 // Update counter variables.
131 if ((t->flags & THREAD_FLAG_INTERNAL) == 0) {
132 _number_of_active_java_threads--;
136 inline void ThreadList::add_to_free_thread_list(threadobject* t)
138 _free_thread_list.push_back(t);
141 inline void ThreadList::add_to_free_index_list(int32_t index)
143 _free_index_list.push_back(index);
146 inline threadobject* ThreadList::get_main_thread()
148 return _active_thread_list.front();
151 inline int32_t ThreadList::get_number_of_active_threads()
153 return _active_thread_list.size();
156 inline int32_t ThreadList::get_number_of_started_java_threads()
158 return _number_of_started_java_threads;
161 inline int32_t ThreadList::get_number_of_active_java_threads()
163 return _number_of_active_java_threads;
166 inline int32_t ThreadList::get_peak_of_active_java_threads()
168 return _peak_of_active_java_threads;
171 inline void ThreadList::reset_peak_of_active_java_threads()
173 _peak_of_active_java_threads = _number_of_active_java_threads;
178 typedef struct ThreadList ThreadList;
180 void ThreadList_lock();
181 void ThreadList_unlock();
182 void ThreadList_dump_threads();
183 void ThreadList_release_thread(threadobject* t);
184 threadobject* ThreadList_get_free_thread();
185 int32_t ThreadList_get_free_thread_index();
186 void ThreadList_add_to_active_thread_list(threadobject* t);
187 threadobject* ThreadList_get_thread_by_index(int32_t index);
188 threadobject* ThreadList_get_main_thread();
189 threadobject* ThreadList_get_thread_from_java_object(java_handle_t* h);
190 int32_t ThreadList_get_number_of_non_daemon_threads();
194 #endif // _THREADLIST_HPP
198 * These are local overrides for various environment variables in Emacs.
199 * Please do not remove this and leave it at the end of the file, where
200 * Emacs will automagically detect them.
201 * ---------------------------------------------------------------------
204 * indent-tabs-mode: t
208 * vim:noexpandtab:sw=4:ts=4: