2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
5 * Copyright (C) 2003 Daniel M. Eischen <deischen@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
34 #include <sys/queue.h>
40 #include "libc_private.h"
41 #include "thr_private.h"
42 #include "static_tls.h"
44 /*#define DEBUG_THREAD_LIST */
45 #ifdef DEBUG_THREAD_LIST
46 #define DBG_MSG stdout_debug
51 #define MAX_THREADS 100000
54 * Define a high water mark for the maximum number of threads that
55 * will be cached. Once this level is reached, any extra threads
58 #define MAX_CACHED_THREADS 100
61 * We've got to keep track of everything that is allocated, not only
62 * to have a speedy free list, but also so they can be deallocated
65 static TAILQ_HEAD(, pthread) free_threadq;
66 static struct umutex free_thread_lock = DEFAULT_UMUTEX;
67 static struct umutex tcb_lock = DEFAULT_UMUTEX;
68 static int free_thread_count = 0;
69 static int inited = 0;
70 static int total_threads;
72 LIST_HEAD(thread_hash_head, pthread);
73 #define HASH_QUEUES 128
74 static struct thread_hash_head thr_hashtable[HASH_QUEUES];
75 #define THREAD_HASH(thrd) (((unsigned long)thrd >> 8) % HASH_QUEUES)
77 static void thr_destroy(struct pthread *curthread, struct pthread *thread);
86 _thr_urwlock_init(&_thr_list_lock);
87 TAILQ_INIT(&_thread_list);
88 TAILQ_INIT(&free_threadq);
89 _thr_umutex_init(&free_thread_lock);
90 _thr_umutex_init(&tcb_lock);
92 for (i = 0; i < HASH_QUEUES; ++i)
93 LIST_INIT(&thr_hashtable[i]);
99 _thr_gc(struct pthread *curthread)
101 struct pthread *td, *td_next;
102 TAILQ_HEAD(, pthread) worklist;
104 TAILQ_INIT(&worklist);
105 THREAD_LIST_WRLOCK(curthread);
107 /* Check the threads waiting for GC. */
108 TAILQ_FOREACH_SAFE(td, &_thread_gc_list, gcle, td_next) {
109 if (td->tid != TID_TERMINATED) {
110 /* make sure we are not still in userland */
113 _thr_stack_free(&td->attr);
114 THR_GCLIST_REMOVE(td);
115 TAILQ_INSERT_HEAD(&worklist, td, gcle);
117 THREAD_LIST_UNLOCK(curthread);
119 while ((td = TAILQ_FIRST(&worklist)) != NULL) {
120 TAILQ_REMOVE(&worklist, td, gcle);
122 * XXX we don't free initial thread, because there might
123 * have some code referencing initial thread.
125 if (td == _thr_initial) {
126 DBG_MSG("Initial thread won't be freed\n");
130 _thr_free(curthread, td);
135 _thr_alloc(struct pthread *curthread)
137 struct pthread *thread = NULL;
140 if (curthread != NULL) {
143 if (free_thread_count > 0) {
144 THR_LOCK_ACQUIRE(curthread, &free_thread_lock);
145 if ((thread = TAILQ_FIRST(&free_threadq)) != NULL) {
146 TAILQ_REMOVE(&free_threadq, thread, tle);
149 THR_LOCK_RELEASE(curthread, &free_thread_lock);
152 if (thread == NULL) {
153 if (total_threads > MAX_THREADS)
155 atomic_fetchadd_int(&total_threads, 1);
156 thread = calloc(1, sizeof(struct pthread));
157 if (thread == NULL) {
158 atomic_fetchadd_int(&total_threads, -1);
161 if ((thread->sleepqueue = _sleepq_alloc()) == NULL ||
162 (thread->wake_addr = _thr_alloc_wake_addr()) == NULL) {
163 thr_destroy(curthread, thread);
164 atomic_fetchadd_int(&total_threads, -1);
168 bzero(&thread->_pthread_startzero,
169 __rangeof(struct pthread, _pthread_startzero, _pthread_endzero));
171 if (curthread != NULL) {
172 THR_LOCK_ACQUIRE(curthread, &tcb_lock);
173 tcb = _tcb_ctor(thread, 0 /* not initial tls */);
174 THR_LOCK_RELEASE(curthread, &tcb_lock);
176 tcb = _tcb_ctor(thread, 1 /* initial tls */);
181 thr_destroy(curthread, thread);
182 atomic_fetchadd_int(&total_threads, -1);
189 _thr_free(struct pthread *curthread, struct pthread *thread)
191 DBG_MSG("Freeing thread %p\n", thread);
194 * Always free tcb, as we only know it is part of RTLD TLS
195 * block, but don't know its detail and can not assume how
196 * it works, so better to avoid caching it here.
198 if (curthread != NULL) {
199 THR_LOCK_ACQUIRE(curthread, &tcb_lock);
200 _tcb_dtor(thread->tcb);
201 THR_LOCK_RELEASE(curthread, &tcb_lock);
203 _tcb_dtor(thread->tcb);
206 if ((curthread == NULL) || (free_thread_count >= MAX_CACHED_THREADS)) {
207 thr_destroy(curthread, thread);
208 atomic_fetchadd_int(&total_threads, -1);
211 * Add the thread to the free thread list, this also avoids
212 * pthread id is reused too quickly, may help some buggy apps.
214 THR_LOCK_ACQUIRE(curthread, &free_thread_lock);
215 TAILQ_INSERT_TAIL(&free_threadq, thread, tle);
217 THR_LOCK_RELEASE(curthread, &free_thread_lock);
222 thr_destroy(struct pthread *curthread __unused, struct pthread *thread)
224 if (thread->sleepqueue != NULL)
225 _sleepq_free(thread->sleepqueue);
226 if (thread->wake_addr != NULL)
227 _thr_release_wake_addr(thread->wake_addr);
232 * Add the thread to the list of all threads and increment
233 * number of active threads.
236 _thr_link(struct pthread *curthread, struct pthread *thread)
238 THREAD_LIST_WRLOCK(curthread);
239 THR_LIST_ADD(thread);
240 THREAD_LIST_UNLOCK(curthread);
241 atomic_add_int(&_thread_active_threads, 1);
245 * Remove an active thread.
248 _thr_unlink(struct pthread *curthread, struct pthread *thread)
250 THREAD_LIST_WRLOCK(curthread);
251 THR_LIST_REMOVE(thread);
252 THREAD_LIST_UNLOCK(curthread);
253 atomic_add_int(&_thread_active_threads, -1);
257 _thr_hash_add(struct pthread *thread)
259 struct thread_hash_head *head;
261 head = &thr_hashtable[THREAD_HASH(thread)];
262 LIST_INSERT_HEAD(head, thread, hle);
266 _thr_hash_remove(struct pthread *thread)
268 LIST_REMOVE(thread, hle);
272 _thr_hash_find(struct pthread *thread)
275 struct thread_hash_head *head;
277 head = &thr_hashtable[THREAD_HASH(thread)];
278 LIST_FOREACH(td, head, hle) {
286 * Find a thread in the linked list of active threads and add a reference
287 * to it. Threads with positive reference counts will not be deallocated
288 * until all references are released.
291 _thr_ref_add(struct pthread *curthread, struct pthread *thread,
297 /* Invalid thread: */
300 if ((ret = _thr_find_thread(curthread, thread, include_dead)) == 0) {
302 THR_CRITICAL_ENTER(curthread);
303 THR_THREAD_UNLOCK(curthread, thread);
306 /* Return zero if the thread exists: */
311 _thr_ref_delete(struct pthread *curthread, struct pthread *thread)
313 THR_THREAD_LOCK(curthread, thread);
315 _thr_try_gc(curthread, thread);
316 THR_CRITICAL_LEAVE(curthread);
319 /* entered with thread lock held, exit with thread lock released */
321 _thr_try_gc(struct pthread *curthread, struct pthread *thread)
323 if (THR_SHOULD_GC(thread)) {
324 THR_REF_ADD(curthread, thread);
325 THR_THREAD_UNLOCK(curthread, thread);
326 THREAD_LIST_WRLOCK(curthread);
327 THR_THREAD_LOCK(curthread, thread);
328 THR_REF_DEL(curthread, thread);
329 if (THR_SHOULD_GC(thread)) {
330 THR_LIST_REMOVE(thread);
331 THR_GCLIST_ADD(thread);
333 THR_THREAD_UNLOCK(curthread, thread);
334 THREAD_LIST_UNLOCK(curthread);
336 THR_THREAD_UNLOCK(curthread, thread);
340 /* return with thread lock held if thread is found */
342 _thr_find_thread(struct pthread *curthread, struct pthread *thread,
345 struct pthread *pthread;
352 THREAD_LIST_RDLOCK(curthread);
353 pthread = _thr_hash_find(thread);
355 THR_THREAD_LOCK(curthread, pthread);
356 if (include_dead == 0 && pthread->state == PS_DEAD) {
357 THR_THREAD_UNLOCK(curthread, pthread);
363 THREAD_LIST_UNLOCK(curthread);
367 #include "pthread_tls.h"
370 thr_distribute_static_tls(uintptr_t tlsbase, void *src, size_t len,
374 memcpy((void *)tlsbase, src, len);
375 memset((char *)tlsbase + len, 0, total_len - len);
379 __pthread_distribute_static_tls(size_t offset, void *src, size_t len,
382 struct pthread *curthread, *thrd;
385 if (!_thr_is_inited()) {
386 tlsbase = _libc_get_static_tls_base(offset);
387 thr_distribute_static_tls(tlsbase, src, len, total_len);
390 curthread = _get_curthread();
391 THREAD_LIST_RDLOCK(curthread);
392 TAILQ_FOREACH(thrd, &_thread_list, tle) {
393 tlsbase = _get_static_tls_base(thrd, offset);
394 thr_distribute_static_tls(tlsbase, src, len, total_len);
396 THREAD_LIST_UNLOCK(curthread);