* src/mm/boehm-gc/BCC_MAKEFILE, src/mm/boehm-gc/digimars.mak: unix2dos'ed
[cacao.git] / src / threads / threadlist.c
1 /* src/threads/threadlist.c - 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 #include "config.h"
27
28 #include <stdint.h>
29
30 #include "mm/memory.h"
31
32 #include "threads/threadlist.h"
33 #include "threads/threads-common.h"
34
35 #include "toolbox/list.h"
36
37 #include "vmcore/options.h"
38
39
40 /* global variables ***********************************************************/
41
42 static list_t *list_thread;                            /* global threads list */
43 static list_t *list_thread_free;                  /* global free threads list */
44 static list_t *list_thread_index_free;
45
46
47 typedef struct thread_index_t {
48         int32_t    index;
49         listnode_t linkage;
50 } thread_index_t;
51
52
53 /* threadlist_init *************************************************************
54
55    Initialize thread-lists.
56
57 *******************************************************************************/
58
59 void threadlist_init(void)
60 {
61         TRACESUBSYSTEMINITIALIZATION("threadlist_init");
62
63         /* Initialize the thread lists. */
64
65         list_thread            = list_create(OFFSET(threadobject, linkage));
66         list_thread_free       = list_create(OFFSET(threadobject, linkage_free));
67         list_thread_index_free = list_create(OFFSET(thread_index_t, linkage));
68 }
69
70
71 /* threadlist_add **************************************************************
72
73    Add the given threadobject as last entry to the thread list.
74
75    IN:
76        t ... threadobject to be added
77
78 *******************************************************************************/
79
80 void threadlist_add(threadobject *t)
81 {
82         list_add_last_unsynced(list_thread, t);
83 }
84
85
86 /* threadlist_remove ***********************************************************
87
88    Remove the given threadobject from the thread list.
89
90    IN:
91        t ... threadobject to be removed
92
93 *******************************************************************************/
94
95 void threadlist_remove(threadobject *t)
96 {
97         list_remove_unsynced(list_thread, t);
98 }
99
100
101 /* threadlist_first ************************************************************
102
103    Return the first entry in the thread list.
104
105    RETURN:
106        threadobject of the first entry
107
108 *******************************************************************************/
109
110 threadobject *threadlist_first(void)
111 {
112         threadobject *t;
113
114         t = list_first_unsynced(list_thread);
115
116         return t;
117 }
118
119
120 /* threads_list_next ***********************************************************
121
122    Return the next entry in the thread list.
123
124    IN:
125        t ... threadobject to get next thread of
126
127    RETURN:
128        threadobject of the next entry
129
130 *******************************************************************************/
131
132 threadobject *threadlist_next(threadobject *t)
133 {
134         threadobject *next;
135
136         next = list_next_unsynced(list_thread, t);
137
138         return next;
139 }
140
141
142 /* threadlist_free_add *********************************************************
143
144    Add the given threadobject as last entry to the free thread list.
145
146    IN:
147        t ... threadobject to be added
148
149 *******************************************************************************/
150
151 void threadlist_free_add(threadobject *t)
152 {
153         list_add_last_unsynced(list_thread_free, t);
154 }
155
156
157 /* threadlist_free_remove ******************************************************
158
159    Remove the given entry from the free thread list.
160
161    IN:
162        t ... threadobject to be removed
163
164 *******************************************************************************/
165
166 void threadlist_free_remove(threadobject *t)
167 {
168         list_remove_unsynced(list_thread_free, t);
169 }
170
171
172 /* threadlist_free_first *******************************************************
173
174    Return the first entry in the free thread list.
175
176    RETURN:
177        threadobject of the first free entry
178
179 *******************************************************************************/
180
181 threadobject *threadlist_free_first(void)
182 {
183         threadobject *t;
184
185         t = list_first_unsynced(list_thread_free);
186
187         return t;
188 }
189
190
191 /* threadlist_get_non_daemons **************************************************
192
193    Return the number of non-daemon threads.
194
195    NOTE: This function does a linear-search over the threads list,
196          because it's only used for joining the threads.
197
198 *******************************************************************************/
199
200 int threadlist_get_non_daemons(void)
201 {
202         threadobject *t;
203         int           nondaemons;
204
205         /* Lock the threads lists. */
206
207         threads_list_lock();
208
209         nondaemons = 0;
210
211         for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
212                 if (!(t->flags & THREAD_FLAG_DAEMON))
213                         nondaemons++;
214         }
215
216         /* Unlock the threads lists. */
217
218         threads_list_unlock();
219
220         return nondaemons;
221 }
222
223
224 /* threadlist_index_first ******************************************************
225
226    Return the first entry in the thread-index list.
227
228    RETURN VALUE:
229        thread-index structure
230
231 *******************************************************************************/
232
233 static inline thread_index_t *threadlist_index_first(void)
234 {
235         thread_index_t *ti;
236
237         ti = list_first_unsynced(list_thread_index_free);
238
239         return ti;
240 }
241
242
243 /* threadlist_index_add ********************************************************
244
245    Add the given thread-index to the thread-index free list.
246
247    IN:
248        i ... thread index
249
250 *******************************************************************************/
251
252 void threadlist_index_add(int index)
253 {
254         thread_index_t *ti;
255
256         ti = NEW(thread_index_t);
257
258 #if defined(ENABLE_STATISTICS)
259         if (opt_stat)
260                 size_thread_index_t += sizeof(thread_index_t);
261 #endif
262
263         /* Set the index in the structure. */
264
265         ti->index = index;
266
267         list_add_last_unsynced(list_thread_index_free, ti);
268 }
269
270
271 /* threadlist_index_remove *****************************************************
272
273    Remove the given thread-index from the thread-index list and free
274    the thread-index structure.
275
276    IN:
277        ti ... thread-index structure
278
279 *******************************************************************************/
280
281 static inline void threadlist_index_remove(thread_index_t *ti)
282 {
283         list_remove_unsynced(list_thread_index_free, ti);
284
285         FREE(ti, thread_index_t);
286
287 #if defined(ENABLE_STATISTICS)
288         if (opt_stat)
289                 size_thread_index_t -= sizeof(thread_index_t);
290 #endif
291 }
292
293
294 /* threadlist_get_free_index ***************************************************
295
296    Return a free thread index.
297
298    RETURN VALUE:
299        free thread index
300
301 *******************************************************************************/
302
303 int threadlist_get_free_index(void)
304 {
305         thread_index_t *ti;
306         int             index;
307
308         /* Try to get a thread index from the free-list. */
309
310         ti = threadlist_index_first();
311
312         /* Is a free thread index available? */
313
314         if (ti != NULL) {
315                 /* Yes, get the index and remove it from the free list. */
316
317                 index = ti->index;
318
319                 threadlist_index_remove(ti);
320         }
321         else {
322                 /* Get a new the thread index. */
323
324                 index = list_thread->size + 1;
325         }
326
327         return index;
328 }
329
330
331 /*
332  * These are local overrides for various environment variables in Emacs.
333  * Please do not remove this and leave it at the end of the file, where
334  * Emacs will automagically detect them.
335  * ---------------------------------------------------------------------
336  * Local variables:
337  * mode: c
338  * indent-tabs-mode: t
339  * c-basic-offset: 4
340  * tab-width: 4
341  * End:
342  * vim:noexpandtab:sw=4:ts=4:
343  */