]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libthr/thread/thr_list.c
MFC r345703:
[FreeBSD/FreeBSD.git] / lib / libthr / thread / thr_list.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
5  * Copyright (C) 2003 Daniel M. Eischen <deischen@freebsd.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
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.
17  *
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.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/types.h>
34 #include <sys/queue.h>
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <pthread.h>
39
40 #include "libc_private.h"
41 #include "thr_private.h"
42 #include "static_tls.h"
43
44 /*#define DEBUG_THREAD_LIST */
45 #ifdef DEBUG_THREAD_LIST
46 #define DBG_MSG         stdout_debug
47 #else
48 #define DBG_MSG(x...)
49 #endif
50
51 #define MAX_THREADS             100000
52
53 /*
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
56  * will be free()'d.
57  */
58 #define MAX_CACHED_THREADS      100
59
60 /*
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
63  * after a fork().
64  */
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;
71
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)
76
77 static void thr_destroy(struct pthread *curthread, struct pthread *thread);
78
79 void
80 _thr_list_init(void)
81 {
82         int i;
83
84         _gc_count = 0;
85         total_threads = 1;
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);
91         if (inited) {
92                 for (i = 0; i < HASH_QUEUES; ++i)
93                         LIST_INIT(&thr_hashtable[i]);
94         }
95         inited = 1;
96 }
97
98 void
99 _thr_gc(struct pthread *curthread)
100 {
101         struct pthread *td, *td_next;
102         TAILQ_HEAD(, pthread) worklist;
103
104         TAILQ_INIT(&worklist);
105         THREAD_LIST_WRLOCK(curthread);
106
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 */
111                         continue;
112                 }
113                 _thr_stack_free(&td->attr);
114                 THR_GCLIST_REMOVE(td);
115                 TAILQ_INSERT_HEAD(&worklist, td, gcle);
116         }
117         THREAD_LIST_UNLOCK(curthread);
118
119         while ((td = TAILQ_FIRST(&worklist)) != NULL) {
120                 TAILQ_REMOVE(&worklist, td, gcle);
121                 /*
122                  * XXX we don't free initial thread, because there might
123                  * have some code referencing initial thread.
124                  */
125                 if (td == _thr_initial) {
126                         DBG_MSG("Initial thread won't be freed\n");
127                         continue;
128                 }
129
130                 _thr_free(curthread, td);
131         }
132 }
133
134 struct pthread *
135 _thr_alloc(struct pthread *curthread)
136 {
137         struct pthread  *thread = NULL;
138         struct tcb      *tcb;
139
140         if (curthread != NULL) {
141                 if (GC_NEEDED())
142                         _thr_gc(curthread);
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);
147                                 free_thread_count--;
148                         }
149                         THR_LOCK_RELEASE(curthread, &free_thread_lock);
150                 }
151         }
152         if (thread == NULL) {
153                 if (total_threads > MAX_THREADS)
154                         return (NULL);
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);
159                         return (NULL);
160                 }
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);
165                         return (NULL);
166                 }
167         } else {
168                 bzero(&thread->_pthread_startzero, 
169                         __rangeof(struct pthread, _pthread_startzero, _pthread_endzero));
170         }
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);
175         } else {
176                 tcb = _tcb_ctor(thread, 1 /* initial tls */);
177         }
178         if (tcb != NULL) {
179                 thread->tcb = tcb;
180         } else {
181                 thr_destroy(curthread, thread);
182                 atomic_fetchadd_int(&total_threads, -1);
183                 thread = NULL;
184         }
185         return (thread);
186 }
187
188 void
189 _thr_free(struct pthread *curthread, struct pthread *thread)
190 {
191         DBG_MSG("Freeing thread %p\n", thread);
192
193         /*
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.
197          */
198         if (curthread != NULL) {
199                 THR_LOCK_ACQUIRE(curthread, &tcb_lock);
200                 _tcb_dtor(thread->tcb);
201                 THR_LOCK_RELEASE(curthread, &tcb_lock);
202         } else {
203                 _tcb_dtor(thread->tcb);
204         }
205         thread->tcb = NULL;
206         if ((curthread == NULL) || (free_thread_count >= MAX_CACHED_THREADS)) {
207                 thr_destroy(curthread, thread);
208                 atomic_fetchadd_int(&total_threads, -1);
209         } else {
210                 /*
211                  * Add the thread to the free thread list, this also avoids
212                  * pthread id is reused too quickly, may help some buggy apps.
213                  */
214                 THR_LOCK_ACQUIRE(curthread, &free_thread_lock);
215                 TAILQ_INSERT_TAIL(&free_threadq, thread, tle);
216                 free_thread_count++;
217                 THR_LOCK_RELEASE(curthread, &free_thread_lock);
218         }
219 }
220
221 static void
222 thr_destroy(struct pthread *curthread __unused, struct pthread *thread)
223 {
224         if (thread->sleepqueue != NULL)
225                 _sleepq_free(thread->sleepqueue);
226         if (thread->wake_addr != NULL)
227                 _thr_release_wake_addr(thread->wake_addr);
228         free(thread);
229 }
230
231 /*
232  * Add the thread to the list of all threads and increment
233  * number of active threads.
234  */
235 void
236 _thr_link(struct pthread *curthread, struct pthread *thread)
237 {
238         THREAD_LIST_WRLOCK(curthread);
239         THR_LIST_ADD(thread);
240         THREAD_LIST_UNLOCK(curthread);
241         atomic_add_int(&_thread_active_threads, 1);
242 }
243
244 /*
245  * Remove an active thread.
246  */
247 void
248 _thr_unlink(struct pthread *curthread, struct pthread *thread)
249 {
250         THREAD_LIST_WRLOCK(curthread);
251         THR_LIST_REMOVE(thread);
252         THREAD_LIST_UNLOCK(curthread);
253         atomic_add_int(&_thread_active_threads, -1);
254 }
255
256 void
257 _thr_hash_add(struct pthread *thread)
258 {
259         struct thread_hash_head *head;
260
261         head = &thr_hashtable[THREAD_HASH(thread)];
262         LIST_INSERT_HEAD(head, thread, hle);
263 }
264
265 void
266 _thr_hash_remove(struct pthread *thread)
267 {
268         LIST_REMOVE(thread, hle);
269 }
270
271 struct pthread *
272 _thr_hash_find(struct pthread *thread)
273 {
274         struct pthread *td;
275         struct thread_hash_head *head;
276
277         head = &thr_hashtable[THREAD_HASH(thread)];
278         LIST_FOREACH(td, head, hle) {
279                 if (td == thread)
280                         return (thread);
281         }
282         return (NULL);
283 }
284
285 /*
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.
289  */
290 int
291 _thr_ref_add(struct pthread *curthread, struct pthread *thread,
292     int include_dead)
293 {
294         int ret;
295
296         if (thread == NULL)
297                 /* Invalid thread: */
298                 return (EINVAL);
299
300         if ((ret = _thr_find_thread(curthread, thread, include_dead)) == 0) {
301                 thread->refcount++;
302                 THR_CRITICAL_ENTER(curthread);
303                 THR_THREAD_UNLOCK(curthread, thread);
304         }
305
306         /* Return zero if the thread exists: */
307         return (ret);
308 }
309
310 void
311 _thr_ref_delete(struct pthread *curthread, struct pthread *thread)
312 {
313         THR_THREAD_LOCK(curthread, thread);
314         thread->refcount--;
315         _thr_try_gc(curthread, thread);
316         THR_CRITICAL_LEAVE(curthread);
317 }
318
319 /* entered with thread lock held, exit with thread lock released */
320 void
321 _thr_try_gc(struct pthread *curthread, struct pthread *thread)
322 {
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);
332                 }
333                 THR_THREAD_UNLOCK(curthread, thread);
334                 THREAD_LIST_UNLOCK(curthread);
335         } else {
336                 THR_THREAD_UNLOCK(curthread, thread);
337         }
338 }
339
340 /* return with thread lock held if thread is found */
341 int
342 _thr_find_thread(struct pthread *curthread, struct pthread *thread,
343     int include_dead)
344 {
345         struct pthread *pthread;
346         int ret;
347
348         if (thread == NULL)
349                 return (EINVAL);
350
351         ret = 0;
352         THREAD_LIST_RDLOCK(curthread);
353         pthread = _thr_hash_find(thread);
354         if (pthread) {
355                 THR_THREAD_LOCK(curthread, pthread);
356                 if (include_dead == 0 && pthread->state == PS_DEAD) {
357                         THR_THREAD_UNLOCK(curthread, pthread);
358                         ret = ESRCH;
359                 }
360         } else {
361                 ret = ESRCH;
362         }
363         THREAD_LIST_UNLOCK(curthread);
364         return (ret);
365 }
366
367 #include "pthread_tls.h"
368
369 static void
370 thr_distribute_static_tls(uintptr_t tlsbase, void *src, size_t len,
371     size_t total_len)
372 {
373
374         memcpy((void *)tlsbase, src, len);
375         memset((char *)tlsbase + len, 0, total_len - len);
376 }
377
378 void
379 __pthread_distribute_static_tls(size_t offset, void *src, size_t len,
380     size_t total_len)
381 {
382         struct pthread *curthread, *thrd;
383         uintptr_t tlsbase;
384
385         if (!_thr_is_inited()) {
386                 tlsbase = _libc_get_static_tls_base(offset);
387                 thr_distribute_static_tls(tlsbase, src, len, total_len);
388                 return;
389         }
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);
395         }
396         THREAD_LIST_UNLOCK(curthread);
397 }