Merged with michi branch at rev 1684fe51cf3d.
[cacao.git] / src / threads / threadlist.hpp
1 /* src/threads/threadlist.hpp - different thread-lists
2
3    Copyright (C) 2008
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
38 /* ThreadList *****************************************************************/
39
40 #ifdef __cplusplus
41
42 using std::list;
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         static int32_t             _number_of_non_daemon_threads;
53
54         static void                 remove_from_active_thread_list(threadobject* t);
55         static void                 add_to_free_thread_list(threadobject* t);
56         static void                 add_to_free_index_list(int32_t index);
57
58 private:
59         // Comparator class.
60         class comparator : public std::binary_function<threadobject*, int32_t, bool> {
61         public:
62                 bool operator() (const threadobject* t, const int32_t index) const
63                 {
64                         return (t->index == index);
65                 }
66         };
67
68 public:
69         static void                 lock()   { _mutex.lock(); }
70         static void                 unlock() { _mutex.unlock(); }
71
72         // TODO make private
73         static void                 add_to_active_thread_list(threadobject* t);
74
75         static void                 dump_threads();
76         static void                 get_active_threads(list<threadobject*> &list);
77         static threadobject*        get_main_thread();
78         static threadobject*        get_free_thread();
79         static int32_t              get_free_thread_index();
80         static int32_t              get_number_of_non_daemon_threads();
81         static threadobject*        get_thread_by_index(int32_t index);
82         static threadobject*        get_thread_from_java_object(java_handle_t* h);
83         static void                 release_thread(threadobject* t);
84 };
85
86 struct ThreadListLocker {
87         ThreadListLocker()  { ThreadList::lock(); }
88         ~ThreadListLocker()  { ThreadList::unlock(); }
89 };
90
91
92 inline void ThreadList::add_to_active_thread_list(threadobject* t)
93 {
94         _active_thread_list.push_back(t);
95 }
96
97 inline void ThreadList::remove_from_active_thread_list(threadobject* t)
98 {
99         _active_thread_list.remove(t);
100 }
101
102 inline void ThreadList::add_to_free_thread_list(threadobject* t)
103 {
104         _free_thread_list.push_back(t);
105 }
106
107 inline void ThreadList::add_to_free_index_list(int32_t index)
108 {
109         _free_index_list.push_back(index);
110 }
111
112 inline threadobject* ThreadList::get_main_thread()
113 {
114         return _active_thread_list.front();
115 }
116
117 #else
118
119 typedef struct ThreadList ThreadList;
120
121 void ThreadList_lock();
122 void ThreadList_unlock();
123 void ThreadList_dump_threads();
124 void ThreadList_release_thread(threadobject* t);
125 threadobject* ThreadList_get_free_thread();
126 int32_t ThreadList_get_free_thread_index();
127 void ThreadList_add_to_active_thread_list(threadobject* t);
128 threadobject* ThreadList_get_thread_by_index(int32_t index);
129 threadobject* ThreadList_get_main_thread();
130 threadobject* ThreadList_get_thread_from_java_object(java_handle_t* h);
131 int32_t ThreadList_get_number_of_non_daemon_threads();
132
133 #endif
134
135 #endif // _THREADLIST_HPP
136
137
138 /*
139  * These are local overrides for various environment variables in Emacs.
140  * Please do not remove this and leave it at the end of the file, where
141  * Emacs will automagically detect them.
142  * ---------------------------------------------------------------------
143  * Local variables:
144  * mode: c++
145  * indent-tabs-mode: t
146  * c-basic-offset: 4
147  * tab-width: 4
148  * End:
149  * vim:noexpandtab:sw=4:ts=4:
150  */