2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3 * Copyright (C) 2003 Daniel M. Eischen <deischen@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * $DragonFly: src/lib/libthread_xu/thread/thr_list.c,v 1.1 2005/02/01 12:38:27 davidxu Exp $
30 #include <sys/cdefs.h>
31 #include <sys/types.h>
32 #include <sys/queue.h>
38 #include "thr_private.h"
39 #include "libc_private.h"
41 /*#define DEBUG_THREAD_LIST */
42 #ifdef DEBUG_THREAD_LIST
43 #define DBG_MSG stdout_debug
49 * Define a high water mark for the maximum number of threads that
50 * will be cached. Once this level is reached, any extra threads
53 #define MAX_CACHED_THREADS 100
56 * We've got to keep track of everything that is allocated, not only
57 * to have a speedy free list, but also so they can be deallocated
60 static TAILQ_HEAD(, pthread) free_threadq;
61 static umtx_t free_thread_lock;
62 static umtx_t tcb_lock;
63 static int free_thread_count = 0;
64 static int inited = 0;
65 static u_int64_t next_uniqueid = 1;
67 LIST_HEAD(thread_hash_head, pthread);
68 #define HASH_QUEUES 128
69 static struct thread_hash_head thr_hashtable[HASH_QUEUES];
70 #define THREAD_HASH(thrd) (((unsigned long)thrd >> 12) % HASH_QUEUES)
72 static void thr_destroy(struct pthread *curthread, struct pthread *thread);
80 _thr_umtx_init(&_thr_list_lock);
81 TAILQ_INIT(&_thread_list);
82 TAILQ_INIT(&free_threadq);
83 _thr_umtx_init(&free_thread_lock);
84 _thr_umtx_init(&tcb_lock);
86 for (i = 0; i < HASH_QUEUES; ++i)
87 LIST_INIT(&thr_hashtable[i]);
93 _thr_gc(struct pthread *curthread)
95 struct pthread *td, *td_next;
96 TAILQ_HEAD(, pthread) worklist;
98 TAILQ_INIT(&worklist);
99 THREAD_LIST_LOCK(curthread);
101 /* Check the threads waiting for GC. */
102 for (td = TAILQ_FIRST(&_thread_gc_list); td != NULL; td = td_next) {
103 td_next = TAILQ_NEXT(td, gcle);
104 if (td->terminated == 0) {
105 /* make sure we are not still in userland */
108 _thr_stack_free(&td->attr);
109 if (((td->tlflags & TLFLAGS_DETACHED) != 0) &&
110 (td->refcount == 0)) {
111 THR_GCLIST_REMOVE(td);
113 * The thread has detached and is no longer
114 * referenced. It is safe to remove all
115 * remnants of the thread.
118 TAILQ_INSERT_HEAD(&worklist, td, gcle);
121 THREAD_LIST_UNLOCK(curthread);
123 while ((td = TAILQ_FIRST(&worklist)) != NULL) {
124 TAILQ_REMOVE(&worklist, td, gcle);
126 * XXX we don't free initial thread, because there might
127 * have some code referencing initial thread.
129 if (td == _thr_initial) {
130 DBG_MSG("Initial thread won't be freed\n");
134 DBG_MSG("Freeing thread %p\n", td);
135 _thr_free(curthread, td);
140 _thr_alloc(struct pthread *curthread)
142 struct pthread *thread = NULL;
143 struct tcb *tcb = NULL;
145 if (curthread != NULL) {
148 if (free_thread_count > 0) {
149 THR_LOCK_ACQUIRE(curthread, &free_thread_lock);
150 if ((thread = TAILQ_FIRST(&free_threadq)) != NULL) {
151 TAILQ_REMOVE(&free_threadq, thread, tle);
155 THR_LOCK_RELEASE(curthread, &free_thread_lock);
158 if ((thread == NULL) &&
159 ((thread = malloc(sizeof(struct pthread))) != NULL)) {
161 THR_LOCK_ACQUIRE(curthread, &tcb_lock);
162 tcb = _tcb_ctor(thread, 0 /* not initial tls */);
163 THR_LOCK_RELEASE(curthread, &tcb_lock);
165 tcb = _tcb_ctor(thread, 1 /* initial tls */);
173 memset(thread, 0, sizeof(*thread));
180 _thr_free(struct pthread *curthread, struct pthread *thread)
182 DBG_MSG("Freeing thread %p\n", thread);
187 if ((curthread == NULL) || (free_thread_count >= MAX_CACHED_THREADS)) {
188 thr_destroy(curthread, thread);
190 /* Add the thread to the free thread list. */
191 THR_LOCK_ACQUIRE(curthread, &free_thread_lock);
192 TAILQ_INSERT_TAIL(&free_threadq, thread, tle);
194 THR_LOCK_RELEASE(curthread, &free_thread_lock);
199 thr_destroy(struct pthread *curthread, struct pthread *thread)
202 THR_LOCK_ACQUIRE(curthread, &tcb_lock);
203 _tcb_dtor(thread->tcb);
204 THR_LOCK_RELEASE(curthread, &tcb_lock);
206 _tcb_dtor(thread->tcb);
212 * Add an active thread:
214 * o Assign the thread a unique id (which GDB uses to track
216 * o Add the thread to the list of all threads and increment
217 * number of active threads.
220 _thr_link(struct pthread *curthread, struct pthread *thread)
222 THREAD_LIST_LOCK(curthread);
224 * Initialize the unique id (which GDB uses to track
225 * threads), add the thread to the list of all threads,
228 thread->uniqueid = next_uniqueid++;
229 THR_LIST_ADD(thread);
230 if (thread->attr.flags & PTHREAD_DETACHED)
231 thread->tlflags |= TLFLAGS_DETACHED;
232 _thread_active_threads++;
233 THREAD_LIST_UNLOCK(curthread);
237 * Remove an active thread.
240 _thr_unlink(struct pthread *curthread, struct pthread *thread)
242 THREAD_LIST_LOCK(curthread);
243 THR_LIST_REMOVE(thread);
244 _thread_active_threads--;
245 THREAD_LIST_UNLOCK(curthread);
249 _thr_hash_add(struct pthread *thread)
251 struct thread_hash_head *head;
253 head = &thr_hashtable[THREAD_HASH(thread)];
254 LIST_INSERT_HEAD(head, thread, hle);
258 _thr_hash_remove(struct pthread *thread)
260 LIST_REMOVE(thread, hle);
264 _thr_hash_find(struct pthread *thread)
267 struct thread_hash_head *head;
269 head = &thr_hashtable[THREAD_HASH(thread)];
270 LIST_FOREACH(td, head, hle) {
278 * Find a thread in the linked list of active threads and add a reference
279 * to it. Threads with positive reference counts will not be deallocated
280 * until all references are released.
283 _thr_ref_add(struct pthread *curthread, struct pthread *thread,
289 /* Invalid thread: */
292 THREAD_LIST_LOCK(curthread);
293 if ((ret = _thr_find_thread(curthread, thread, include_dead)) == 0) {
296 THREAD_LIST_UNLOCK(curthread);
298 /* Return zero if the thread exists: */
303 _thr_ref_delete(struct pthread *curthread, struct pthread *thread)
305 if (thread != NULL) {
306 THREAD_LIST_LOCK(curthread);
308 if ((thread->refcount == 0) &&
309 (thread->tlflags & TLFLAGS_GC_SAFE) != 0)
310 THR_GCLIST_ADD(thread);
311 THREAD_LIST_UNLOCK(curthread);
316 _thr_find_thread(struct pthread *curthread, struct pthread *thread,
319 struct pthread *pthread;
322 /* Invalid thread: */
325 pthread = _thr_hash_find(thread);
327 if (include_dead == 0 && pthread->state == PS_DEAD) {
332 /* Return zero if the thread exists: */
333 return ((pthread != NULL) ? 0 : ESRCH);