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