thr_list.c revision 160426
1/* 2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org> 3 * Copyright (C) 2003 Daniel M. Eischen <deischen@freebsd.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice unmodified, this list of conditions, and the following 11 * disclaimer. 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. 15 * 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. 26 * 27 * $FreeBSD: head/lib/libthr/thread/thr_list.c 160426 2006-07-17 09:23:44Z delphij $ 28 */ 29 30#include <sys/types.h> 31#include <sys/queue.h> 32 33#include <stdlib.h> 34#include <string.h> 35#include <pthread.h> 36 37#include "thr_private.h" 38#include "libc_private.h" 39 40/*#define DEBUG_THREAD_LIST */ 41#ifdef DEBUG_THREAD_LIST 42#define DBG_MSG stdout_debug 43#else 44#define DBG_MSG(x...) 45#endif 46 47#define MAX_THREADS 100000 48 49/* 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 52 * will be free()'d. 53 */ 54#define MAX_CACHED_THREADS 100 55 56/* 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 59 * after a fork(). 60 */ 61static TAILQ_HEAD(, pthread) free_threadq; 62static umtx_t free_thread_lock; 63static umtx_t tcb_lock; 64static int free_thread_count = 0; 65static int inited = 0; 66static int total_threads; 67 68LIST_HEAD(thread_hash_head, pthread); 69#define HASH_QUEUES 128 70static struct thread_hash_head thr_hashtable[HASH_QUEUES]; 71#define THREAD_HASH(thrd) (((unsigned long)thrd >> 8) % HASH_QUEUES) 72 73static void thr_destroy(struct pthread *curthread, struct pthread *thread); 74 75void 76_thr_list_init(void) 77{ 78 int i; 79 80 _gc_count = 0; 81 total_threads = 1; 82 _thr_umtx_init(&_thr_list_lock); 83 TAILQ_INIT(&_thread_list); 84 TAILQ_INIT(&free_threadq); 85 _thr_umtx_init(&free_thread_lock); 86 _thr_umtx_init(&tcb_lock); 87 if (inited) { 88 for (i = 0; i < HASH_QUEUES; ++i) 89 LIST_INIT(&thr_hashtable[i]); 90 } 91 inited = 1; 92} 93 94void 95_thr_gc(struct pthread *curthread) 96{ 97 struct pthread *td, *td_next; 98 TAILQ_HEAD(, pthread) worklist; 99 100 TAILQ_INIT(&worklist); 101 THREAD_LIST_LOCK(curthread); 102 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 */ 107 continue; 108 } 109 _thr_stack_free(&td->attr); 110 if (((td->tlflags & TLFLAGS_DETACHED) != 0) && 111 (td->refcount == 0)) { 112 THR_GCLIST_REMOVE(td); 113 /* 114 * The thread has detached and is no longer 115 * referenced. It is safe to remove all 116 * remnants of the thread. 117 */ 118 THR_LIST_REMOVE(td); 119 TAILQ_INSERT_HEAD(&worklist, td, gcle); 120 } 121 } 122 THREAD_LIST_UNLOCK(curthread); 123 124 while ((td = TAILQ_FIRST(&worklist)) != NULL) { 125 TAILQ_REMOVE(&worklist, td, gcle); 126 /* 127 * XXX we don't free initial thread, because there might 128 * have some code referencing initial thread. 129 */ 130 if (td == _thr_initial) { 131 DBG_MSG("Initial thread won't be freed\n"); 132 continue; 133 } 134 135 _thr_free(curthread, td); 136 } 137} 138 139struct pthread * 140_thr_alloc(struct pthread *curthread) 141{ 142 struct pthread *thread = NULL; 143 struct tcb *tcb; 144 145 if (curthread != NULL) { 146 if (GC_NEEDED()) 147 _thr_gc(curthread); 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); 152 free_thread_count--; 153 } 154 THR_LOCK_RELEASE(curthread, &free_thread_lock); 155 } 156 } 157 if (thread == NULL) { 158 if (total_threads > MAX_THREADS) 159 return (NULL); 160 atomic_fetchadd_int(&total_threads, 1); 161 thread = malloc(sizeof(struct pthread)); 162 if (thread == NULL) { 163 atomic_fetchadd_int(&total_threads, -1); 164 return (NULL); 165 } 166 } 167 if (curthread != NULL) { 168 THR_LOCK_ACQUIRE(curthread, &tcb_lock); 169 tcb = _tcb_ctor(thread, 0 /* not initial tls */); 170 THR_LOCK_RELEASE(curthread, &tcb_lock); 171 } else { 172 tcb = _tcb_ctor(thread, 1 /* initial tls */); 173 } 174 if (tcb != NULL) { 175 memset(thread, 0, sizeof(*thread)); 176 thread->tcb = tcb; 177 } else { 178 thr_destroy(curthread, thread); 179 atomic_fetchadd_int(&total_threads, -1); 180 thread = NULL; 181 } 182 return (thread); 183} 184 185void 186_thr_free(struct pthread *curthread, struct pthread *thread) 187{ 188 DBG_MSG("Freeing thread %p\n", thread); 189 190 /* 191 * Always free tcb, as we only know it is part of RTLD TLS 192 * block, but don't know its detail and can not assume how 193 * it works, so better to avoid caching it here. 194 */ 195 if (curthread != NULL) { 196 THR_LOCK_ACQUIRE(curthread, &tcb_lock); 197 _tcb_dtor(thread->tcb); 198 THR_LOCK_RELEASE(curthread, &tcb_lock); 199 } else { 200 _tcb_dtor(thread->tcb); 201 } 202 thread->tcb = NULL; 203 if ((curthread == NULL) || (free_thread_count >= MAX_CACHED_THREADS)) { 204 thr_destroy(curthread, thread); 205 atomic_fetchadd_int(&total_threads, -1); 206 } else { 207 /* 208 * Add the thread to the free thread list, this also avoids 209 * pthread id is reused too quickly, may help some buggy apps. 210 */ 211 THR_LOCK_ACQUIRE(curthread, &free_thread_lock); 212 TAILQ_INSERT_TAIL(&free_threadq, thread, tle); 213 free_thread_count++; 214 THR_LOCK_RELEASE(curthread, &free_thread_lock); 215 } 216} 217 218static void 219thr_destroy(struct pthread *curthread __unused, struct pthread *thread) 220{ 221 free(thread); 222} 223 224/* 225 * Add the thread to the list of all threads and increment 226 * number of active threads. 227 */ 228void 229_thr_link(struct pthread *curthread, struct pthread *thread) 230{ 231 THREAD_LIST_LOCK(curthread); 232 THR_LIST_ADD(thread); 233 _thread_active_threads++; 234 THREAD_LIST_UNLOCK(curthread); 235} 236 237/* 238 * Remove an active thread. 239 */ 240void 241_thr_unlink(struct pthread *curthread, struct pthread *thread) 242{ 243 THREAD_LIST_LOCK(curthread); 244 THR_LIST_REMOVE(thread); 245 _thread_active_threads--; 246 THREAD_LIST_UNLOCK(curthread); 247} 248 249void 250_thr_hash_add(struct pthread *thread) 251{ 252 struct thread_hash_head *head; 253 254 head = &thr_hashtable[THREAD_HASH(thread)]; 255 LIST_INSERT_HEAD(head, thread, hle); 256} 257 258void 259_thr_hash_remove(struct pthread *thread) 260{ 261 LIST_REMOVE(thread, hle); 262} 263 264struct pthread * 265_thr_hash_find(struct pthread *thread) 266{ 267 struct pthread *td; 268 struct thread_hash_head *head; 269 270 head = &thr_hashtable[THREAD_HASH(thread)]; 271 LIST_FOREACH(td, head, hle) { 272 if (td == thread) 273 return (thread); 274 } 275 return (NULL); 276} 277 278/* 279 * Find a thread in the linked list of active threads and add a reference 280 * to it. Threads with positive reference counts will not be deallocated 281 * until all references are released. 282 */ 283int 284_thr_ref_add(struct pthread *curthread, struct pthread *thread, 285 int include_dead) 286{ 287 int ret; 288 289 if (thread == NULL) 290 /* Invalid thread: */ 291 return (EINVAL); 292 293 THREAD_LIST_LOCK(curthread); 294 if ((ret = _thr_find_thread(curthread, thread, include_dead)) == 0) { 295 thread->refcount++; 296 } 297 THREAD_LIST_UNLOCK(curthread); 298 299 /* Return zero if the thread exists: */ 300 return (ret); 301} 302 303void 304_thr_ref_delete(struct pthread *curthread, struct pthread *thread) 305{ 306 THREAD_LIST_LOCK(curthread); 307 _thr_ref_delete_unlocked(curthread, thread); 308 THREAD_LIST_UNLOCK(curthread); 309} 310 311void 312_thr_ref_delete_unlocked(struct pthread *curthread __unused, 313 struct pthread *thread) 314{ 315 if (thread != NULL) { 316 thread->refcount--; 317 if ((thread->refcount == 0) && thread->state == PS_DEAD && 318 (thread->tlflags & TLFLAGS_DETACHED) != 0) 319 THR_GCLIST_ADD(thread); 320 } 321} 322 323int 324_thr_find_thread(struct pthread *curthread __unused, struct pthread *thread, 325 int include_dead) 326{ 327 struct pthread *pthread; 328 329 if (thread == NULL) 330 /* Invalid thread: */ 331 return (EINVAL); 332 333 pthread = _thr_hash_find(thread); 334 if (pthread) { 335 if (include_dead == 0 && pthread->state == PS_DEAD) { 336 pthread = NULL; 337 } 338 } 339 340 /* Return zero if the thread exists: */ 341 return ((pthread != NULL) ? 0 : ESRCH); 342} 343