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