thr_list.c revision 216641
1251877Speter/*
2251877Speter * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3251877Speter * Copyright (C) 2003 Daniel M. Eischen <deischen@freebsd.org>
4251877Speter * All rights reserved.
5251877Speter *
6251877Speter * Redistribution and use in source and binary forms, with or without
7251877Speter * modification, are permitted provided that the following conditions
8251877Speter * are met:
9251877Speter * 1. Redistributions of source code must retain the above copyright
10251877Speter *    notice unmodified, this list of conditions, and the following
11251877Speter *    disclaimer.
12251877Speter * 2. Redistributions in binary form must reproduce the above copyright
13251877Speter *    notice, this list of conditions and the following disclaimer in the
14251877Speter *    documentation and/or other materials provided with the distribution.
15251877Speter *
16251877Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17251877Speter * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18251877Speter * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19251877Speter * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20251877Speter * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21251877Speter * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22251877Speter * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23251877Speter * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24251877Speter * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25251877Speter * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26251877Speter *
27251877Speter * $FreeBSD: head/lib/libthr/thread/thr_list.c 216641 2010-12-22 05:01:52Z davidxu $
28251877Speter */
29251877Speter
30251877Speter#include <sys/types.h>
31251877Speter#include <sys/queue.h>
32251877Speter
33251877Speter#include <stdlib.h>
34251877Speter#include <string.h>
35251877Speter#include <pthread.h>
36251877Speter
37251877Speter#include "thr_private.h"
38251877Speter#include "libc_private.h"
39251877Speter
40251877Speter/*#define DEBUG_THREAD_LIST */
41251877Speter#ifdef DEBUG_THREAD_LIST
42251877Speter#define DBG_MSG		stdout_debug
43251877Speter#else
44251877Speter#define DBG_MSG(x...)
45251877Speter#endif
46251877Speter
47251877Speter#define MAX_THREADS		100000
48251877Speter
49251877Speter/*
50251877Speter * Define a high water mark for the maximum number of threads that
51251877Speter * will be cached.  Once this level is reached, any extra threads
52251877Speter * will be free()'d.
53251877Speter */
54251877Speter#define	MAX_CACHED_THREADS	100
55251877Speter
56251877Speter/*
57251877Speter * We've got to keep track of everything that is allocated, not only
58251877Speter * to have a speedy free list, but also so they can be deallocated
59251877Speter * after a fork().
60251877Speter */
61251877Speterstatic TAILQ_HEAD(, pthread)	free_threadq;
62251877Speterstatic struct umutex		free_thread_lock = DEFAULT_UMUTEX;
63251877Speterstatic struct umutex		tcb_lock = DEFAULT_UMUTEX;
64251877Speterstatic int			free_thread_count = 0;
65251877Speterstatic int			inited = 0;
66251877Speterstatic int			total_threads;
67251877Speter
68251877SpeterLIST_HEAD(thread_hash_head, pthread);
69251877Speter#define HASH_QUEUES	128
70251877Speterstatic struct thread_hash_head	thr_hashtable[HASH_QUEUES];
71251877Speter#define	THREAD_HASH(thrd)	(((unsigned long)thrd >> 8) % HASH_QUEUES)
72251877Speter
73251877Speterstatic void thr_destroy(struct pthread *curthread, struct pthread *thread);
74251877Speter
75251877Spetervoid
76251877Speter_thr_list_init(void)
77251877Speter{
78251877Speter	int i;
79251877Speter
80251877Speter	_gc_count = 0;
81251877Speter	total_threads = 1;
82251877Speter	_thr_urwlock_init(&_thr_list_lock);
83251877Speter	TAILQ_INIT(&_thread_list);
84251877Speter	TAILQ_INIT(&free_threadq);
85251877Speter	_thr_umutex_init(&free_thread_lock);
86251877Speter	_thr_umutex_init(&tcb_lock);
87251877Speter	if (inited) {
88251877Speter		for (i = 0; i < HASH_QUEUES; ++i)
89251877Speter			LIST_INIT(&thr_hashtable[i]);
90251877Speter	}
91251877Speter	inited = 1;
92251877Speter}
93251877Speter
94251877Spetervoid
95251877Speter_thr_gc(struct pthread *curthread)
96251877Speter{
97251877Speter	struct pthread *td, *td_next;
98251877Speter	TAILQ_HEAD(, pthread) worklist;
99251877Speter
100251877Speter	TAILQ_INIT(&worklist);
101251877Speter	THREAD_LIST_WRLOCK(curthread);
102251877Speter
103251877Speter	/* Check the threads waiting for GC. */
104251877Speter	TAILQ_FOREACH_SAFE(td, &_thread_gc_list, gcle, td_next) {
105251877Speter		if (td->tid != TID_TERMINATED) {
106251877Speter			/* make sure we are not still in userland */
107251877Speter			continue;
108251877Speter		}
109251877Speter		_thr_stack_free(&td->attr);
110251877Speter		THR_GCLIST_REMOVE(td);
111251877Speter		TAILQ_INSERT_HEAD(&worklist, td, gcle);
112251877Speter	}
113251877Speter	THREAD_LIST_UNLOCK(curthread);
114251877Speter
115251877Speter	while ((td = TAILQ_FIRST(&worklist)) != NULL) {
116251877Speter		TAILQ_REMOVE(&worklist, td, gcle);
117251877Speter		/*
118251877Speter		 * XXX we don't free initial thread, because there might
119251877Speter		 * have some code referencing initial thread.
120251877Speter		 */
121251877Speter		if (td == _thr_initial) {
122251877Speter			DBG_MSG("Initial thread won't be freed\n");
123251877Speter			continue;
124251877Speter		}
125251877Speter
126251877Speter		_thr_free(curthread, td);
127251877Speter	}
128251877Speter}
129251877Speter
130251877Speterstruct pthread *
131251877Speter_thr_alloc(struct pthread *curthread)
132251877Speter{
133251877Speter	struct pthread	*thread = NULL;
134251877Speter	struct tcb	*tcb;
135251877Speter
136251877Speter	if (curthread != NULL) {
137251877Speter		if (GC_NEEDED())
138251877Speter			_thr_gc(curthread);
139251877Speter		if (free_thread_count > 0) {
140251877Speter			THR_LOCK_ACQUIRE(curthread, &free_thread_lock);
141251877Speter			if ((thread = TAILQ_FIRST(&free_threadq)) != NULL) {
142251877Speter				TAILQ_REMOVE(&free_threadq, thread, tle);
143251877Speter				free_thread_count--;
144251877Speter			}
145251877Speter			THR_LOCK_RELEASE(curthread, &free_thread_lock);
146251877Speter		}
147251877Speter	}
148251877Speter	if (thread == NULL) {
149251877Speter		if (total_threads > MAX_THREADS)
150251877Speter			return (NULL);
151253895Speter		atomic_fetchadd_int(&total_threads, 1);
152253895Speter		thread = malloc(sizeof(struct pthread));
153253895Speter		if (thread == NULL) {
154253895Speter			atomic_fetchadd_int(&total_threads, -1);
155253895Speter			return (NULL);
156253895Speter		}
157253895Speter	}
158253895Speter	if (curthread != NULL) {
159251877Speter		THR_LOCK_ACQUIRE(curthread, &tcb_lock);
160251877Speter		tcb = _tcb_ctor(thread, 0 /* not initial tls */);
161251877Speter		THR_LOCK_RELEASE(curthread, &tcb_lock);
162251877Speter	} else {
163251877Speter		tcb = _tcb_ctor(thread, 1 /* initial tls */);
164251877Speter	}
165251877Speter	if (tcb != NULL) {
166251877Speter		memset(thread, 0, sizeof(*thread));
167251877Speter		thread->tcb = tcb;
168251877Speter		thread->sleepqueue = _sleepq_alloc();
169251877Speter		thread->wake_addr = _thr_alloc_wake_addr();
170251877Speter	} else {
171251877Speter		thr_destroy(curthread, thread);
172251877Speter		atomic_fetchadd_int(&total_threads, -1);
173251877Speter		thread = NULL;
174251877Speter	}
175251877Speter	return (thread);
176251877Speter}
177251877Speter
178251877Spetervoid
179251877Speter_thr_free(struct pthread *curthread, struct pthread *thread)
180251877Speter{
181251877Speter	DBG_MSG("Freeing thread %p\n", thread);
182251877Speter
183251877Speter	/*
184251877Speter	 * Always free tcb, as we only know it is part of RTLD TLS
185251877Speter	 * block, but don't know its detail and can not assume how
186251877Speter	 * it works, so better to avoid caching it here.
187251877Speter	 */
188251877Speter	if (curthread != NULL) {
189251877Speter		THR_LOCK_ACQUIRE(curthread, &tcb_lock);
190251877Speter		_tcb_dtor(thread->tcb);
191251877Speter		THR_LOCK_RELEASE(curthread, &tcb_lock);
192251877Speter	} else {
193251877Speter		_tcb_dtor(thread->tcb);
194251877Speter	}
195251877Speter	thread->tcb = NULL;
196251877Speter	if ((curthread == NULL) || (free_thread_count >= MAX_CACHED_THREADS)) {
197251877Speter		_sleepq_free(thread->sleepqueue);
198251877Speter		_thr_release_wake_addr(thread->wake_addr);
199251877Speter		thr_destroy(curthread, thread);
200251877Speter		atomic_fetchadd_int(&total_threads, -1);
201251877Speter	} else {
202251877Speter		/*
203251877Speter		 * Add the thread to the free thread list, this also avoids
204251877Speter		 * pthread id is reused too quickly, may help some buggy apps.
205251877Speter		 */
206251877Speter		THR_LOCK_ACQUIRE(curthread, &free_thread_lock);
207251877Speter		TAILQ_INSERT_TAIL(&free_threadq, thread, tle);
208251877Speter		free_thread_count++;
209251877Speter		THR_LOCK_RELEASE(curthread, &free_thread_lock);
210251877Speter	}
211251877Speter}
212251877Speter
213251877Speterstatic void
214251877Speterthr_destroy(struct pthread *curthread __unused, struct pthread *thread)
215251877Speter{
216251877Speter	free(thread);
217251877Speter}
218251877Speter
219251877Speter/*
220251877Speter * Add the thread to the list of all threads and increment
221251877Speter * number of active threads.
222251877Speter */
223251877Spetervoid
224251877Speter_thr_link(struct pthread *curthread, struct pthread *thread)
225251877Speter{
226251877Speter	THREAD_LIST_WRLOCK(curthread);
227251877Speter	THR_LIST_ADD(thread);
228251877Speter	THREAD_LIST_UNLOCK(curthread);
229251877Speter	atomic_add_int(&_thread_active_threads, 1);
230251877Speter}
231251877Speter
232251877Speter/*
233251877Speter * Remove an active thread.
234251877Speter */
235251877Spetervoid
236251877Speter_thr_unlink(struct pthread *curthread, struct pthread *thread)
237251877Speter{
238251877Speter	THREAD_LIST_WRLOCK(curthread);
239251877Speter	THR_LIST_REMOVE(thread);
240251877Speter	THREAD_LIST_UNLOCK(curthread);
241251877Speter	atomic_add_int(&_thread_active_threads, -1);
242251877Speter}
243251877Speter
244251877Spetervoid
245251877Speter_thr_hash_add(struct pthread *thread)
246251877Speter{
247251877Speter	struct thread_hash_head *head;
248251877Speter
249251877Speter	head = &thr_hashtable[THREAD_HASH(thread)];
250251877Speter	LIST_INSERT_HEAD(head, thread, hle);
251251877Speter}
252251877Speter
253251877Spetervoid
254251877Speter_thr_hash_remove(struct pthread *thread)
255251877Speter{
256251877Speter	LIST_REMOVE(thread, hle);
257251877Speter}
258251877Speter
259251877Speterstruct pthread *
260251877Speter_thr_hash_find(struct pthread *thread)
261251877Speter{
262251877Speter	struct pthread *td;
263251877Speter	struct thread_hash_head *head;
264251877Speter
265251877Speter	head = &thr_hashtable[THREAD_HASH(thread)];
266251877Speter	LIST_FOREACH(td, head, hle) {
267251877Speter		if (td == thread)
268251877Speter			return (thread);
269251877Speter	}
270251877Speter	return (NULL);
271251877Speter}
272251877Speter
273251877Speter/*
274251877Speter * Find a thread in the linked list of active threads and add a reference
275251877Speter * to it.  Threads with positive reference counts will not be deallocated
276251877Speter * until all references are released.
277251877Speter */
278251877Speterint
279251877Speter_thr_ref_add(struct pthread *curthread, struct pthread *thread,
280251877Speter    int include_dead)
281251877Speter{
282251877Speter	int ret;
283251877Speter
284251877Speter	if (thread == NULL)
285251877Speter		/* Invalid thread: */
286251877Speter		return (EINVAL);
287251877Speter
288251877Speter	if ((ret = _thr_find_thread(curthread, thread, include_dead)) == 0) {
289251877Speter		thread->refcount++;
290251877Speter		THR_CRITICAL_ENTER(curthread);
291251877Speter		THR_THREAD_UNLOCK(curthread, thread);
292251877Speter	}
293251877Speter
294251877Speter	/* Return zero if the thread exists: */
295	return (ret);
296}
297
298void
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 */
308void
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 */
329int
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