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$ 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 */ 61 static TAILQ_HEAD(, pthread) free_threadq; 62 static umtx_t free_thread_lock; 63 static umtx_t tcb_lock; 64 static int free_thread_count = 0; 65 static int inited = 0; 66 static int total_threads; 67 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) 72 73 static void thr_destroy(struct pthread *curthread, struct pthread *thread); 74 75 void 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 94 void 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 for (td = TAILQ_FIRST(&_thread_gc_list); td != NULL; td = td_next) { 105 td_next = TAILQ_NEXT(td, gcle); 106 if (td->tid != TID_TERMINATED) { 107 /* make sure we are not still in userland */ 108 continue; 109 } 110 _thr_stack_free(&td->attr); 111 if (((td->tlflags & TLFLAGS_DETACHED) != 0) && 112 (td->refcount == 0)) { 113 THR_GCLIST_REMOVE(td); 114 /* 115 * The thread has detached and is no longer 116 * referenced. It is safe to remove all 117 * remnants of the thread. 118 */ 119 THR_LIST_REMOVE(td); 120 TAILQ_INSERT_HEAD(&worklist, td, gcle); 121 } 122 } 123 THREAD_LIST_UNLOCK(curthread); 124 125 while ((td = TAILQ_FIRST(&worklist)) != NULL) { 126 TAILQ_REMOVE(&worklist, td, gcle); 127 /* 128 * XXX we don't free initial thread, because there might 129 * have some code referencing initial thread. 130 */ 131 if (td == _thr_initial) { 132 DBG_MSG("Initial thread won't be freed\n"); 133 continue; 134 } 135 136 _thr_free(curthread, td); 137 } 138 } 139 140 struct pthread * 141 _thr_alloc(struct pthread *curthread) 142 { 143 struct pthread *thread = NULL; 144 struct tcb *tcb; 145 146 if (curthread != NULL) { 147 if (GC_NEEDED()) 148 _thr_gc(curthread); 149 if (free_thread_count > 0) { 150 THR_LOCK_ACQUIRE(curthread, &free_thread_lock); 151 if ((thread = TAILQ_FIRST(&free_threadq)) != NULL) { 152 TAILQ_REMOVE(&free_threadq, thread, tle); 153 free_thread_count--; 154 } 155 THR_LOCK_RELEASE(curthread, &free_thread_lock); 156 } 157 } 158 if (thread == NULL) { 159 if (total_threads > MAX_THREADS) 160 return (NULL); 161 atomic_fetchadd_int(&total_threads, 1); 162 thread = malloc(sizeof(struct pthread)); 163 if (thread == NULL) { 164 atomic_fetchadd_int(&total_threads, -1); 165 return (NULL); 166 } 167 } 168 if (curthread != NULL) { 169 THR_LOCK_ACQUIRE(curthread, &tcb_lock); 170 tcb = _tcb_ctor(thread, 0 /* not initial tls */); 171 THR_LOCK_RELEASE(curthread, &tcb_lock); 172 } else { 173 tcb = _tcb_ctor(thread, 1 /* initial tls */); 174 } 175 if (tcb != NULL) { 176 memset(thread, 0, sizeof(*thread)); 177 thread->tcb = tcb; 178 } else { 179 thr_destroy(curthread, thread); 180 atomic_fetchadd_int(&total_threads, -1); 181 thread = NULL; 182 } 183 return (thread); 184 } 185 186 void 187 _thr_free(struct pthread *curthread, struct pthread *thread) 188 { 189 DBG_MSG("Freeing thread %p\n", thread); 190 191 /* 192 * Always free tcb, as we only know it is part of RTLD TLS 193 * block, but don't know its detail and can not assume how 194 * it works, so better to avoid caching it here. 195 */ 196 if (curthread != NULL) { 197 THR_LOCK_ACQUIRE(curthread, &tcb_lock); 198 _tcb_dtor(thread->tcb); 199 THR_LOCK_RELEASE(curthread, &tcb_lock); 200 } else { 201 _tcb_dtor(thread->tcb); 202 } 203 thread->tcb = NULL; 204 if ((curthread == NULL) || (free_thread_count >= MAX_CACHED_THREADS)) { 205 thr_destroy(curthread, thread); 206 atomic_fetchadd_int(&total_threads, -1); 207 } else { 208 /* 209 * Add the thread to the free thread list, this also avoids 210 * pthread id is reused too quickly, may help some buggy apps. 211 */ 212 THR_LOCK_ACQUIRE(curthread, &free_thread_lock); 213 TAILQ_INSERT_TAIL(&free_threadq, thread, tle); 214 free_thread_count++; 215 THR_LOCK_RELEASE(curthread, &free_thread_lock); 216 } 217 } 218 219 static void 220 thr_destroy(struct pthread *curthread __unused, struct pthread *thread) 221 { 222 free(thread); 223 } 224 225 /* 226 * Add the thread to the list of all threads and increment 227 * number of active threads. 228 */ 229 void 230 _thr_link(struct pthread *curthread, struct pthread *thread) 231 { 232 THREAD_LIST_LOCK(curthread); 233 THR_LIST_ADD(thread); 234 _thread_active_threads++; 235 THREAD_LIST_UNLOCK(curthread); 236 } 237 238 /* 239 * Remove an active thread. 240 */ 241 void 242 _thr_unlink(struct pthread *curthread, struct pthread *thread) 243 { 244 THREAD_LIST_LOCK(curthread); 245 THR_LIST_REMOVE(thread); 246 _thread_active_threads--; 247 THREAD_LIST_UNLOCK(curthread); 248 } 249 250 void 251 _thr_hash_add(struct pthread *thread) 252 { 253 struct thread_hash_head *head; 254 255 head = &thr_hashtable[THREAD_HASH(thread)]; 256 LIST_INSERT_HEAD(head, thread, hle); 257 } 258 259 void 260 _thr_hash_remove(struct pthread *thread) 261 { 262 LIST_REMOVE(thread, hle); 263 } 264 265 struct pthread * 266 _thr_hash_find(struct pthread *thread) 267 { 268 struct pthread *td; 269 struct thread_hash_head *head; 270 271 head = &thr_hashtable[THREAD_HASH(thread)]; 272 LIST_FOREACH(td, head, hle) { 273 if (td == thread) 274 return (thread); 275 } 276 return (NULL); 277 } 278 279 /* 280 * Find a thread in the linked list of active threads and add a reference 281 * to it. Threads with positive reference counts will not be deallocated 282 * until all references are released. 283 */ 284 int 285 _thr_ref_add(struct pthread *curthread, struct pthread *thread, 286 int include_dead) 287 { 288 int ret; 289 290 if (thread == NULL) 291 /* Invalid thread: */ 292 return (EINVAL); 293 294 THREAD_LIST_LOCK(curthread); 295 if ((ret = _thr_find_thread(curthread, thread, include_dead)) == 0) { 296 thread->refcount++; 297 } 298 THREAD_LIST_UNLOCK(curthread); 299 300 /* Return zero if the thread exists: */ 301 return (ret); 302 } 303 304 void 305 _thr_ref_delete(struct pthread *curthread, struct pthread *thread) 306 { 307 THREAD_LIST_LOCK(curthread); 308 _thr_ref_delete_unlocked(curthread, thread); 309 THREAD_LIST_UNLOCK(curthread); 310 } 311 312 void 313 _thr_ref_delete_unlocked(struct pthread *curthread __unused, 314 struct pthread *thread) 315 { 316 if (thread != NULL) { 317 thread->refcount--; 318 if ((thread->refcount == 0) && thread->state == PS_DEAD && 319 (thread->tlflags & TLFLAGS_DETACHED) != 0) 320 THR_GCLIST_ADD(thread); 321 } 322 } 323 324 int 325 _thr_find_thread(struct pthread *curthread __unused, struct pthread *thread, 326 int include_dead) 327 { 328 struct pthread *pthread; 329 330 if (thread == NULL) 331 /* Invalid thread: */ 332 return (EINVAL); 333 334 pthread = _thr_hash_find(thread); 335 if (pthread) { 336 if (include_dead == 0 && pthread->state == PS_DEAD) { 337 pthread = NULL; 338 } 339 } 340 341 /* Return zero if the thread exists: */ 342 return ((pthread != NULL) ? 0 : ESRCH); 343 } 344