CACAO
threadlist.hpp
Go to the documentation of this file.
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:
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.
51 
52  // Thread counter for internal usage.
53  int32_t _last_index;
54 
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  static ThreadList *get() { assert(the_threadlist); return the_threadlist; }
63 
64  Mutex& mutex() { return _mutex; }
65 
66  void wait_cond(Condition *cond) { cond->wait(_mutex); }
67 
69 
70  // Thread management methods.
72  void get_free_thread(threadobject **t, int32_t *index);
75  void release_thread(threadobject* t, bool needs_deactivate);
77 
78  // Thread listing methods.
81 
82  // Thread counting methods visible to Java.
88 
89  // Thread counting methods for internal use.
92 
93  // Debugging methods.
94  void dump_threads();
95 };
96 
98  _number_of_started_java_threads(0),
99  _number_of_active_java_threads(0),
100  _peak_of_active_java_threads(0),
101  _last_index(0)
102 {
103 }
104 
106 {
107  assert(!the_threadlist);
109 }
110 
112 {
113  MutexLocker(mutex());
114 
115  _active_thread_list.push_back(t);
116  t->is_in_active_list = true;
117 
118  // Update counter variables.
119  if ((t->flags & THREAD_FLAG_INTERNAL) == 0) {
123  }
124 }
125 
127 {
128  MutexLocker(mutex());
129 
130  _active_thread_list.remove(t);
131  t->is_in_active_list = false;
132 
133  // Update counter variables.
134  if ((t->flags & THREAD_FLAG_INTERNAL) == 0) {
136  }
137 }
138 
140 {
141  MutexLocker(mutex());
142 
143  return _active_thread_list.front();
144 }
145 
147 {
148  MutexLocker(mutex());
149 
150  return _active_thread_list.size();
151 }
152 
154 {
155  MutexLocker(mutex());
156 
158 }
159 
161 {
162  MutexLocker(mutex());
163 
165 }
166 
168 {
169  MutexLocker(mutex());
170 
172 }
173 
175 {
176  MutexLocker(mutex());
177 
179 }
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  */
int32_t _peak_of_active_java_threads
Definition: threadlist.hpp:50
std::size_t index
void dump_threads()
Dumps info for all threads running in the VM.
Definition: threadlist.cpp:47
threadobject * get_thread_by_index(int32_t index)
Return the thread object with the given index.
Definition: threadlist.cpp:224
int32_t get_number_of_active_threads()
Definition: threadlist.hpp:146
void get_active_java_threads(List< threadobject * > &list)
Fills the passed list with all currently active threads which should be visible to Java...
Definition: threadlist.cpp:111
int32_t _number_of_active_java_threads
Definition: threadlist.hpp:49
Mutex & mutex()
Definition: threadlist.hpp:64
threadobject * get_main_thread()
Definition: threadlist.hpp:139
Dummy implementation of a mutex.
Definition: mutex-none.hpp:33
int32_t _number_of_started_java_threads
Definition: threadlist.hpp:48
int32_t get_number_of_daemon_java_threads()
Return the number of daemon threads visible to Java.
Definition: threadlist.cpp:164
Mutex _mutex
Definition: threadlist.hpp:42
List< threadobject * > _free_thread_list
Definition: threadlist.hpp:45
int32_t get_number_of_non_daemon_threads()
Return the number of non-daemon threads.
Definition: threadlist.cpp:195
static ThreadList * the_threadlist
Definition: threadlist.hpp:40
void release_thread(threadobject *t, bool needs_deactivate)
Release the thread.
Definition: threadlist.cpp:282
void remove_from_active_thread_list(threadobject *t)
Definition: threadlist.hpp:126
static void create_object()
Supposed to be called exactly once, early during initialization.
Definition: threadlist.hpp:105
int32_t _last_index
Definition: threadlist.hpp:53
void reset_peak_of_active_java_threads()
Definition: threadlist.hpp:174
int32_t get_peak_of_active_java_threads()
Definition: threadlist.hpp:167
void wait_cond(Condition *cond)
Definition: threadlist.hpp:66
void get_active_threads(List< threadobject * > &list)
Fills the passed list with all currently active threads.
Definition: threadlist.cpp:95
void deactivate_thread(threadobject *t)
Definition: threadlist.cpp:269
int32_t get_number_of_active_java_threads()
Definition: threadlist.hpp:160
Dummy condition variable.
void add_to_active_thread_list(threadobject *t)
Definition: threadlist.hpp:111
void get_free_thread(threadobject **t, int32_t *index)
Get the next free thread object.
Definition: threadlist.cpp:138
#define MAX(a, b)
Definition: global.hpp:99
threadobject * get_thread_from_java_object(java_handle_t *h)
Return the Java thread object from the given thread object.
Definition: threadlist.cpp:251
bool is_in_active_list
Definition: thread.hpp:100
int32_t get_number_of_started_java_threads()
Definition: threadlist.hpp:153
AnyObjLocker< Mutex > MutexLocker
Definition: mutex.hpp:60
List< threadobject * > _active_thread_list
Definition: threadlist.hpp:44
void wait(Mutex *mutex)
Waits for the condition variable.