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.
30 #include <sys/types.h>
31 #include <sys/queue.h>
37 #include "thr_private.h"
38 #include "libc_private.h"
40 /*#define DEBUG_THREAD_LIST */
41 #ifdef DEBUG_THREAD_LIST
42 #define DBG_MSG stdout_debug
47 #define MAX_THREADS 100000
50 * Define a high water mark for the maximum number of threads that
51 * will be cached. Once this level is reached, any extra threads
54 #define MAX_CACHED_THREADS 100
57 * We've got to keep track of everything that is allocated, not only
58 * to have a speedy free list, but also so they can be deallocated
61 static TAILQ_HEAD(, pthread) free_threadq;
62 static struct umutex free_thread_lock = DEFAULT_UMUTEX;
63 static struct umutex tcb_lock = DEFAULT_UMUTEX;
64 static int free_thread_count = 0;
65 static int inited = 0;
66 static int total_threads;
68 LIST_HEAD(thread_hash_head, pthread);
69 #define HASH_QUEUES 128
70 static struct thread_hash_head thr_hashtable[HASH_QUEUES];
71 #define THREAD_HASH(thrd) (((unsigned long)thrd >> 8) % HASH_QUEUES)
73 static void thr_destroy(struct pthread *curthread, struct pthread *thread);
82 _thr_urwlock_init(&_thr_list_lock);
83 TAILQ_INIT(&_thread_list);
84 TAILQ_INIT(&free_threadq);
85 _thr_umutex_init(&free_thread_lock);
86 _thr_umutex_init(&tcb_lock);
88 for (i = 0; i < HASH_QUEUES; ++i)
89 LIST_INIT(&thr_hashtable[i]);
95 _thr_gc(struct pthread *curthread)
97 struct pthread *td, *td_next;
98 TAILQ_HEAD(, pthread) worklist;
100 TAILQ_INIT(&worklist);
101 THREAD_LIST_WRLOCK(curthread);
103 /* Check the threads waiting for GC. */
104 TAILQ_FOREACH_SAFE(td, &_thread_gc_list, gcle, td_next) {
105 if (td->tid != TID_TERMINATED) {
106 /* make sure we are not still in userland */
109 _thr_stack_free(&td->attr);
110 THR_GCLIST_REMOVE(td);
111 TAILQ_INSERT_HEAD(&worklist, td, gcle);
113 THREAD_LIST_UNLOCK(curthread);
115 while ((td = TAILQ_FIRST(&worklist)) != NULL) {
116 TAILQ_REMOVE(&worklist, td, gcle);
118 * XXX we don't free initial thread, because there might
119 * have some code referencing initial thread.
121 if (td == _thr_initial) {
122 DBG_MSG("Initial thread won't be freed\n");
126 _thr_free(curthread, td);
131 _thr_alloc(struct pthread *curthread)
133 struct pthread *thread = NULL;
136 if (curthread != NULL) {
139 if (free_thread_count > 0) {
140 THR_LOCK_ACQUIRE(curthread, &free_thread_lock);
141 if ((thread = TAILQ_FIRST(&free_threadq)) != NULL) {
142 TAILQ_REMOVE(&free_threadq, thread, tle);
145 THR_LOCK_RELEASE(curthread, &free_thread_lock);
148 if (thread == NULL) {
149 if (total_threads > MAX_THREADS)
151 atomic_fetchadd_int(&total_threads, 1);
152 thread = malloc(sizeof(struct pthread));
153 if (thread == NULL) {
154 atomic_fetchadd_int(&total_threads, -1);
158 if (curthread != NULL) {
159 THR_LOCK_ACQUIRE(curthread, &tcb_lock);
160 tcb = _tcb_ctor(thread, 0 /* not initial tls */);
161 THR_LOCK_RELEASE(curthread, &tcb_lock);
163 tcb = _tcb_ctor(thread, 1 /* initial tls */);
166 memset(thread, 0, sizeof(*thread));
169 thr_destroy(curthread, thread);
170 atomic_fetchadd_int(&total_threads, -1);
177 _thr_free(struct pthread *curthread, struct pthread *thread)
179 DBG_MSG("Freeing thread %p\n", thread);
182 * Always free tcb, as we only know it is part of RTLD TLS
183 * block, but don't know its detail and can not assume how
184 * it works, so better to avoid caching it here.
186 if (curthread != NULL) {
187 THR_LOCK_ACQUIRE(curthread, &tcb_lock);
188 _tcb_dtor(thread->tcb);
189 THR_LOCK_RELEASE(curthread, &tcb_lock);
191 _tcb_dtor(thread->tcb);
194 if ((curthread == NULL) || (free_thread_count >= MAX_CACHED_THREADS)) {
195 thr_destroy(curthread, thread);
196 atomic_fetchadd_int(&total_threads, -1);
199 * Add the thread to the free thread list, this also avoids
200 * pthread id is reused too quickly, may help some buggy apps.
202 THR_LOCK_ACQUIRE(curthread, &free_thread_lock);
203 TAILQ_INSERT_TAIL(&free_threadq, thread, tle);
205 THR_LOCK_RELEASE(curthread, &free_thread_lock);
210 thr_destroy(struct pthread *curthread __unused, struct pthread *thread)
216 * 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_WRLOCK(curthread);
223 THR_LIST_ADD(thread);
224 THREAD_LIST_UNLOCK(curthread);
225 atomic_add_int(&_thread_active_threads, 1);
229 * Remove an active thread.
232 _thr_unlink(struct pthread *curthread, struct pthread *thread)
234 THREAD_LIST_WRLOCK(curthread);
235 THR_LIST_REMOVE(thread);
236 THREAD_LIST_UNLOCK(curthread);
237 atomic_add_int(&_thread_active_threads, -1);
241 _thr_hash_add(struct pthread *thread)
243 struct thread_hash_head *head;
245 head = &thr_hashtable[THREAD_HASH(thread)];
246 LIST_INSERT_HEAD(head, thread, hle);
250 _thr_hash_remove(struct pthread *thread)
252 LIST_REMOVE(thread, hle);
256 _thr_hash_find(struct pthread *thread)
259 struct thread_hash_head *head;
261 head = &thr_hashtable[THREAD_HASH(thread)];
262 LIST_FOREACH(td, head, hle) {
270 * Find a thread in the linked list of active threads and add a reference
271 * to it. Threads with positive reference counts will not be deallocated
272 * until all references are released.
275 _thr_ref_add(struct pthread *curthread, struct pthread *thread,
281 /* Invalid thread: */
284 if ((ret = _thr_find_thread(curthread, thread, include_dead)) == 0) {
286 THR_CRITICAL_ENTER(curthread);
287 THR_THREAD_UNLOCK(curthread, thread);
290 /* Return zero if the thread exists: */
295 _thr_ref_delete(struct pthread *curthread, struct pthread *thread)
297 THR_THREAD_LOCK(curthread, thread);
299 _thr_try_gc(curthread, thread);
300 THR_CRITICAL_LEAVE(curthread);
303 /* entered with thread lock held, exit with thread lock released */
305 _thr_try_gc(struct pthread *curthread, struct pthread *thread)
307 if (THR_SHOULD_GC(thread)) {
308 THR_REF_ADD(curthread, thread);
309 THR_THREAD_UNLOCK(curthread, thread);
310 THREAD_LIST_WRLOCK(curthread);
311 THR_THREAD_LOCK(curthread, thread);
312 THR_REF_DEL(curthread, thread);
313 if (THR_SHOULD_GC(thread)) {
314 THR_LIST_REMOVE(thread);
315 THR_GCLIST_ADD(thread);
317 THR_THREAD_UNLOCK(curthread, thread);
318 THREAD_LIST_UNLOCK(curthread);
320 THR_THREAD_UNLOCK(curthread, thread);
324 /* return with thread lock held if thread is found */
326 _thr_find_thread(struct pthread *curthread, struct pthread *thread,
329 struct pthread *pthread;
336 THREAD_LIST_RDLOCK(curthread);
337 pthread = _thr_hash_find(thread);
339 THR_THREAD_LOCK(curthread, pthread);
340 if (include_dead == 0 && pthread->state == PS_DEAD) {
341 THR_THREAD_UNLOCK(curthread, pthread);
347 THREAD_LIST_UNLOCK(curthread);