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