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 "namespace.h" 37 #include <sys/param.h> 38 #include <sys/auxv.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(_spinunlock); 177 STATIC_LIB_REQUIRE(_thread_init_hack); 178 179 /* 180 * These are needed when linking statically. All references within 181 * libgcc (and in the future libc) to these routines are weak, but 182 * if they are not (strongly) referenced by the application or other 183 * libraries, then the actual functions will not be loaded. 184 */ 185 STATIC_LIB_REQUIRE(_pthread_once); 186 STATIC_LIB_REQUIRE(_pthread_key_create); 187 STATIC_LIB_REQUIRE(_pthread_key_delete); 188 STATIC_LIB_REQUIRE(_pthread_getspecific); 189 STATIC_LIB_REQUIRE(_pthread_setspecific); 190 STATIC_LIB_REQUIRE(_pthread_mutex_init); 191 STATIC_LIB_REQUIRE(_pthread_mutex_destroy); 192 STATIC_LIB_REQUIRE(_pthread_mutex_lock); 193 STATIC_LIB_REQUIRE(_pthread_mutex_trylock); 194 STATIC_LIB_REQUIRE(_pthread_mutex_unlock); 195 STATIC_LIB_REQUIRE(_pthread_create); 196 197 /* Pull in all symbols required by libthread_db */ 198 STATIC_LIB_REQUIRE(_thread_state_running); 199 200 #define DUAL_ENTRY(entry) \ 201 (pthread_func_t)entry, (pthread_func_t)entry 202 203 static pthread_func_t jmp_table[][2] = { 204 [PJT_ATFORK] = {DUAL_ENTRY(_thr_atfork)}, 205 [PJT_ATTR_DESTROY] = {DUAL_ENTRY(_thr_attr_destroy)}, 206 [PJT_ATTR_GETDETACHSTATE] = {DUAL_ENTRY(_thr_attr_getdetachstate)}, 207 [PJT_ATTR_GETGUARDSIZE] = {DUAL_ENTRY(_thr_attr_getguardsize)}, 208 [PJT_ATTR_GETINHERITSCHED] = {DUAL_ENTRY(_thr_attr_getinheritsched)}, 209 [PJT_ATTR_GETSCHEDPARAM] = {DUAL_ENTRY(_thr_attr_getschedparam)}, 210 [PJT_ATTR_GETSCHEDPOLICY] = {DUAL_ENTRY(_thr_attr_getschedpolicy)}, 211 [PJT_ATTR_GETSCOPE] = {DUAL_ENTRY(_thr_attr_getscope)}, 212 [PJT_ATTR_GETSTACKADDR] = {DUAL_ENTRY(_thr_attr_getstackaddr)}, 213 [PJT_ATTR_GETSTACKSIZE] = {DUAL_ENTRY(_thr_attr_getstacksize)}, 214 [PJT_ATTR_INIT] = {DUAL_ENTRY(_thr_attr_init)}, 215 [PJT_ATTR_SETDETACHSTATE] = {DUAL_ENTRY(_thr_attr_setdetachstate)}, 216 [PJT_ATTR_SETGUARDSIZE] = {DUAL_ENTRY(_thr_attr_setguardsize)}, 217 [PJT_ATTR_SETINHERITSCHED] = {DUAL_ENTRY(_thr_attr_setinheritsched)}, 218 [PJT_ATTR_SETSCHEDPARAM] = {DUAL_ENTRY(_thr_attr_setschedparam)}, 219 [PJT_ATTR_SETSCHEDPOLICY] = {DUAL_ENTRY(_thr_attr_setschedpolicy)}, 220 [PJT_ATTR_SETSCOPE] = {DUAL_ENTRY(_thr_attr_setscope)}, 221 [PJT_ATTR_SETSTACKADDR] = {DUAL_ENTRY(_thr_attr_setstackaddr)}, 222 [PJT_ATTR_SETSTACKSIZE] = {DUAL_ENTRY(_thr_attr_setstacksize)}, 223 [PJT_CANCEL] = {DUAL_ENTRY(_thr_cancel)}, 224 [PJT_CLEANUP_POP] = {DUAL_ENTRY(_thr_cleanup_pop)}, 225 [PJT_CLEANUP_PUSH] = {DUAL_ENTRY(_thr_cleanup_push)}, 226 [PJT_COND_BROADCAST] = {DUAL_ENTRY(_thr_cond_broadcast)}, 227 [PJT_COND_DESTROY] = {DUAL_ENTRY(_thr_cond_destroy)}, 228 [PJT_COND_INIT] = {DUAL_ENTRY(_thr_cond_init)}, 229 [PJT_COND_SIGNAL] = {DUAL_ENTRY(_thr_cond_signal)}, 230 [PJT_COND_TIMEDWAIT] = {DUAL_ENTRY(_thr_cond_timedwait)}, 231 [PJT_COND_WAIT] = {(pthread_func_t)__thr_cond_wait, 232 (pthread_func_t)_thr_cond_wait}, 233 [PJT_DETACH] = {DUAL_ENTRY(_thr_detach)}, 234 [PJT_EQUAL] = {DUAL_ENTRY(_thr_equal)}, 235 [PJT_EXIT] = {DUAL_ENTRY(_Tthr_exit)}, 236 [PJT_GETSPECIFIC] = {DUAL_ENTRY(_thr_getspecific)}, 237 [PJT_JOIN] = {DUAL_ENTRY(_thr_join)}, 238 [PJT_KEY_CREATE] = {DUAL_ENTRY(_thr_key_create)}, 239 [PJT_KEY_DELETE] = {DUAL_ENTRY(_thr_key_delete)}, 240 [PJT_KILL] = {DUAL_ENTRY(_Tthr_kill)}, 241 [PJT_MAIN_NP] = {DUAL_ENTRY(_thr_main_np)}, 242 [PJT_MUTEXATTR_DESTROY] = {DUAL_ENTRY(_thr_mutexattr_destroy)}, 243 [PJT_MUTEXATTR_INIT] = {DUAL_ENTRY(_thr_mutexattr_init)}, 244 [PJT_MUTEXATTR_SETTYPE] = {DUAL_ENTRY(_thr_mutexattr_settype)}, 245 [PJT_MUTEX_DESTROY] = {DUAL_ENTRY(_thr_mutex_destroy)}, 246 [PJT_MUTEX_INIT] = {DUAL_ENTRY(__Tthr_mutex_init)}, 247 [PJT_MUTEX_LOCK] = {DUAL_ENTRY(__Tthr_mutex_lock)}, 248 [PJT_MUTEX_TRYLOCK] = {DUAL_ENTRY(__Tthr_mutex_trylock)}, 249 [PJT_MUTEX_UNLOCK] = {DUAL_ENTRY(_thr_mutex_unlock)}, 250 [PJT_ONCE] = {DUAL_ENTRY(_thr_once)}, 251 [PJT_RWLOCK_DESTROY] = {DUAL_ENTRY(_thr_rwlock_destroy)}, 252 [PJT_RWLOCK_INIT] = {DUAL_ENTRY(_thr_rwlock_init)}, 253 [PJT_RWLOCK_RDLOCK] = {DUAL_ENTRY(_Tthr_rwlock_rdlock)}, 254 [PJT_RWLOCK_TRYRDLOCK] = {DUAL_ENTRY(_Tthr_rwlock_tryrdlock)}, 255 [PJT_RWLOCK_TRYWRLOCK] = {DUAL_ENTRY(_Tthr_rwlock_trywrlock)}, 256 [PJT_RWLOCK_UNLOCK] = {DUAL_ENTRY(_Tthr_rwlock_unlock)}, 257 [PJT_RWLOCK_WRLOCK] = {DUAL_ENTRY(_Tthr_rwlock_wrlock)}, 258 [PJT_SELF] = {DUAL_ENTRY(_Tthr_self)}, 259 [PJT_SETCANCELSTATE] = {DUAL_ENTRY(_thr_setcancelstate)}, 260 [PJT_SETCANCELTYPE] = {DUAL_ENTRY(_thr_setcanceltype)}, 261 [PJT_SETSPECIFIC] = {DUAL_ENTRY(_thr_setspecific)}, 262 [PJT_SIGMASK] = {DUAL_ENTRY(_thr_sigmask)}, 263 [PJT_TESTCANCEL] = {DUAL_ENTRY(_Tthr_testcancel)}, 264 [PJT_CLEANUP_POP_IMP] = {DUAL_ENTRY(__thr_cleanup_pop_imp)}, 265 [PJT_CLEANUP_PUSH_IMP] = {DUAL_ENTRY(__thr_cleanup_push_imp)}, 266 [PJT_CANCEL_ENTER] = {DUAL_ENTRY(_Tthr_cancel_enter)}, 267 [PJT_CANCEL_LEAVE] = {DUAL_ENTRY(_Tthr_cancel_leave)}, 268 [PJT_MUTEX_CONSISTENT] = {DUAL_ENTRY(_Tthr_mutex_consistent)}, 269 [PJT_MUTEXATTR_GETROBUST] = {DUAL_ENTRY(_thr_mutexattr_getrobust)}, 270 [PJT_MUTEXATTR_SETROBUST] = {DUAL_ENTRY(_thr_mutexattr_setrobust)}, 271 [PJT_GETTHREADID_NP] = {DUAL_ENTRY(_thr_getthreadid_np)}, 272 [PJT_ATTR_GET_NP] = {DUAL_ENTRY(_thr_attr_get_np)}, 273 [PJT_GETNAME_NP] = {DUAL_ENTRY(_thr_getname_np)}, 274 [PJT_SUSPEND_ALL_NP] = {DUAL_ENTRY(_thr_suspend_all_np)}, 275 [PJT_RESUME_ALL_NP] = {DUAL_ENTRY(_thr_resume_all_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 /* Force _get_curthread() return NULL until set. */ 338 _tcb_get()->tcb_thread = NULL; 339 /* Create and initialize the initial thread. */ 340 curthread = _thr_alloc(NULL); 341 if (curthread == NULL) 342 PANIC("Can't allocate initial thread"); 343 init_main_thread(curthread); 344 } else { 345 first = 0; 346 } 347 348 /* 349 * Add the thread to the thread list queue. 350 */ 351 THR_LIST_ADD(curthread); 352 _thread_active_threads = 1; 353 354 /* Setup the thread specific data */ 355 _tcb_set(curthread->tcb); 356 357 if (first) { 358 _thr_initial = curthread; 359 dlopened = _rtld_is_dlopened(&_thread_autoinit_dummy_decl) != 0; 360 _thr_signal_init(dlopened); 361 if (_thread_event_mask & TD_CREATE) 362 _thr_report_creation(curthread, curthread); 363 /* 364 * Always use our rtld lock implementation. 365 * It is faster because it postpones signal handlers 366 * instead of calling sigprocmask(2). 367 */ 368 _thr_rtld_init(); 369 } 370 } 371 372 /* 373 * This function and pthread_create() do a lot of the same things. 374 * It'd be nice to consolidate the common stuff in one place. 375 */ 376 static void 377 init_main_thread(struct pthread *thread) 378 { 379 struct sched_param sched_param; 380 int i; 381 382 /* Setup the thread attributes. */ 383 thr_self(&thread->tid); 384 thread->attr = _pthread_attr_default; 385 /* 386 * Set up the thread stack. 387 * 388 * Create a red zone below the main stack. All other stacks 389 * are constrained to a maximum size by the parameters 390 * passed to mmap(), but this stack is only limited by 391 * resource limits, so this stack needs an explicitly mapped 392 * red zone to protect the thread stack that is just beyond. 393 */ 394 if (mmap(_usrstack - _thr_stack_initial - 395 _thr_guard_default, _thr_guard_default, 0, MAP_ANON, 396 -1, 0) == MAP_FAILED) 397 PANIC("Cannot allocate red zone for initial thread"); 398 399 /* 400 * Mark the stack as an application supplied stack so that it 401 * isn't deallocated. 402 * 403 * XXX - I'm not sure it would hurt anything to deallocate 404 * the main thread stack because deallocation doesn't 405 * actually free() it; it just puts it in the free 406 * stack queue for later reuse. 407 */ 408 thread->attr.stackaddr_attr = _usrstack - _thr_stack_initial; 409 thread->attr.stacksize_attr = _thr_stack_initial; 410 thread->attr.guardsize_attr = _thr_guard_default; 411 thread->attr.flags |= THR_STACK_USER; 412 413 /* 414 * Write a magic value to the thread structure 415 * to help identify valid ones: 416 */ 417 thread->magic = THR_MAGIC; 418 419 thread->cancel_enable = 1; 420 thread->cancel_async = 0; 421 422 /* Initialize the mutex queues */ 423 for (i = 0; i < TMQ_NITEMS; i++) 424 TAILQ_INIT(&thread->mq[i]); 425 426 thread->state = PS_RUNNING; 427 428 _thr_getscheduler(thread->tid, &thread->attr.sched_policy, 429 &sched_param); 430 thread->attr.prio = sched_param.sched_priority; 431 432 #ifdef _PTHREAD_FORCED_UNWIND 433 thread->unwind_stackend = _usrstack; 434 #endif 435 436 /* Others cleared to zero by thr_alloc() */ 437 } 438 439 bool 440 __thr_get_main_stack_base(char **base) 441 { 442 size_t len; 443 int mib[2]; 444 445 if (elf_aux_info(AT_USRSTACKBASE, base, sizeof(*base)) == 0) 446 return (true); 447 448 mib[0] = CTL_KERN; 449 mib[1] = KERN_USRSTACK; 450 len = sizeof(*base); 451 if (sysctl(mib, nitems(mib), base, &len, NULL, 0) == 0) 452 return (true); 453 454 return (false); 455 } 456 457 bool 458 __thr_get_main_stack_lim(size_t *lim) 459 { 460 struct rlimit rlim; 461 462 if (elf_aux_info(AT_USRSTACKLIM, lim, sizeof(*lim)) == 0) 463 return (true); 464 465 if (getrlimit(RLIMIT_STACK, &rlim) == 0) { 466 *lim = rlim.rlim_cur; 467 return (true); 468 } 469 470 return (false); 471 } 472 473 static void 474 init_private(void) 475 { 476 char *env, *env_bigstack, *env_splitstack; 477 478 _thr_umutex_init(&_mutex_static_lock); 479 _thr_umutex_init(&_cond_static_lock); 480 _thr_umutex_init(&_rwlock_static_lock); 481 _thr_umutex_init(&_keytable_lock); 482 _thr_urwlock_init(&_thr_atfork_lock); 483 _thr_umutex_init(&_thr_event_lock); 484 _thr_umutex_init(&_suspend_all_lock); 485 _thr_spinlock_init(); 486 _thr_list_init(); 487 _thr_wake_addr_init(); 488 _sleepq_init(); 489 _single_thread = NULL; 490 _suspend_all_waiters = 0; 491 492 /* 493 * Avoid reinitializing some things if they don't need to be, 494 * e.g. after a fork(). 495 */ 496 if (init_once == 0) { 497 __thr_pshared_init(); 498 __thr_malloc_init(); 499 500 /* Find the stack top */ 501 if (!__thr_get_main_stack_base(&_usrstack)) 502 PANIC("Cannot get kern.usrstack"); 503 env_bigstack = getenv("LIBPTHREAD_BIGSTACK_MAIN"); 504 env_splitstack = getenv("LIBPTHREAD_SPLITSTACK_MAIN"); 505 if (env_bigstack != NULL || env_splitstack == NULL) { 506 if (!__thr_get_main_stack_lim(&_thr_stack_initial)) 507 PANIC("Cannot get stack rlimit"); 508 } 509 _thr_is_smp = sysconf(_SC_NPROCESSORS_CONF); 510 if (_thr_is_smp == -1) 511 PANIC("Cannot get _SC_NPROCESSORS_CONF"); 512 _thr_is_smp = (_thr_is_smp > 1); 513 _thr_page_size = getpagesize(); 514 _thr_guard_default = _thr_page_size; 515 _pthread_attr_default.guardsize_attr = _thr_guard_default; 516 _pthread_attr_default.stacksize_attr = _thr_stack_default; 517 env = getenv("LIBPTHREAD_SPINLOOPS"); 518 if (env) 519 _thr_spinloops = atoi(env); 520 env = getenv("LIBPTHREAD_YIELDLOOPS"); 521 if (env) 522 _thr_yieldloops = atoi(env); 523 env = getenv("LIBPTHREAD_QUEUE_FIFO"); 524 if (env) 525 _thr_queuefifo = atoi(env); 526 env = getenv("LIBPTHREAD_UMTX_MIN_TIMEOUT"); 527 if (env) { 528 char *endptr; 529 long mint; 530 531 mint = strtol(env, &endptr, 0); 532 if (*endptr == '\0' && mint >= 0) { 533 _umtx_op(NULL, UMTX_OP_SET_MIN_TIMEOUT, 534 mint, NULL, NULL); 535 } 536 } 537 } 538 init_once = 1; 539 } 540