thr_list.c revision 144736
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 144736 2005-04-07 06:09:17Z davidxu $ 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/* 48 * Define a high water mark for the maximum number of threads that 49 * will be cached. Once this level is reached, any extra threads 50 * will be free()'d. 51 */ 52#define MAX_CACHED_THREADS 100 53 54/* 55 * We've got to keep track of everything that is allocated, not only 56 * to have a speedy free list, but also so they can be deallocated 57 * after a fork(). 58 */ 59static TAILQ_HEAD(, pthread) free_threadq; 60static umtx_t free_thread_lock; 61static umtx_t tcb_lock; 62static int free_thread_count = 0; 63static int inited = 0; 64 65LIST_HEAD(thread_hash_head, pthread); 66#define HASH_QUEUES 128 67static struct thread_hash_head thr_hashtable[HASH_QUEUES]; 68#define THREAD_HASH(thrd) (((unsigned long)thrd >> 8) % HASH_QUEUES) 69 70static void thr_destroy(struct pthread *curthread, struct pthread *thread); 71 72void 73_thr_list_init(void) 74{ 75 int i; 76 77 _gc_count = 0; 78 _thr_umtx_init(&_thr_list_lock); 79 TAILQ_INIT(&_thread_list); 80 TAILQ_INIT(&free_threadq); 81 _thr_umtx_init(&free_thread_lock); 82 _thr_umtx_init(&tcb_lock); 83 if (inited) { 84 for (i = 0; i < HASH_QUEUES; ++i) 85 LIST_INIT(&thr_hashtable[i]); 86 } 87 inited = 1; 88} 89 90void 91_thr_gc(struct pthread *curthread) 92{ 93 struct pthread *td, *td_next; 94 TAILQ_HEAD(, pthread) worklist; 95 96 TAILQ_INIT(&worklist); 97 THREAD_LIST_LOCK(curthread); 98 99 /* Check the threads waiting for GC. */ 100 for (td = TAILQ_FIRST(&_thread_gc_list); td != NULL; td = td_next) { 101 td_next = TAILQ_NEXT(td, gcle); 102 if (td->terminated == 0) { 103 /* make sure we are not still in userland */ 104 continue; 105 } 106 _thr_stack_free(&td->attr); 107 if (((td->tlflags & TLFLAGS_DETACHED) != 0) && 108 (td->refcount == 0)) { 109 THR_GCLIST_REMOVE(td); 110 /* 111 * The thread has detached and is no longer 112 * referenced. It is safe to remove all 113 * remnants of the thread. 114 */ 115 THR_LIST_REMOVE(td); 116 TAILQ_INSERT_HEAD(&worklist, td, gcle); 117 } 118 } 119 THREAD_LIST_UNLOCK(curthread); 120 121 while ((td = TAILQ_FIRST(&worklist)) != NULL) { 122 TAILQ_REMOVE(&worklist, td, gcle); 123 /* 124 * XXX we don't free initial thread, because there might 125 * have some code referencing initial thread. 126 */ 127 if (td == _thr_initial) { 128 DBG_MSG("Initial thread won't be freed\n"); 129 continue; 130 } 131 132 DBG_MSG("Freeing thread %p\n", td); 133 _thr_free(curthread, td); 134 } 135} 136 137struct pthread * 138_thr_alloc(struct pthread *curthread) 139{ 140 struct pthread *thread = NULL; 141 struct tcb *tcb; 142 143 if (curthread != NULL) { 144 if (GC_NEEDED()) 145 _thr_gc(curthread); 146 if (free_thread_count > 0) { 147 THR_LOCK_ACQUIRE(curthread, &free_thread_lock); 148 if ((thread = TAILQ_FIRST(&free_threadq)) != NULL) { 149 TAILQ_REMOVE(&free_threadq, thread, tle); 150 free_thread_count--; 151 } 152 THR_LOCK_RELEASE(curthread, &free_thread_lock); 153 } 154 } 155 if (thread == NULL) { 156 thread = malloc(sizeof(struct pthread)); 157 if (thread == NULL) 158 return (NULL); 159 } 160 if (curthread != NULL) { 161 THR_LOCK_ACQUIRE(curthread, &tcb_lock); 162 tcb = _tcb_ctor(thread, 0 /* not initial tls */); 163 THR_LOCK_RELEASE(curthread, &tcb_lock); 164 } else { 165 tcb = _tcb_ctor(thread, 1 /* initial tls */); 166 } 167 if (tcb != NULL) { 168 memset(thread, 0, sizeof(*thread)); 169 thread->tcb = tcb; 170 } else { 171 thr_destroy(curthread, thread); 172 thread = NULL; 173 } 174 return (thread); 175} 176 177void 178_thr_free(struct pthread *curthread, struct pthread *thread) 179{ 180 DBG_MSG("Freeing thread %p\n", thread); 181 if (thread->name) { 182 free(thread->name); 183 thread->name = NULL; 184 } 185 /* 186 * Always free tcb, as we only know it is part of RTLD TLS 187 * block, but don't know its detail and can not assume how 188 * it works, so better to avoid caching it here. 189 */ 190 if (curthread != NULL) { 191 THR_LOCK_ACQUIRE(curthread, &tcb_lock); 192 _tcb_dtor(thread->tcb); 193 THR_LOCK_RELEASE(curthread, &tcb_lock); 194 } else { 195 _tcb_dtor(thread->tcb); 196 } 197 thread->tcb = NULL; 198 if ((curthread == NULL) || (free_thread_count >= MAX_CACHED_THREADS)) { 199 thr_destroy(curthread, thread); 200 } else { 201 /* 202 * Add the thread to the free thread list, this also avoids 203 * pthread id is reused too quickly, may help some buggy apps. 204 */ 205 THR_LOCK_ACQUIRE(curthread, &free_thread_lock); 206 TAILQ_INSERT_TAIL(&free_threadq, thread, tle); 207 free_thread_count++; 208 THR_LOCK_RELEASE(curthread, &free_thread_lock); 209 } 210} 211 212static void 213thr_destroy(struct pthread *curthread __unused, struct pthread *thread) 214{ 215 free(thread); 216} 217 218/* 219 * Add the thread to the list of all threads and increment 220 * number of active threads. 221 */ 222void 223_thr_link(struct pthread *curthread, struct pthread *thread) 224{ 225 THREAD_LIST_LOCK(curthread); 226 THR_LIST_ADD(thread); 227 if (thread->attr.flags & PTHREAD_DETACHED) 228 thread->tlflags |= TLFLAGS_DETACHED; 229 _thread_active_threads++; 230 THREAD_LIST_UNLOCK(curthread); 231} 232 233/* 234 * Remove an active thread. 235 */ 236void 237_thr_unlink(struct pthread *curthread, struct pthread *thread) 238{ 239 THREAD_LIST_LOCK(curthread); 240 THR_LIST_REMOVE(thread); 241 _thread_active_threads--; 242 THREAD_LIST_UNLOCK(curthread); 243} 244 245void 246_thr_hash_add(struct pthread *thread) 247{ 248 struct thread_hash_head *head; 249 250 head = &thr_hashtable[THREAD_HASH(thread)]; 251 LIST_INSERT_HEAD(head, thread, hle); 252} 253 254void 255_thr_hash_remove(struct pthread *thread) 256{ 257 LIST_REMOVE(thread, hle); 258} 259 260struct pthread * 261_thr_hash_find(struct pthread *thread) 262{ 263 struct pthread *td; 264 struct thread_hash_head *head; 265 266 head = &thr_hashtable[THREAD_HASH(thread)]; 267 LIST_FOREACH(td, head, hle) { 268 if (td == thread) 269 return (thread); 270 } 271 return (NULL); 272} 273 274/* 275 * Find a thread in the linked list of active threads and add a reference 276 * to it. Threads with positive reference counts will not be deallocated 277 * until all references are released. 278 */ 279int 280_thr_ref_add(struct pthread *curthread, struct pthread *thread, 281 int include_dead) 282{ 283 int ret; 284 285 if (thread == NULL) 286 /* Invalid thread: */ 287 return (EINVAL); 288 289 THREAD_LIST_LOCK(curthread); 290 if ((ret = _thr_find_thread(curthread, thread, include_dead)) == 0) { 291 thread->refcount++; 292 } 293 THREAD_LIST_UNLOCK(curthread); 294 295 /* Return zero if the thread exists: */ 296 return (ret); 297} 298 299void 300_thr_ref_delete(struct pthread *curthread, struct pthread *thread) 301{ 302 if (thread != NULL) { 303 THREAD_LIST_LOCK(curthread); 304 thread->refcount--; 305 if ((thread->refcount == 0) && 306 (thread->tlflags & TLFLAGS_GC_SAFE) != 0) 307 THR_GCLIST_ADD(thread); 308 THREAD_LIST_UNLOCK(curthread); 309 } 310} 311 312int 313_thr_find_thread(struct pthread *curthread, struct pthread *thread, 314 int include_dead) 315{ 316 struct pthread *pthread; 317 318 if (thread == NULL) 319 /* Invalid thread: */ 320 return (EINVAL); 321 322 pthread = _thr_hash_find(thread); 323 if (pthread) { 324 if (include_dead == 0 && pthread->state == PS_DEAD) { 325 pthread = NULL; 326 } 327 } 328 329 /* Return zero if the thread exists: */ 330 return ((pthread != NULL) ? 0 : ESRCH); 331} 332