thr_init.c revision 156901
136285Sbrian/*
236285Sbrian * Copyright (c) 2003 Daniel M. Eischen <deischen@freebsd.org>
336285Sbrian * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
436285Sbrian * All rights reserved.
536285Sbrian *
636285Sbrian * Redistribution and use in source and binary forms, with or without
736285Sbrian * modification, are permitted provided that the following conditions
836285Sbrian * are met:
936285Sbrian * 1. Redistributions of source code must retain the above copyright
1036285Sbrian *    notice, this list of conditions and the following disclaimer.
1136285Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1236285Sbrian *    notice, this list of conditions and the following disclaimer in the
1336285Sbrian *    documentation and/or other materials provided with the distribution.
1436285Sbrian * 3. All advertising materials mentioning features or use of this software
1536285Sbrian *    must display the following acknowledgement:
1636285Sbrian *	This product includes software developed by John Birrell.
1736285Sbrian * 4. Neither the name of the author nor the names of any co-contributors
1836285Sbrian *    may be used to endorse or promote products derived from this software
1950479Speter *    without specific prior written permission.
2036285Sbrian *
2136285Sbrian * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
2236285Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2346686Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2436285Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2546686Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2646686Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2746686Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2846686Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2946686Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3036285Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3149472Sbrian * SUCH DAMAGE.
3249472Sbrian *
3349472Sbrian * $FreeBSD: head/lib/libthr/thread/thr_init.c 156901 2006-03-20 03:14:14Z davidxu $
3449472Sbrian */
3549472Sbrian
3649472Sbrian#include "namespace.h"
3746686Sbrian#include <sys/types.h>
3849472Sbrian#include <sys/signalvar.h>
3949472Sbrian#include <sys/ioctl.h>
4049472Sbrian#include <sys/sysctl.h>
4149472Sbrian#include <sys/ttycom.h>
4249472Sbrian#include <sys/mman.h>
4346686Sbrian#include <errno.h>
4446686Sbrian#include <fcntl.h>
4546686Sbrian#include <paths.h>
4647061Sbrian#include <pthread.h>
4749472Sbrian#include <pthread_np.h>
4846686Sbrian#include <signal.h>
4946686Sbrian#include <stdlib.h>
5046686Sbrian#include <string.h>
5147061Sbrian#include <time.h>
5247061Sbrian#include <unistd.h>
5347061Sbrian#include "un-namespace.h"
5447061Sbrian
5547769Sbrian#include "libc_private.h"
5646686Sbrian#include "thr_private.h"
5746686Sbrian
5846686Sbrianvoid		*_usrstack;
5946686Sbrianstruct pthread	*_thr_initial;
6036285Sbrianint		_thr_scope_system;
6136285Sbrianint		_libthr_debug;
6236285Sbrianint		_thread_event_mask;
6336285Sbrianstruct pthread	*_thread_last_event;
6436285Sbrianpthreadlist	_thread_list = TAILQ_HEAD_INITIALIZER(_thread_list);
6536285Sbrianpthreadlist 	_thread_gc_list = TAILQ_HEAD_INITIALIZER(_thread_gc_list);
6636285Sbrianint		_thread_active_threads = 1;
6736285Sbrianatfork_head	_thr_atfork_list = TAILQ_HEAD_INITIALIZER(_thr_atfork_list);
6836285Sbrianumtx_t		_thr_atfork_lock;
6936285Sbrian
7036285Sbrianstruct pthread_attr _pthread_attr_default = {
7136285Sbrian	.sched_policy = SCHED_RR,
7245264Sbrian	.sched_inherit = 0,
7345264Sbrian	.sched_interval = TIMESLICE_USEC,
7445264Sbrian	.prio = THR_DEFAULT_PRIORITY,
7545264Sbrian	.suspend = THR_CREATE_RUNNING,
7645264Sbrian	.flags = PTHREAD_SCOPE_SYSTEM,
7747682Sbrian	.arg_attr = NULL,
7836285Sbrian	.cleanup_attr = NULL,
7936285Sbrian	.stackaddr_attr = NULL,
8036285Sbrian	.stacksize_attr = THR_STACK_DEFAULT,
8147061Sbrian	.guardsize_attr = 0
8236467Sbrian};
8336285Sbrian
8447061Sbrianstruct pthread_mutex_attr _pthread_mutexattr_default = {
8547061Sbrian	.m_type = PTHREAD_MUTEX_DEFAULT,
8646686Sbrian	.m_protocol = PTHREAD_PRIO_NONE,
8746686Sbrian	.m_ceiling = 0,
8846686Sbrian	.m_flags = 0
8946686Sbrian};
9036285Sbrian
9146102Sbrian/* Default condition variable attributes: */
9246102Sbrianstruct pthread_cond_attr _pthread_condattr_default = {
9344073Sbrian	.c_pshared = PTHREAD_PROCESS_PRIVATE,
9444073Sbrian	.c_clockid = CLOCK_REALTIME
9544073Sbrian};
9644073Sbrian
9736285Sbrianpid_t		_thr_pid;
9836285Sbrianint		_thr_guard_default;
9936285Sbrianint		_thr_stack_default = THR_STACK_DEFAULT;
10036285Sbrianint		_thr_stack_initial = THR_STACK_INITIAL;
10136285Sbrianint		_thr_page_size;
10236285Sbrianint		_gc_count;
10336285Sbrianumtx_t		_mutex_static_lock;
10436285Sbrianumtx_t		_cond_static_lock;
10536285Sbrianumtx_t		_rwlock_static_lock;
10636285Sbrianumtx_t		_keytable_lock;
10736285Sbrianumtx_t		_thr_list_lock;
10836285Sbrianumtx_t		_thr_event_lock;
10947061Sbrian
11047061Sbrianint	__pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
11147061Sbrianint	__pthread_mutex_lock(pthread_mutex_t *);
11247061Sbrianint	__pthread_mutex_trylock(pthread_mutex_t *);
11346686Sbrianvoid	_thread_init_hack(void) __attribute__ ((constructor));
11446686Sbrian
11546686Sbrianstatic void init_private(void);
11646686Sbrianstatic void init_main_thread(struct pthread *thread);
11746686Sbrian
11846686Sbrian/*
11946686Sbrian * All weak references used within libc should be in this table.
12046686Sbrian * This is so that static libraries will work.
12146686Sbrian */
12246686SbrianSTATIC_LIB_REQUIRE(_accept);
12346686SbrianSTATIC_LIB_REQUIRE(_bind);
12446686SbrianSTATIC_LIB_REQUIRE(_close);
12546686SbrianSTATIC_LIB_REQUIRE(_connect);
12646686SbrianSTATIC_LIB_REQUIRE(_dup);
12746686SbrianSTATIC_LIB_REQUIRE(_dup2);
12846686SbrianSTATIC_LIB_REQUIRE(_execve);
12946686SbrianSTATIC_LIB_REQUIRE(_fcntl);
13036285SbrianSTATIC_LIB_REQUIRE(_flock);
13136285SbrianSTATIC_LIB_REQUIRE(_flockfile);
13236285SbrianSTATIC_LIB_REQUIRE(_fstat);
13346686SbrianSTATIC_LIB_REQUIRE(_fstatfs);
13436285SbrianSTATIC_LIB_REQUIRE(_fsync);
13536285SbrianSTATIC_LIB_REQUIRE(_getdirentries);
13636285SbrianSTATIC_LIB_REQUIRE(_getlogin);
13746686SbrianSTATIC_LIB_REQUIRE(_getpeername);
13846686SbrianSTATIC_LIB_REQUIRE(_getsockname);
13936285SbrianSTATIC_LIB_REQUIRE(_getsockopt);
14036285SbrianSTATIC_LIB_REQUIRE(_ioctl);
14136285SbrianSTATIC_LIB_REQUIRE(_kevent);
14236285SbrianSTATIC_LIB_REQUIRE(_listen);
14336285SbrianSTATIC_LIB_REQUIRE(_nanosleep);
14438544SbrianSTATIC_LIB_REQUIRE(_open);
14547461SbrianSTATIC_LIB_REQUIRE(_pthread_getspecific);
14647061SbrianSTATIC_LIB_REQUIRE(_pthread_key_create);
14747769SbrianSTATIC_LIB_REQUIRE(_pthread_key_delete);
14849472SbrianSTATIC_LIB_REQUIRE(_pthread_mutex_destroy);
149STATIC_LIB_REQUIRE(_pthread_mutex_init);
150STATIC_LIB_REQUIRE(_pthread_mutex_lock);
151STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
152STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
153STATIC_LIB_REQUIRE(_pthread_mutexattr_init);
154STATIC_LIB_REQUIRE(_pthread_mutexattr_destroy);
155STATIC_LIB_REQUIRE(_pthread_mutexattr_settype);
156STATIC_LIB_REQUIRE(_pthread_once);
157STATIC_LIB_REQUIRE(_pthread_setspecific);
158STATIC_LIB_REQUIRE(_read);
159STATIC_LIB_REQUIRE(_readv);
160STATIC_LIB_REQUIRE(_recvfrom);
161STATIC_LIB_REQUIRE(_recvmsg);
162STATIC_LIB_REQUIRE(_select);
163STATIC_LIB_REQUIRE(_sendmsg);
164STATIC_LIB_REQUIRE(_sendto);
165STATIC_LIB_REQUIRE(_setsockopt);
166STATIC_LIB_REQUIRE(_sigaction);
167STATIC_LIB_REQUIRE(_sigprocmask);
168STATIC_LIB_REQUIRE(_sigsuspend);
169STATIC_LIB_REQUIRE(_socket);
170STATIC_LIB_REQUIRE(_socketpair);
171STATIC_LIB_REQUIRE(_thread_init_hack);
172STATIC_LIB_REQUIRE(_wait4);
173STATIC_LIB_REQUIRE(_write);
174STATIC_LIB_REQUIRE(_writev);
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 */
182STATIC_LIB_REQUIRE(_pthread_once);
183STATIC_LIB_REQUIRE(_pthread_key_create);
184STATIC_LIB_REQUIRE(_pthread_key_delete);
185STATIC_LIB_REQUIRE(_pthread_getspecific);
186STATIC_LIB_REQUIRE(_pthread_setspecific);
187STATIC_LIB_REQUIRE(_pthread_mutex_init);
188STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
189STATIC_LIB_REQUIRE(_pthread_mutex_lock);
190STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
191STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
192STATIC_LIB_REQUIRE(_pthread_create);
193
194/* Pull in all symbols required by libthread_db */
195STATIC_LIB_REQUIRE(_thread_state_running);
196
197#define	DUAL_ENTRY(entry)	\
198	(pthread_func_t)entry, (pthread_func_t)entry
199
200static 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};
264
265static int init_once = 0;
266
267/*
268 * For the shared version of the threads library, the above is sufficient.
269 * But for the archive version of the library, we need a little bit more.
270 * Namely, we must arrange for this particular module to be pulled in from
271 * the archive library at link time.  To accomplish that, we define and
272 * initialize a variable, "_thread_autoinit_dummy_decl".  This variable is
273 * referenced (as an extern) from libc/stdlib/exit.c. This will always
274 * create a need for this module, ensuring that it is present in the
275 * executable.
276 */
277extern int _thread_autoinit_dummy_decl;
278int _thread_autoinit_dummy_decl = 0;
279
280void
281_thread_init_hack(void)
282{
283
284	_libpthread_init(NULL);
285}
286
287
288/*
289 * Threaded process initialization.
290 *
291 * This is only called under two conditions:
292 *
293 *   1) Some thread routines have detected that the library hasn't yet
294 *      been initialized (_thr_initial == NULL && curthread == NULL), or
295 *
296 *   2) An explicit call to reinitialize after a fork (indicated
297 *      by curthread != NULL)
298 */
299void
300_libpthread_init(struct pthread *curthread)
301{
302	int fd, first = 0;
303	sigset_t sigset, oldset;
304
305	/* Check if this function has already been called: */
306	if ((_thr_initial != NULL) && (curthread == NULL))
307		/* Only initialize the threaded application once. */
308		return;
309
310	/*
311	 * Check the size of the jump table to make sure it is preset
312	 * with the correct number of entries.
313	 */
314	if (sizeof(jmp_table) != (sizeof(pthread_func_t) * PJT_MAX * 2))
315		PANIC("Thread jump table not properly initialized");
316	memcpy(__thr_jtable, jmp_table, sizeof(jmp_table));
317
318	/*
319	 * Check for the special case of this process running as
320	 * or in place of init as pid = 1:
321	 */
322	if ((_thr_pid = getpid()) == 1) {
323		/*
324		 * Setup a new session for this process which is
325		 * assumed to be running as root.
326		 */
327		if (setsid() == -1)
328			PANIC("Can't set session ID");
329		if (revoke(_PATH_CONSOLE) != 0)
330			PANIC("Can't revoke console");
331		if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0)
332			PANIC("Can't open console");
333		if (setlogin("root") == -1)
334			PANIC("Can't set login to root");
335		if (__sys_ioctl(fd, TIOCSCTTY, (char *) NULL) == -1)
336			PANIC("Can't set controlling terminal");
337	}
338
339	/* Initialize pthread private data. */
340	init_private();
341
342	/* Set the initial thread. */
343	if (curthread == NULL) {
344		first = 1;
345		/* Create and initialize the initial thread. */
346		curthread = _thr_alloc(NULL);
347		if (curthread == NULL)
348			PANIC("Can't allocate initial thread");
349		init_main_thread(curthread);
350	}
351	/*
352	 * Add the thread to the thread list queue.
353	 */
354	THR_LIST_ADD(curthread);
355	_thread_active_threads = 1;
356
357	/* Setup the thread specific data */
358	_tcb_set(curthread->tcb);
359
360	if (first) {
361		SIGFILLSET(sigset);
362		SIGDELSET(sigset, SIGTRAP);
363		__sys_sigprocmask(SIG_SETMASK, &sigset, &oldset);
364		_thr_signal_init();
365		_thr_initial = curthread;
366		SIGDELSET(oldset, SIGCANCEL);
367		__sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
368		if (_thread_event_mask & TD_CREATE)
369			_thr_report_creation(curthread, curthread);
370	}
371}
372
373/*
374 * This function and pthread_create() do a lot of the same things.
375 * It'd be nice to consolidate the common stuff in one place.
376 */
377static void
378init_main_thread(struct pthread *thread)
379{
380	/* Setup the thread attributes. */
381	thr_self(&thread->tid);
382	thread->attr = _pthread_attr_default;
383	/*
384	 * Set up the thread stack.
385	 *
386	 * Create a red zone below the main stack.  All other stacks
387	 * are constrained to a maximum size by the parameters
388	 * passed to mmap(), but this stack is only limited by
389	 * resource limits, so this stack needs an explicitly mapped
390	 * red zone to protect the thread stack that is just beyond.
391	 */
392	if (mmap((void *)_usrstack - _thr_stack_initial -
393	    _thr_guard_default, _thr_guard_default, 0, MAP_ANON,
394	    -1, 0) == MAP_FAILED)
395		PANIC("Cannot allocate red zone for initial thread");
396
397	/*
398	 * Mark the stack as an application supplied stack so that it
399	 * isn't deallocated.
400	 *
401	 * XXX - I'm not sure it would hurt anything to deallocate
402	 *       the main thread stack because deallocation doesn't
403	 *       actually free() it; it just puts it in the free
404	 *       stack queue for later reuse.
405	 */
406	thread->attr.stackaddr_attr = (void *)_usrstack - _thr_stack_initial;
407	thread->attr.stacksize_attr = _thr_stack_initial;
408	thread->attr.guardsize_attr = _thr_guard_default;
409	thread->attr.flags |= THR_STACK_USER;
410
411	/*
412	 * Write a magic value to the thread structure
413	 * to help identify valid ones:
414	 */
415	thread->magic = THR_MAGIC;
416
417	thread->cancelflags = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED;
418	thr_set_name(thread->tid, "initial thread");
419
420	/* Default the priority of the initial thread: */
421	thread->base_priority = THR_DEFAULT_PRIORITY;
422	thread->active_priority = THR_DEFAULT_PRIORITY;
423	thread->inherited_priority = 0;
424
425	/* Initialize the mutex queue: */
426	TAILQ_INIT(&thread->mutexq);
427	TAILQ_INIT(&thread->pri_mutexq);
428
429	thread->state = PS_RUNNING;
430
431	/* Others cleared to zero by thr_alloc() */
432}
433
434static void
435init_private(void)
436{
437	size_t len;
438	int mib[2];
439
440	_thr_umtx_init(&_mutex_static_lock);
441	_thr_umtx_init(&_cond_static_lock);
442	_thr_umtx_init(&_rwlock_static_lock);
443	_thr_umtx_init(&_keytable_lock);
444	_thr_umtx_init(&_thr_atfork_lock);
445	_thr_umtx_init(&_thr_event_lock);
446	_thr_once_init();
447	_thr_spinlock_init();
448	_thr_list_init();
449
450	/*
451	 * Avoid reinitializing some things if they don't need to be,
452	 * e.g. after a fork().
453	 */
454	if (init_once == 0) {
455		/* Find the stack top */
456		mib[0] = CTL_KERN;
457		mib[1] = KERN_USRSTACK;
458		len = sizeof (_usrstack);
459		if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
460			PANIC("Cannot get kern.usrstack from sysctl");
461		_thr_page_size = getpagesize();
462		_thr_guard_default = _thr_page_size;
463		_pthread_attr_default.guardsize_attr = _thr_guard_default;
464		_pthread_attr_default.stacksize_attr = _thr_stack_default;
465
466		TAILQ_INIT(&_thr_atfork_list);
467#ifdef SYSTEM_SCOPE_ONLY
468		_thr_scope_system = 1;
469#else
470		if (getenv("LIBPTHREAD_SYSTEM_SCOPE") != NULL)
471			_thr_scope_system = 1;
472		else if (getenv("LIBPTHREAD_PROCESS_SCOPE") != NULL)
473			_thr_scope_system = -1;
474#endif
475	}
476	init_once = 1;
477}
478