1 /* src/threads/threadlist.c - different thread-lists
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
30 #include "mm/memory.h"
32 #include "threads/threadlist.h"
33 #include "threads/threads-common.h"
35 #include "toolbox/list.h"
37 #include "vmcore/options.h"
40 /* global variables ***********************************************************/
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;
47 typedef struct thread_index_t {
53 /* threadlist_init *************************************************************
55 Initialize thread-lists.
57 *******************************************************************************/
59 void threadlist_init(void)
61 TRACESUBSYSTEMINITIALIZATION("threadlist_init");
63 /* Initialize the thread lists. */
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));
69 /* Initialize the implementation specific parts. */
71 threadlist_impl_init();
75 /* threadlist_add **************************************************************
77 Add the given threadobject as last entry to the thread list.
80 t ... threadobject to be added
82 *******************************************************************************/
84 void threadlist_add(threadobject *t)
86 list_add_last_unsynced(list_thread, t);
90 /* threadlist_remove ***********************************************************
92 Remove the given threadobject from the thread list.
95 t ... threadobject to be removed
97 *******************************************************************************/
99 void threadlist_remove(threadobject *t)
101 list_remove_unsynced(list_thread, t);
105 /* threadlist_first ************************************************************
107 Return the first entry in the thread list.
110 threadobject of the first entry
112 *******************************************************************************/
114 threadobject *threadlist_first(void)
118 t = list_first_unsynced(list_thread);
124 /* threadlist_next *************************************************************
126 Return the next entry in the thread list.
129 t ... threadobject to get next thread of
132 threadobject of the next entry
134 *******************************************************************************/
136 threadobject *threadlist_next(threadobject *t)
140 next = list_next_unsynced(list_thread, t);
146 /* threadlist_free_add *********************************************************
148 Add the given threadobject as last entry to the free thread list.
151 t ... threadobject to be added
153 *******************************************************************************/
155 void threadlist_free_add(threadobject *t)
157 list_add_last_unsynced(list_thread_free, t);
161 /* threadlist_free_remove ******************************************************
163 Remove the given entry from the free thread list.
166 t ... threadobject to be removed
168 *******************************************************************************/
170 void threadlist_free_remove(threadobject *t)
172 list_remove_unsynced(list_thread_free, t);
176 /* threadlist_free_first *******************************************************
178 Return the first entry in the free thread list.
181 threadobject of the first free entry
183 *******************************************************************************/
185 threadobject *threadlist_free_first(void)
189 t = list_first_unsynced(list_thread_free);
195 /* threadlist_get_non_daemons **************************************************
197 Return the number of non-daemon threads.
199 NOTE: This function does a linear-search over the threads list,
200 because it's only used for joining the threads.
202 *******************************************************************************/
204 int threadlist_get_non_daemons(void)
209 /* Lock the thread lists. */
215 for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
216 if (!(t->flags & THREAD_FLAG_DAEMON))
220 /* Unlock the thread lists. */
228 /* threadlist_index_first ******************************************************
230 Return the first entry in the thread-index list.
233 thread-index structure
235 *******************************************************************************/
237 static inline thread_index_t *threadlist_index_first(void)
241 ti = list_first_unsynced(list_thread_index_free);
247 /* threadlist_index_add ********************************************************
249 Add the given thread-index to the thread-index free list.
254 *******************************************************************************/
256 void threadlist_index_add(int index)
260 ti = NEW(thread_index_t);
262 #if defined(ENABLE_STATISTICS)
264 size_thread_index_t += sizeof(thread_index_t);
267 /* Set the index in the structure. */
271 list_add_last_unsynced(list_thread_index_free, ti);
275 /* threadlist_index_remove *****************************************************
277 Remove the given thread-index from the thread-index list and free
278 the thread-index structure.
281 ti ... thread-index structure
283 *******************************************************************************/
285 static inline void threadlist_index_remove(thread_index_t *ti)
287 list_remove_unsynced(list_thread_index_free, ti);
289 FREE(ti, thread_index_t);
291 #if defined(ENABLE_STATISTICS)
293 size_thread_index_t -= sizeof(thread_index_t);
298 /* threadlist_get_free_index ***************************************************
300 Return a free thread index.
305 *******************************************************************************/
307 int threadlist_get_free_index(void)
312 /* Try to get a thread index from the free-list. */
314 ti = threadlist_index_first();
316 /* Is a free thread index available? */
319 /* Yes, get the index and remove it from the free list. */
323 threadlist_index_remove(ti);
326 /* Get a new the thread index. */
328 index = list_thread->size + 1;
336 * These are local overrides for various environment variables in Emacs.
337 * Please do not remove this and leave it at the end of the file, where
338 * Emacs will automagically detect them.
339 * ---------------------------------------------------------------------
342 * indent-tabs-mode: t
346 * vim:noexpandtab:sw=4:ts=4: