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));
71 /* threadlist_add **************************************************************
73 Add the given threadobject as last entry to the thread list.
76 t ... threadobject to be added
78 *******************************************************************************/
80 void threadlist_add(threadobject *t)
82 list_add_last_unsynced(list_thread, t);
86 /* threadlist_remove ***********************************************************
88 Remove the given threadobject from the thread list.
91 t ... threadobject to be removed
93 *******************************************************************************/
95 void threadlist_remove(threadobject *t)
97 list_remove_unsynced(list_thread, t);
101 /* threadlist_first ************************************************************
103 Return the first entry in the thread list.
106 threadobject of the first entry
108 *******************************************************************************/
110 threadobject *threadlist_first(void)
114 t = list_first_unsynced(list_thread);
120 /* threads_list_next ***********************************************************
122 Return the next entry in the thread list.
125 t ... threadobject to get next thread of
128 threadobject of the next entry
130 *******************************************************************************/
132 threadobject *threadlist_next(threadobject *t)
136 next = list_next_unsynced(list_thread, t);
142 /* threadlist_free_add *********************************************************
144 Add the given threadobject as last entry to the free thread list.
147 t ... threadobject to be added
149 *******************************************************************************/
151 void threadlist_free_add(threadobject *t)
153 list_add_last_unsynced(list_thread_free, t);
157 /* threadlist_free_remove ******************************************************
159 Remove the given entry from the free thread list.
162 t ... threadobject to be removed
164 *******************************************************************************/
166 void threadlist_free_remove(threadobject *t)
168 list_remove_unsynced(list_thread_free, t);
172 /* threadlist_free_first *******************************************************
174 Return the first entry in the free thread list.
177 threadobject of the first free entry
179 *******************************************************************************/
181 threadobject *threadlist_free_first(void)
185 t = list_first_unsynced(list_thread_free);
191 /* threadlist_get_non_daemons **************************************************
193 Return the number of non-daemon threads.
195 NOTE: This function does a linear-search over the threads list,
196 because it's only used for joining the threads.
198 *******************************************************************************/
200 int threadlist_get_non_daemons(void)
205 /* Lock the threads lists. */
211 for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
212 if (!(t->flags & THREAD_FLAG_DAEMON))
216 /* Unlock the threads lists. */
218 threads_list_unlock();
224 /* threadlist_index_first ******************************************************
226 Return the first entry in the thread-index list.
229 thread-index structure
231 *******************************************************************************/
233 static inline thread_index_t *threadlist_index_first(void)
237 ti = list_first_unsynced(list_thread_index_free);
243 /* threadlist_index_add ********************************************************
245 Add the given thread-index to the thread-index free list.
250 *******************************************************************************/
252 void threadlist_index_add(int index)
256 ti = NEW(thread_index_t);
258 #if defined(ENABLE_STATISTICS)
260 size_thread_index_t += sizeof(thread_index_t);
263 /* Set the index in the structure. */
267 list_add_last_unsynced(list_thread_index_free, ti);
271 /* threadlist_index_remove *****************************************************
273 Remove the given thread-index from the thread-index list and free
274 the thread-index structure.
277 ti ... thread-index structure
279 *******************************************************************************/
281 static inline void threadlist_index_remove(thread_index_t *ti)
283 list_remove_unsynced(list_thread_index_free, ti);
285 FREE(ti, thread_index_t);
287 #if defined(ENABLE_STATISTICS)
289 size_thread_index_t -= sizeof(thread_index_t);
294 /* threadlist_get_free_index ***************************************************
296 Return a free thread index.
301 *******************************************************************************/
303 int threadlist_get_free_index(void)
308 /* Try to get a thread index from the free-list. */
310 ti = threadlist_index_first();
312 /* Is a free thread index available? */
315 /* Yes, get the index and remove it from the free list. */
319 threadlist_index_remove(ti);
322 /* Get a new the thread index. */
324 index = list_thread->size + 1;
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 * ---------------------------------------------------------------------
338 * indent-tabs-mode: t
342 * vim:noexpandtab:sw=4:ts=4: