1/*
2 * Copyright (c) 2003 Daniel M. Eischen <deischen@freebsd.org>
3 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
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, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by John Birrell.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD$
34 */
35
36#include "namespace.h"
37#include <sys/types.h>
38#include <sys/signalvar.h>
39#include <sys/ioctl.h>
40#include <sys/resource.h>
41#include <sys/sysctl.h>
42#include <sys/ttycom.h>
43#include <sys/mman.h>
44#include <sys/rtprio.h>
45#include <errno.h>
46#include <fcntl.h>
47#include <paths.h>
48#include <pthread.h>
49#include <pthread_np.h>
50#include <signal.h>
51#include <stdlib.h>
52#include <string.h>
53#include <time.h>
54#include <unistd.h>
55#include "un-namespace.h"
56
57#include "libc_private.h"
58#include "thr_private.h"
59
60char		*_usrstack;
61struct pthread	*_thr_initial;
62int		_libthr_debug;
63int		_thread_event_mask;
64struct pthread	*_thread_last_event;
65pthreadlist	_thread_list = TAILQ_HEAD_INITIALIZER(_thread_list);
66pthreadlist 	_thread_gc_list = TAILQ_HEAD_INITIALIZER(_thread_gc_list);
67int		_thread_active_threads = 1;
68atfork_head	_thr_atfork_list = TAILQ_HEAD_INITIALIZER(_thr_atfork_list);
69struct urwlock	_thr_atfork_lock = DEFAULT_URWLOCK;
70
71struct pthread_prio	_thr_priorities[3] = {
72	{RTP_PRIO_MIN,  RTP_PRIO_MAX, 0}, /* FIFO */
73	{0, 0, 63}, /* OTHER */
74	{RTP_PRIO_MIN, RTP_PRIO_MAX, 0}  /* RR */
75};
76
77struct pthread_attr _pthread_attr_default = {
78	.sched_policy = SCHED_OTHER,
79	.sched_inherit = PTHREAD_INHERIT_SCHED,
80	.prio = 0,
81	.suspend = THR_CREATE_RUNNING,
82	.flags = PTHREAD_SCOPE_SYSTEM,
83	.stackaddr_attr = NULL,
84	.stacksize_attr = THR_STACK_DEFAULT,
85	.guardsize_attr = 0,
86	.cpusetsize = 0,
87	.cpuset = NULL
88};
89
90struct pthread_mutex_attr _pthread_mutexattr_default = {
91	.m_type = PTHREAD_MUTEX_DEFAULT,
92	.m_protocol = PTHREAD_PRIO_NONE,
93	.m_ceiling = 0
94};
95
96struct pthread_mutex_attr _pthread_mutexattr_adaptive_default = {
97	.m_type = PTHREAD_MUTEX_ADAPTIVE_NP,
98	.m_protocol = PTHREAD_PRIO_NONE,
99	.m_ceiling = 0
100};
101
102/* Default condition variable attributes: */
103struct pthread_cond_attr _pthread_condattr_default = {
104	.c_pshared = PTHREAD_PROCESS_PRIVATE,
105	.c_clockid = CLOCK_REALTIME
106};
107
108pid_t		_thr_pid;
109int		_thr_is_smp = 0;
110size_t		_thr_guard_default;
111size_t		_thr_stack_default = THR_STACK_DEFAULT;
112size_t		_thr_stack_initial = THR_STACK_INITIAL;
113int		_thr_page_size;
114int		_thr_spinloops;
115int		_thr_yieldloops;
116int		_thr_queuefifo = 4;
117int		_gc_count;
118struct umutex	_mutex_static_lock = DEFAULT_UMUTEX;
119struct umutex	_cond_static_lock = DEFAULT_UMUTEX;
120struct umutex	_rwlock_static_lock = DEFAULT_UMUTEX;
121struct umutex	_keytable_lock = DEFAULT_UMUTEX;
122struct urwlock	_thr_list_lock = DEFAULT_URWLOCK;
123struct umutex	_thr_event_lock = DEFAULT_UMUTEX;
124struct umutex	_suspend_all_lock = DEFAULT_UMUTEX;
125struct pthread	*_single_thread;
126int		_suspend_all_cycle;
127int		_suspend_all_waiters;
128
129int	__pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
130int	__pthread_mutex_lock(pthread_mutex_t *);
131int	__pthread_mutex_trylock(pthread_mutex_t *);
132void	_thread_init_hack(void) __attribute__ ((constructor));
133
134static void init_private(void);
135static void init_main_thread(struct pthread *thread);
136
137/*
138 * All weak references used within libc should be in this table.
139 * This is so that static libraries will work.
140 */
141
142STATIC_LIB_REQUIRE(_fork);
143STATIC_LIB_REQUIRE(_pthread_getspecific);
144STATIC_LIB_REQUIRE(_pthread_key_create);
145STATIC_LIB_REQUIRE(_pthread_key_delete);
146STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
147STATIC_LIB_REQUIRE(_pthread_mutex_init);
148STATIC_LIB_REQUIRE(_pthread_mutex_lock);
149STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
150STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
151STATIC_LIB_REQUIRE(_pthread_mutexattr_init);
152STATIC_LIB_REQUIRE(_pthread_mutexattr_destroy);
153STATIC_LIB_REQUIRE(_pthread_mutexattr_settype);
154STATIC_LIB_REQUIRE(_pthread_once);
155STATIC_LIB_REQUIRE(_pthread_setspecific);
156STATIC_LIB_REQUIRE(_raise);
157STATIC_LIB_REQUIRE(_sem_destroy);
158STATIC_LIB_REQUIRE(_sem_getvalue);
159STATIC_LIB_REQUIRE(_sem_init);
160STATIC_LIB_REQUIRE(_sem_post);
161STATIC_LIB_REQUIRE(_sem_timedwait);
162STATIC_LIB_REQUIRE(_sem_trywait);
163STATIC_LIB_REQUIRE(_sem_wait);
164STATIC_LIB_REQUIRE(_sigaction);
165STATIC_LIB_REQUIRE(_sigprocmask);
166STATIC_LIB_REQUIRE(_sigsuspend);
167STATIC_LIB_REQUIRE(_sigtimedwait);
168STATIC_LIB_REQUIRE(_sigwait);
169STATIC_LIB_REQUIRE(_sigwaitinfo);
170STATIC_LIB_REQUIRE(_spinlock);
171STATIC_LIB_REQUIRE(_spinlock_debug);
172STATIC_LIB_REQUIRE(_spinunlock);
173STATIC_LIB_REQUIRE(_thread_init_hack);
174
175/*
176 * These are needed when linking statically.  All references within
177 * libgcc (and in the future libc) to these routines are weak, but
178 * if they are not (strongly) referenced by the application or other
179 * libraries, then the actual functions will not be loaded.
180 */
181STATIC_LIB_REQUIRE(_pthread_once);
182STATIC_LIB_REQUIRE(_pthread_key_create);
183STATIC_LIB_REQUIRE(_pthread_key_delete);
184STATIC_LIB_REQUIRE(_pthread_getspecific);
185STATIC_LIB_REQUIRE(_pthread_setspecific);
186STATIC_LIB_REQUIRE(_pthread_mutex_init);
187STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
188STATIC_LIB_REQUIRE(_pthread_mutex_lock);
189STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
190STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
191STATIC_LIB_REQUIRE(_pthread_create);
192
193/* Pull in all symbols required by libthread_db */
194STATIC_LIB_REQUIRE(_thread_state_running);
195
196#define	DUAL_ENTRY(entry)	\
197	(pthread_func_t)entry, (pthread_func_t)entry
198
199static pthread_func_t jmp_table[][2] = {
200	{DUAL_ENTRY(_pthread_atfork)},	/* PJT_ATFORK */
201	{DUAL_ENTRY(_pthread_attr_destroy)},	/* PJT_ATTR_DESTROY */
202	{DUAL_ENTRY(_pthread_attr_getdetachstate)},	/* PJT_ATTR_GETDETACHSTATE */
203	{DUAL_ENTRY(_pthread_attr_getguardsize)},	/* PJT_ATTR_GETGUARDSIZE */
204	{DUAL_ENTRY(_pthread_attr_getinheritsched)},	/* PJT_ATTR_GETINHERITSCHED */
205	{DUAL_ENTRY(_pthread_attr_getschedparam)},	/* PJT_ATTR_GETSCHEDPARAM */
206	{DUAL_ENTRY(_pthread_attr_getschedpolicy)},	/* PJT_ATTR_GETSCHEDPOLICY */
207	{DUAL_ENTRY(_pthread_attr_getscope)},	/* PJT_ATTR_GETSCOPE */
208	{DUAL_ENTRY(_pthread_attr_getstackaddr)},	/* PJT_ATTR_GETSTACKADDR */
209	{DUAL_ENTRY(_pthread_attr_getstacksize)},	/* PJT_ATTR_GETSTACKSIZE */
210	{DUAL_ENTRY(_pthread_attr_init)},	/* PJT_ATTR_INIT */
211	{DUAL_ENTRY(_pthread_attr_setdetachstate)},	/* PJT_ATTR_SETDETACHSTATE */
212	{DUAL_ENTRY(_pthread_attr_setguardsize)},	/* PJT_ATTR_SETGUARDSIZE */
213	{DUAL_ENTRY(_pthread_attr_setinheritsched)},	/* PJT_ATTR_SETINHERITSCHED */
214	{DUAL_ENTRY(_pthread_attr_setschedparam)},	/* PJT_ATTR_SETSCHEDPARAM */
215	{DUAL_ENTRY(_pthread_attr_setschedpolicy)},	/* PJT_ATTR_SETSCHEDPOLICY */
216	{DUAL_ENTRY(_pthread_attr_setscope)},	/* PJT_ATTR_SETSCOPE */
217	{DUAL_ENTRY(_pthread_attr_setstackaddr)},	/* PJT_ATTR_SETSTACKADDR */
218	{DUAL_ENTRY(_pthread_attr_setstacksize)},	/* PJT_ATTR_SETSTACKSIZE */
219	{DUAL_ENTRY(_pthread_cancel)},	/* PJT_CANCEL */
220	{DUAL_ENTRY(_pthread_cleanup_pop)},	/* PJT_CLEANUP_POP */
221	{DUAL_ENTRY(_pthread_cleanup_push)},	/* PJT_CLEANUP_PUSH */
222	{DUAL_ENTRY(_pthread_cond_broadcast)},	/* PJT_COND_BROADCAST */
223	{DUAL_ENTRY(_pthread_cond_destroy)},	/* PJT_COND_DESTROY */
224	{DUAL_ENTRY(_pthread_cond_init)},	/* PJT_COND_INIT */
225	{DUAL_ENTRY(_pthread_cond_signal)},	/* PJT_COND_SIGNAL */
226	{DUAL_ENTRY(_pthread_cond_timedwait)},	/* PJT_COND_TIMEDWAIT */
227	{(pthread_func_t)__pthread_cond_wait,
228	 (pthread_func_t)_pthread_cond_wait},	/* PJT_COND_WAIT */
229	{DUAL_ENTRY(_pthread_detach)},	/* PJT_DETACH */
230	{DUAL_ENTRY(_pthread_equal)},	/* PJT_EQUAL */
231	{DUAL_ENTRY(_pthread_exit)},	/* PJT_EXIT */
232	{DUAL_ENTRY(_pthread_getspecific)},	/* PJT_GETSPECIFIC */
233	{DUAL_ENTRY(_pthread_join)},	/* PJT_JOIN */
234	{DUAL_ENTRY(_pthread_key_create)},	/* PJT_KEY_CREATE */
235	{DUAL_ENTRY(_pthread_key_delete)},	/* PJT_KEY_DELETE*/
236	{DUAL_ENTRY(_pthread_kill)},	/* PJT_KILL */
237	{DUAL_ENTRY(_pthread_main_np)},		/* PJT_MAIN_NP */
238	{DUAL_ENTRY(_pthread_mutexattr_destroy)}, /* PJT_MUTEXATTR_DESTROY */
239	{DUAL_ENTRY(_pthread_mutexattr_init)},	/* PJT_MUTEXATTR_INIT */
240	{DUAL_ENTRY(_pthread_mutexattr_settype)}, /* PJT_MUTEXATTR_SETTYPE */
241	{DUAL_ENTRY(_pthread_mutex_destroy)},	/* PJT_MUTEX_DESTROY */
242	{DUAL_ENTRY(_pthread_mutex_init)},	/* PJT_MUTEX_INIT */
243	{(pthread_func_t)__pthread_mutex_lock,
244	 (pthread_func_t)_pthread_mutex_lock},	/* PJT_MUTEX_LOCK */
245	{(pthread_func_t)__pthread_mutex_trylock,
246	 (pthread_func_t)_pthread_mutex_trylock},/* PJT_MUTEX_TRYLOCK */
247	{DUAL_ENTRY(_pthread_mutex_unlock)},	/* PJT_MUTEX_UNLOCK */
248	{DUAL_ENTRY(_pthread_once)},		/* PJT_ONCE */
249	{DUAL_ENTRY(_pthread_rwlock_destroy)},	/* PJT_RWLOCK_DESTROY */
250	{DUAL_ENTRY(_pthread_rwlock_init)},	/* PJT_RWLOCK_INIT */
251	{DUAL_ENTRY(_pthread_rwlock_rdlock)},	/* PJT_RWLOCK_RDLOCK */
252	{DUAL_ENTRY(_pthread_rwlock_tryrdlock)},/* PJT_RWLOCK_TRYRDLOCK */
253	{DUAL_ENTRY(_pthread_rwlock_trywrlock)},/* PJT_RWLOCK_TRYWRLOCK */
254	{DUAL_ENTRY(_pthread_rwlock_unlock)},	/* PJT_RWLOCK_UNLOCK */
255	{DUAL_ENTRY(_pthread_rwlock_wrlock)},	/* PJT_RWLOCK_WRLOCK */
256	{DUAL_ENTRY(_pthread_self)},		/* PJT_SELF */
257	{DUAL_ENTRY(_pthread_setcancelstate)},	/* PJT_SETCANCELSTATE */
258	{DUAL_ENTRY(_pthread_setcanceltype)},	/* PJT_SETCANCELTYPE */
259	{DUAL_ENTRY(_pthread_setspecific)},	/* PJT_SETSPECIFIC */
260	{DUAL_ENTRY(_pthread_sigmask)},		/* PJT_SIGMASK */
261	{DUAL_ENTRY(_pthread_testcancel)},	/* PJT_TESTCANCEL */
262	{DUAL_ENTRY(__pthread_cleanup_pop_imp)},/* PJT_CLEANUP_POP_IMP */
263	{DUAL_ENTRY(__pthread_cleanup_push_imp)},/* PJT_CLEANUP_PUSH_IMP */
264	{DUAL_ENTRY(_pthread_cancel_enter)},	/* PJT_CANCEL_ENTER */
265	{DUAL_ENTRY(_pthread_cancel_leave)}		/* PJT_CANCEL_LEAVE */
266};
267
268static int init_once = 0;
269
270/*
271 * For the shared version of the threads library, the above is sufficient.
272 * But for the archive version of the library, we need a little bit more.
273 * Namely, we must arrange for this particular module to be pulled in from
274 * the archive library at link time.  To accomplish that, we define and
275 * initialize a variable, "_thread_autoinit_dummy_decl".  This variable is
276 * referenced (as an extern) from libc/stdlib/exit.c. This will always
277 * create a need for this module, ensuring that it is present in the
278 * executable.
279 */
280extern int _thread_autoinit_dummy_decl;
281int _thread_autoinit_dummy_decl = 0;
282
283void
284_thread_init_hack(void)
285{
286
287	_libpthread_init(NULL);
288}
289
290
291/*
292 * Threaded process initialization.
293 *
294 * This is only called under two conditions:
295 *
296 *   1) Some thread routines have detected that the library hasn't yet
297 *      been initialized (_thr_initial == NULL && curthread == NULL), or
298 *
299 *   2) An explicit call to reinitialize after a fork (indicated
300 *      by curthread != NULL)
301 */
302void
303_libpthread_init(struct pthread *curthread)
304{
305	int fd, first = 0;
306
307	/* Check if this function has already been called: */
308	if ((_thr_initial != NULL) && (curthread == NULL))
309		/* Only initialize the threaded application once. */
310		return;
311
312	/*
313	 * Check the size of the jump table to make sure it is preset
314	 * with the correct number of entries.
315	 */
316	if (sizeof(jmp_table) != (sizeof(pthread_func_t) * PJT_MAX * 2))
317		PANIC("Thread jump table not properly initialized");
318	memcpy(__thr_jtable, jmp_table, sizeof(jmp_table));
319
320	/*
321	 * Check for the special case of this process running as
322	 * or in place of init as pid = 1:
323	 */
324	if ((_thr_pid = getpid()) == 1) {
325		/*
326		 * Setup a new session for this process which is
327		 * assumed to be running as root.
328		 */
329		if (setsid() == -1)
330			PANIC("Can't set session ID");
331		if (revoke(_PATH_CONSOLE) != 0)
332			PANIC("Can't revoke console");
333		if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0)
334			PANIC("Can't open console");
335		if (setlogin("root") == -1)
336			PANIC("Can't set login to root");
337		if (_ioctl(fd, TIOCSCTTY, (char *) NULL) == -1)
338			PANIC("Can't set controlling terminal");
339	}
340
341	/* Initialize pthread private data. */
342	init_private();
343
344	/* Set the initial thread. */
345	if (curthread == NULL) {
346		first = 1;
347		/* Create and initialize the initial thread. */
348		curthread = _thr_alloc(NULL);
349		if (curthread == NULL)
350			PANIC("Can't allocate initial thread");
351		init_main_thread(curthread);
352	}
353	/*
354	 * Add the thread to the thread list queue.
355	 */
356	THR_LIST_ADD(curthread);
357	_thread_active_threads = 1;
358
359	/* Setup the thread specific data */
360	_tcb_set(curthread->tcb);
361
362	if (first) {
363		_thr_initial = curthread;
364		_thr_signal_init();
365		if (_thread_event_mask & TD_CREATE)
366			_thr_report_creation(curthread, curthread);
367		/*
368		 * Always use our rtld lock implementation.
369		 * It is faster because it postpones signal handlers
370		 * instead of calling sigprocmask(2).
371		 */
372		_thr_rtld_init();
373	}
374}
375
376/*
377 * This function and pthread_create() do a lot of the same things.
378 * It'd be nice to consolidate the common stuff in one place.
379 */
380static void
381init_main_thread(struct pthread *thread)
382{
383	struct sched_param sched_param;
384
385	/* Setup the thread attributes. */
386	thr_self(&thread->tid);
387	thread->attr = _pthread_attr_default;
388	/*
389	 * Set up the thread stack.
390	 *
391	 * Create a red zone below the main stack.  All other stacks
392	 * are constrained to a maximum size by the parameters
393	 * passed to mmap(), but this stack is only limited by
394	 * resource limits, so this stack needs an explicitly mapped
395	 * red zone to protect the thread stack that is just beyond.
396	 */
397	if (mmap(_usrstack - _thr_stack_initial -
398	    _thr_guard_default, _thr_guard_default, 0, MAP_ANON,
399	    -1, 0) == MAP_FAILED)
400		PANIC("Cannot allocate red zone for initial thread");
401
402	/*
403	 * Mark the stack as an application supplied stack so that it
404	 * isn't deallocated.
405	 *
406	 * XXX - I'm not sure it would hurt anything to deallocate
407	 *       the main thread stack because deallocation doesn't
408	 *       actually free() it; it just puts it in the free
409	 *       stack queue for later reuse.
410	 */
411	thread->attr.stackaddr_attr = _usrstack - _thr_stack_initial;
412	thread->attr.stacksize_attr = _thr_stack_initial;
413	thread->attr.guardsize_attr = _thr_guard_default;
414	thread->attr.flags |= THR_STACK_USER;
415
416	/*
417	 * Write a magic value to the thread structure
418	 * to help identify valid ones:
419	 */
420	thread->magic = THR_MAGIC;
421
422	thread->cancel_enable = 1;
423	thread->cancel_async = 0;
424
425	/* Initialize the mutex queue: */
426	TAILQ_INIT(&thread->mutexq);
427	TAILQ_INIT(&thread->pp_mutexq);
428
429	thread->state = PS_RUNNING;
430
431	_thr_getscheduler(thread->tid, &thread->attr.sched_policy,
432		 &sched_param);
433	thread->attr.prio = sched_param.sched_priority;
434
435#ifdef _PTHREAD_FORCED_UNWIND
436	thread->unwind_stackend = _usrstack;
437#endif
438
439	/* Others cleared to zero by thr_alloc() */
440}
441
442static void
443init_private(void)
444{
445	struct rlimit rlim;
446	size_t len;
447	int mib[2];
448	char *env;
449
450	_thr_umutex_init(&_mutex_static_lock);
451	_thr_umutex_init(&_cond_static_lock);
452	_thr_umutex_init(&_rwlock_static_lock);
453	_thr_umutex_init(&_keytable_lock);
454	_thr_urwlock_init(&_thr_atfork_lock);
455	_thr_umutex_init(&_thr_event_lock);
456	_thr_umutex_init(&_suspend_all_lock);
457	_thr_once_init();
458	_thr_spinlock_init();
459	_thr_list_init();
460	_thr_wake_addr_init();
461	_sleepq_init();
462	_single_thread = NULL;
463	_suspend_all_waiters = 0;
464
465	/*
466	 * Avoid reinitializing some things if they don't need to be,
467	 * e.g. after a fork().
468	 */
469	if (init_once == 0) {
470		/* Find the stack top */
471		mib[0] = CTL_KERN;
472		mib[1] = KERN_USRSTACK;
473		len = sizeof (_usrstack);
474		if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
475			PANIC("Cannot get kern.usrstack from sysctl");
476		env = getenv("LIBPTHREAD_BIGSTACK_MAIN");
477		if (env != NULL) {
478			if (getrlimit(RLIMIT_STACK, &rlim) == -1)
479				PANIC("Cannot get stack rlimit");
480			_thr_stack_initial = rlim.rlim_cur;
481		}
482		len = sizeof(_thr_is_smp);
483		sysctlbyname("kern.smp.cpus", &_thr_is_smp, &len, NULL, 0);
484		_thr_is_smp = (_thr_is_smp > 1);
485		_thr_page_size = getpagesize();
486		_thr_guard_default = _thr_page_size;
487		_pthread_attr_default.guardsize_attr = _thr_guard_default;
488		_pthread_attr_default.stacksize_attr = _thr_stack_default;
489		env = getenv("LIBPTHREAD_SPINLOOPS");
490		if (env)
491			_thr_spinloops = atoi(env);
492		env = getenv("LIBPTHREAD_YIELDLOOPS");
493		if (env)
494			_thr_yieldloops = atoi(env);
495		env = getenv("LIBPTHREAD_QUEUE_FIFO");
496		if (env)
497			_thr_queuefifo = atoi(env);
498		TAILQ_INIT(&_thr_atfork_list);
499	}
500	init_once = 1;
501}
502