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