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