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