xref: /netbsd-src/lib/libpthread/pthread.c (revision bcfb2645e279a9a462d3964404fd6120c1639d06)
1 /*	$NetBSD: pthread.c,v 1.166 2020/02/16 17:14:31 kamil Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008, 2020
5  *     The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Nathan J. Williams and Andrew Doran.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: pthread.c,v 1.166 2020/02/16 17:14:31 kamil Exp $");
35 
36 #define	__EXPOSE_STACK	1
37 
38 #include <sys/param.h>
39 #include <sys/exec_elf.h>
40 #include <sys/mman.h>
41 #include <sys/lwp.h>
42 #include <sys/lwpctl.h>
43 #include <sys/resource.h>
44 #include <sys/sysctl.h>
45 #include <sys/tls.h>
46 #include <uvm/uvm_param.h>
47 
48 #include <assert.h>
49 #include <dlfcn.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <lwp.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <stddef.h>
57 #include <string.h>
58 #include <syslog.h>
59 #include <ucontext.h>
60 #include <unistd.h>
61 #include <sched.h>
62 
63 #include "atexit.h"
64 #include "pthread.h"
65 #include "pthread_int.h"
66 #include "pthread_makelwp.h"
67 #include "reentrant.h"
68 
69 pthread_rwlock_t pthread__alltree_lock = PTHREAD_RWLOCK_INITIALIZER;
70 static rb_tree_t	pthread__alltree;
71 
72 static signed int	pthread__cmp(void *, const void *, const void *);
73 
74 static const rb_tree_ops_t pthread__alltree_ops = {
75 	.rbto_compare_nodes = pthread__cmp,
76 	.rbto_compare_key = pthread__cmp,
77 	.rbto_node_offset = offsetof(struct __pthread_st, pt_alltree),
78 	.rbto_context = NULL
79 };
80 
81 static void	pthread__create_tramp(void *);
82 static void	pthread__initthread(pthread_t);
83 static void	pthread__scrubthread(pthread_t, char *, int);
84 static void	pthread__initmain(pthread_t *);
85 static void	pthread__fork_callback(void);
86 static void	pthread__reap(pthread_t);
87 static void	pthread__child_callback(void);
88 static void	pthread__start(void);
89 
90 void	pthread__init(void);
91 
92 int pthread__started;
93 int __uselibcstub = 1;
94 pthread_mutex_t pthread__deadqueue_lock = PTHREAD_MUTEX_INITIALIZER;
95 pthread_queue_t pthread__deadqueue;
96 pthread_queue_t pthread__allqueue;
97 
98 static pthread_attr_t pthread_default_attr;
99 static lwpctl_t pthread__dummy_lwpctl = { .lc_curcpu = LWPCTL_CPU_NONE };
100 
101 enum {
102 	DIAGASSERT_ABORT =	1<<0,
103 	DIAGASSERT_STDERR =	1<<1,
104 	DIAGASSERT_SYSLOG =	1<<2
105 };
106 
107 static int pthread__diagassert;
108 
109 int pthread__concurrency;
110 int pthread__nspins;
111 int pthread__unpark_max = PTHREAD__UNPARK_MAX;
112 int pthread__dbg;	/* set by libpthread_dbg if active */
113 
114 /*
115  * We have to initialize the pthread_stack* variables here because
116  * mutexes are used before pthread_init() and thus pthread__initmain()
117  * are called.  Since mutexes only save the stack pointer and not a
118  * pointer to the thread data, it is safe to change the mapping from
119  * stack pointer to thread data afterwards.
120  */
121 size_t	pthread__stacksize;
122 size_t	pthread__guardsize;
123 size_t	pthread__pagesize;
124 static struct __pthread_st *pthread__main;
125 static size_t __pthread_st_size;
126 
127 int _sys___sigprocmask14(int, const sigset_t *, sigset_t *);
128 
129 __strong_alias(__libc_thr_self,pthread_self)
130 __strong_alias(__libc_thr_create,pthread_create)
131 __strong_alias(__libc_thr_exit,pthread_exit)
132 __strong_alias(__libc_thr_errno,pthread__errno)
133 __strong_alias(__libc_thr_setcancelstate,pthread_setcancelstate)
134 __strong_alias(__libc_thr_equal,pthread_equal)
135 __strong_alias(__libc_thr_init,pthread__init)
136 
137 /*
138  * Static library kludge.  Place a reference to a symbol any library
139  * file which does not already have a reference here.
140  */
141 extern int pthread__cancel_stub_binder;
142 
143 void *pthread__static_lib_binder[] = {
144 	&pthread__cancel_stub_binder,
145 	pthread_cond_init,
146 	pthread_mutex_init,
147 	pthread_rwlock_init,
148 	pthread_barrier_init,
149 	pthread_key_create,
150 	pthread_setspecific,
151 };
152 
153 #define	NHASHLOCK	64
154 
155 static union hashlock {
156 	pthread_mutex_t	mutex;
157 	char		pad[64];
158 } hashlocks[NHASHLOCK] __aligned(64);
159 
160 /*
161  * This needs to be started by the library loading code, before main()
162  * gets to run, for various things that use the state of the initial thread
163  * to work properly (thread-specific data is an application-visible example;
164  * spinlock counts for mutexes is an internal example).
165  */
166 void
167 pthread__init(void)
168 {
169 	pthread_t first;
170 	char *p;
171 	int i;
172 	int mib[2];
173 	unsigned int value;
174 	size_t len;
175 	extern int __isthreaded;
176 
177 	/*
178 	 * Allocate pthread_keys descriptors before
179 	 * reseting __uselibcstub because otherwise
180 	 * malloc() will call pthread_keys_create()
181 	 * while pthread_keys descriptors are not
182 	 * yet allocated.
183 	 */
184 	pthread__main = pthread_tsd_earlyinit(&__pthread_st_size);
185 	if (pthread__main == NULL)
186 		err(EXIT_FAILURE, "Cannot allocate pthread storage");
187 
188 	__uselibcstub = 0;
189 
190 	pthread__pagesize = (size_t)sysconf(_SC_PAGESIZE);
191 	pthread__concurrency = (int)sysconf(_SC_NPROCESSORS_CONF);
192 
193 	mib[0] = CTL_VM;
194 	mib[1] = VM_THREAD_GUARD_SIZE;
195 	len = sizeof(value);
196 	if (sysctl(mib, __arraycount(mib), &value, &len, NULL, 0) == 0)
197 		pthread__guardsize = value;
198 	else
199 		pthread__guardsize = pthread__pagesize;
200 
201 	/* Initialize locks first; they're needed elsewhere. */
202 	pthread__lockprim_init();
203 	for (i = 0; i < NHASHLOCK; i++) {
204 		pthread_mutex_init(&hashlocks[i].mutex, NULL);
205 	}
206 
207 	/* Fetch parameters. */
208 	i = (int)_lwp_unpark_all(NULL, 0, NULL);
209 	if (i == -1)
210 		err(EXIT_FAILURE, "_lwp_unpark_all");
211 	if (i < pthread__unpark_max)
212 		pthread__unpark_max = i;
213 
214 	/* Basic data structure setup */
215 	pthread_attr_init(&pthread_default_attr);
216 	PTQ_INIT(&pthread__allqueue);
217 	PTQ_INIT(&pthread__deadqueue);
218 
219 	rb_tree_init(&pthread__alltree, &pthread__alltree_ops);
220 
221 	/* Create the thread structure corresponding to main() */
222 	pthread__initmain(&first);
223 	pthread__initthread(first);
224 	pthread__scrubthread(first, NULL, 0);
225 
226 	first->pt_lid = _lwp_self();
227 	PTQ_INSERT_HEAD(&pthread__allqueue, first, pt_allq);
228 	(void)rb_tree_insert_node(&pthread__alltree, first);
229 
230 	if (_lwp_ctl(LWPCTL_FEATURE_CURCPU, &first->pt_lwpctl) != 0) {
231 		err(EXIT_FAILURE, "_lwp_ctl");
232 	}
233 
234 	/* Start subsystems */
235 	PTHREAD_MD_INIT
236 
237 	for (p = pthread__getenv("PTHREAD_DIAGASSERT"); p && *p; p++) {
238 		switch (*p) {
239 		case 'a':
240 			pthread__diagassert |= DIAGASSERT_ABORT;
241 			break;
242 		case 'A':
243 			pthread__diagassert &= ~DIAGASSERT_ABORT;
244 			break;
245 		case 'e':
246 			pthread__diagassert |= DIAGASSERT_STDERR;
247 			break;
248 		case 'E':
249 			pthread__diagassert &= ~DIAGASSERT_STDERR;
250 			break;
251 		case 'l':
252 			pthread__diagassert |= DIAGASSERT_SYSLOG;
253 			break;
254 		case 'L':
255 			pthread__diagassert &= ~DIAGASSERT_SYSLOG;
256 			break;
257 		}
258 	}
259 
260 	/*
261 	 * We are officially threded now.
262 	 *
263 	 * libc must be informed about this before bootstrapping malloc(3).
264 	 */
265 	__isthreaded = 1;
266 
267 	/*
268 	 * Tell libc that we're here and it should role-play accordingly.
269 	 *
270 	 * pthread_atfork(3) calls malloc(3) and initializes the system malloc.
271 	 * At this point all POSIX thread inferfaces must be functional.
272 	 */
273 	pthread_atfork(NULL, NULL, pthread__fork_callback);
274 
275 	/* Register atfork handlers for TSD. */
276 	pthread_tsd_init();
277 }
278 
279 static void
280 pthread__fork_callback(void)
281 {
282 	struct __pthread_st *self = pthread__self();
283 
284 	/* lwpctl state is not copied across fork. */
285 	if (_lwp_ctl(LWPCTL_FEATURE_CURCPU, &self->pt_lwpctl)) {
286 		err(EXIT_FAILURE, "_lwp_ctl");
287 	}
288 	self->pt_lid = _lwp_self();
289 }
290 
291 static void
292 pthread__child_callback(void)
293 {
294 
295 	/*
296 	 * Clean up data structures that a forked child process might
297 	 * trip over. Note that if threads have been created (causing
298 	 * this handler to be registered) the standards say that the
299 	 * child will trigger undefined behavior if it makes any
300 	 * pthread_* calls (or any other calls that aren't
301 	 * async-signal-safe), so we don't really have to clean up
302 	 * much. Anything that permits some pthread_* calls to work is
303 	 * merely being polite.
304 	 */
305 	pthread__started = 0;
306 }
307 
308 static void
309 pthread__start(void)
310 {
311 
312 	/*
313 	 * Per-process timers are cleared by fork(); despite the
314 	 * various restrictions on fork() and threads, it's legal to
315 	 * fork() before creating any threads.
316 	 */
317 	pthread_atfork(NULL, NULL, pthread__child_callback);
318 }
319 
320 
321 /* General-purpose thread data structure sanitization. */
322 /* ARGSUSED */
323 static void
324 pthread__initthread(pthread_t t)
325 {
326 
327 	t->pt_self = t;
328 	t->pt_magic = PT_MAGIC;
329 	t->pt_willpark = 0;
330 	t->pt_unpark = 0;
331 	t->pt_nwaiters = 0;
332 	t->pt_sleepobj = NULL;
333 	t->pt_signalled = 0;
334 	t->pt_havespecific = 0;
335 	t->pt_early = NULL;
336 	t->pt_lwpctl = &pthread__dummy_lwpctl;
337 
338 	memcpy(&t->pt_lockops, pthread__lock_ops, sizeof(t->pt_lockops));
339 	pthread_mutex_init(&t->pt_lock, NULL);
340 	PTQ_INIT(&t->pt_cleanup_stack);
341 }
342 
343 static void
344 pthread__scrubthread(pthread_t t, char *name, int flags)
345 {
346 
347 	t->pt_state = PT_STATE_RUNNING;
348 	t->pt_exitval = NULL;
349 	t->pt_flags = flags;
350 	t->pt_cancel = 0;
351 	t->pt_errno = 0;
352 	t->pt_name = name;
353 	t->pt_lid = 0;
354 }
355 
356 static int
357 pthread__getstack(pthread_t newthread, const pthread_attr_t *attr)
358 {
359 	void *stackbase, *stackbase2, *redzone;
360 	size_t stacksize, guardsize;
361 	bool allocated;
362 
363 	if (attr != NULL) {
364 		pthread_attr_getstack(attr, &stackbase, &stacksize);
365 		pthread_attr_getguardsize(attr, &guardsize);
366 	} else {
367 		stackbase = NULL;
368 		stacksize = 0;
369 		guardsize = pthread__guardsize;
370 	}
371 	if (stacksize == 0)
372 		stacksize = pthread__stacksize;
373 
374 	if (newthread->pt_stack_allocated) {
375 		if (stackbase == NULL &&
376 		    newthread->pt_stack.ss_size == stacksize &&
377 		    newthread->pt_guardsize == guardsize)
378 			return 0;
379 		stackbase2 = newthread->pt_stack.ss_sp;
380 #ifndef __MACHINE_STACK_GROWS_UP
381 		stackbase2 = (char *)stackbase2 - newthread->pt_guardsize;
382 #endif
383 		munmap(stackbase2,
384 		    newthread->pt_stack.ss_size + newthread->pt_guardsize);
385 		newthread->pt_stack.ss_sp = NULL;
386 		newthread->pt_stack.ss_size = 0;
387 		newthread->pt_guardsize = 0;
388 		newthread->pt_stack_allocated = false;
389 	}
390 
391 	newthread->pt_stack_allocated = false;
392 
393 	if (stackbase == NULL) {
394 		stacksize = ((stacksize - 1) | (pthread__pagesize - 1)) + 1;
395 		guardsize = ((guardsize - 1) | (pthread__pagesize - 1)) + 1;
396 		stackbase = mmap(NULL, stacksize + guardsize,
397 		    PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
398 		if (stackbase == MAP_FAILED)
399 			return ENOMEM;
400 		allocated = true;
401 	} else {
402 		allocated = false;
403 	}
404 #ifdef __MACHINE_STACK_GROWS_UP
405 	redzone = (char *)stackbase + stacksize;
406 	stackbase2 = (char *)stackbase;
407 #else
408 	redzone = (char *)stackbase;
409 	stackbase2 = (char *)stackbase + guardsize;
410 #endif
411 	if (allocated && guardsize &&
412 	    mprotect(redzone, guardsize, PROT_NONE) == -1) {
413 		munmap(stackbase, stacksize + guardsize);
414 		return EPERM;
415 	}
416 	newthread->pt_stack.ss_size = stacksize;
417 	newthread->pt_stack.ss_sp = stackbase2;
418 	newthread->pt_guardsize = guardsize;
419 	newthread->pt_stack_allocated = allocated;
420 	return 0;
421 }
422 
423 int
424 pthread_create(pthread_t *thread, const pthread_attr_t *attr,
425 	    void *(*startfunc)(void *), void *arg)
426 {
427 	pthread_t newthread;
428 	pthread_attr_t nattr;
429 	struct pthread_attr_private *p;
430 	char * volatile name;
431 	unsigned long flag;
432 	void *private_area;
433 	int ret;
434 
435 	if (__predict_false(__uselibcstub)) {
436     		pthread__errorfunc(__FILE__, __LINE__, __func__,
437 		    "pthread_create() requires linking with -lpthread");
438 		return __libc_thr_create_stub(thread, attr, startfunc, arg);
439 	}
440 
441 	/*
442 	 * It's okay to check this without a lock because there can
443 	 * only be one thread before it becomes true.
444 	 */
445 	if (pthread__started == 0) {
446 		pthread__start();
447 		pthread__started = 1;
448 	}
449 
450 	if (attr == NULL)
451 		nattr = pthread_default_attr;
452 	else if (attr->pta_magic == PT_ATTR_MAGIC)
453 		nattr = *attr;
454 	else
455 		return EINVAL;
456 
457 	/* Fetch misc. attributes from the attr structure. */
458 	name = NULL;
459 	if ((p = nattr.pta_private) != NULL)
460 		if (p->ptap_name[0] != '\0')
461 			if ((name = strdup(p->ptap_name)) == NULL)
462 				return ENOMEM;
463 
464 	newthread = NULL;
465 
466 	/*
467 	 * Try to reclaim a dead thread.
468 	 */
469 	if (!PTQ_EMPTY(&pthread__deadqueue)) {
470 		pthread_mutex_lock(&pthread__deadqueue_lock);
471 		PTQ_FOREACH(newthread, &pthread__deadqueue, pt_deadq) {
472 			/* Still busily exiting, or finished? */
473 			if (newthread->pt_lwpctl->lc_curcpu ==
474 			    LWPCTL_CPU_EXITED)
475 				break;
476 		}
477 		if (newthread)
478 			PTQ_REMOVE(&pthread__deadqueue, newthread, pt_deadq);
479 		pthread_mutex_unlock(&pthread__deadqueue_lock);
480 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
481 		if (newthread && newthread->pt_tls) {
482 			_rtld_tls_free(newthread->pt_tls);
483 			newthread->pt_tls = NULL;
484 		}
485 #endif
486 	}
487 
488 	/*
489 	 * If necessary set up a stack, allocate space for a pthread_st,
490 	 * and initialize it.
491 	 */
492 	if (newthread == NULL) {
493 		newthread = calloc(1, __pthread_st_size);
494 		if (newthread == NULL) {
495 			free(name);
496 			return ENOMEM;
497 		}
498 		newthread->pt_stack_allocated = false;
499 
500 		if (pthread__getstack(newthread, attr)) {
501 			free(newthread);
502 			free(name);
503 			return ENOMEM;
504 		}
505 
506 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
507 		newthread->pt_tls = NULL;
508 #endif
509 
510 		/* Add to list of all threads. */
511 		pthread_rwlock_wrlock(&pthread__alltree_lock);
512 		PTQ_INSERT_TAIL(&pthread__allqueue, newthread, pt_allq);
513 		(void)rb_tree_insert_node(&pthread__alltree, newthread);
514 		pthread_rwlock_unlock(&pthread__alltree_lock);
515 
516 		/* Will be reset by the thread upon exit. */
517 		pthread__initthread(newthread);
518 	} else {
519 		if (pthread__getstack(newthread, attr)) {
520 			pthread_mutex_lock(&pthread__deadqueue_lock);
521 			PTQ_INSERT_TAIL(&pthread__deadqueue, newthread, pt_deadq);
522 			pthread_mutex_unlock(&pthread__deadqueue_lock);
523 			return ENOMEM;
524 		}
525 	}
526 
527 	/*
528 	 * Create the new LWP.
529 	 */
530 	pthread__scrubthread(newthread, name, nattr.pta_flags);
531 	newthread->pt_func = startfunc;
532 	newthread->pt_arg = arg;
533 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
534 	private_area = newthread->pt_tls = _rtld_tls_allocate();
535 	newthread->pt_tls->tcb_pthread = newthread;
536 #else
537 	private_area = newthread;
538 #endif
539 
540 	flag = 0;
541 	if ((newthread->pt_flags & PT_FLAG_SUSPENDED) != 0 ||
542 	    (nattr.pta_flags & PT_FLAG_EXPLICIT_SCHED) != 0)
543 		flag |= LWP_SUSPENDED;
544 	if ((newthread->pt_flags & PT_FLAG_DETACHED) != 0)
545 		flag |= LWP_DETACHED;
546 
547 	ret = pthread__makelwp(pthread__create_tramp, newthread, private_area,
548 	    newthread->pt_stack.ss_sp, newthread->pt_stack.ss_size,
549 	    flag, &newthread->pt_lid);
550 	if (ret != 0) {
551 		ret = errno;
552 		pthread_mutex_lock(&newthread->pt_lock);
553 		/* Will unlock and free name. */
554 		pthread__reap(newthread);
555 		return ret;
556 	}
557 
558 	if ((nattr.pta_flags & PT_FLAG_EXPLICIT_SCHED) != 0) {
559 		if (p != NULL) {
560 			(void)pthread_setschedparam(newthread, p->ptap_policy,
561 			    &p->ptap_sp);
562 		}
563 		if ((newthread->pt_flags & PT_FLAG_SUSPENDED) == 0) {
564 			(void)_lwp_continue(newthread->pt_lid);
565 		}
566 	}
567 
568 	*thread = newthread;
569 
570 	return 0;
571 }
572 
573 
574 __dead static void
575 pthread__create_tramp(void *cookie)
576 {
577 	pthread_t self;
578 	void *retval;
579 
580 	self = cookie;
581 
582 	/*
583 	 * Throw away some stack in a feeble attempt to reduce cache
584 	 * thrash.  May help for SMT processors.  XXX We should not
585 	 * be allocating stacks on fixed 2MB boundaries.  Needs a
586 	 * thread register or decent thread local storage.
587 	 */
588 	(void)alloca(((unsigned)self->pt_lid & 7) << 8);
589 
590 	if (self->pt_name != NULL) {
591 		pthread_mutex_lock(&self->pt_lock);
592 		if (self->pt_name != NULL)
593 			(void)_lwp_setname(0, self->pt_name);
594 		pthread_mutex_unlock(&self->pt_lock);
595 	}
596 
597 	if (_lwp_ctl(LWPCTL_FEATURE_CURCPU, &self->pt_lwpctl)) {
598 		err(EXIT_FAILURE, "_lwp_ctl");
599 	}
600 
601 	retval = (*self->pt_func)(self->pt_arg);
602 
603 	pthread_exit(retval);
604 
605 	/*NOTREACHED*/
606 	pthread__abort();
607 }
608 
609 int
610 pthread_suspend_np(pthread_t thread)
611 {
612 	pthread_t self;
613 
614 	pthread__error(EINVAL, "Invalid thread",
615 	    thread->pt_magic == PT_MAGIC);
616 
617 	self = pthread__self();
618 	if (self == thread) {
619 		return EDEADLK;
620 	}
621 	if (pthread__find(thread) != 0)
622 		return ESRCH;
623 	if (_lwp_suspend(thread->pt_lid) == 0)
624 		return 0;
625 	return errno;
626 }
627 
628 int
629 pthread_resume_np(pthread_t thread)
630 {
631 
632 	pthread__error(EINVAL, "Invalid thread",
633 	    thread->pt_magic == PT_MAGIC);
634 
635 	if (pthread__find(thread) != 0)
636 		return ESRCH;
637 	if (_lwp_continue(thread->pt_lid) == 0)
638 		return 0;
639 	return errno;
640 }
641 
642 /*
643  * In case the thread is exiting at an inopportune time leaving waiters not
644  * awoken (because cancelled, for instance) make sure we have no waiters
645  * left.
646  */
647 static void
648 pthread__clear_waiters(pthread_t self)
649 {
650 
651 	if (self->pt_nwaiters != 0) {
652 		(void)_lwp_unpark_all(self->pt_waiters, self->pt_nwaiters,
653 		    NULL);
654 		self->pt_nwaiters = 0;
655 	}
656 	self->pt_willpark = 0;
657 }
658 
659 void
660 pthread_exit(void *retval)
661 {
662 	pthread_t self;
663 	struct pt_clean_t *cleanup;
664 
665 	if (__predict_false(__uselibcstub)) {
666 		__libc_thr_exit_stub(retval);
667 		goto out;
668 	}
669 
670 	self = pthread__self();
671 
672 	/* Disable cancellability. */
673 	pthread_mutex_lock(&self->pt_lock);
674 	self->pt_flags |= PT_FLAG_CS_DISABLED;
675 	self->pt_cancel = 0;
676 
677 	/* Call any cancellation cleanup handlers */
678 	if (!PTQ_EMPTY(&self->pt_cleanup_stack)) {
679 		pthread_mutex_unlock(&self->pt_lock);
680 		while (!PTQ_EMPTY(&self->pt_cleanup_stack)) {
681 			cleanup = PTQ_FIRST(&self->pt_cleanup_stack);
682 			PTQ_REMOVE(&self->pt_cleanup_stack, cleanup, ptc_next);
683 			(*cleanup->ptc_cleanup)(cleanup->ptc_arg);
684 		}
685 		pthread_mutex_lock(&self->pt_lock);
686 	}
687 
688 	pthread_mutex_unlock(&self->pt_lock);
689 	__cxa_thread_run_atexit();
690 	pthread_mutex_lock(&self->pt_lock);
691 
692 	/* Perform cleanup of thread-specific data */
693 	pthread__destroy_tsd(self);
694 
695 	/*
696 	 * Signal our exit.  Our stack and pthread_t won't be reused until
697 	 * pthread_create() can see from kernel info that this LWP is gone.
698 	 */
699 	self->pt_exitval = retval;
700 	if (self->pt_flags & PT_FLAG_DETACHED) {
701 		/* pthread__reap() will drop the lock. */
702 		pthread__reap(self);
703 		pthread__clear_waiters(self);
704 		_lwp_exit();
705 	} else {
706 		self->pt_state = PT_STATE_ZOMBIE;
707 		pthread_mutex_unlock(&self->pt_lock);
708 		pthread__clear_waiters(self);
709 		/* Note: name will be freed by the joiner. */
710 		_lwp_exit();
711 	}
712 
713 out:
714 	/*NOTREACHED*/
715 	pthread__abort();
716 	exit(1);
717 }
718 
719 
720 int
721 pthread_join(pthread_t thread, void **valptr)
722 {
723 	pthread_t self;
724 
725 	pthread__error(EINVAL, "Invalid thread",
726 	    thread->pt_magic == PT_MAGIC);
727 
728 	self = pthread__self();
729 
730 	if (pthread__find(thread) != 0)
731 		return ESRCH;
732 
733 	if (thread == self)
734 		return EDEADLK;
735 
736 	/* IEEE Std 1003.1 says pthread_join() never returns EINTR. */
737 	for (;;) {
738 		pthread__testcancel(self);
739 		if (_lwp_wait(thread->pt_lid, NULL) == 0)
740 			break;
741 		if (errno != EINTR)
742 			return errno;
743 	}
744 
745 	/*
746 	 * Don't test for cancellation again.  The spec is that if
747 	 * cancelled, pthread_join() must not have succeeded.
748 	 */
749 	pthread_mutex_lock(&thread->pt_lock);
750 	if (thread->pt_state != PT_STATE_ZOMBIE) {
751 		pthread__errorfunc(__FILE__, __LINE__, __func__,
752 		    "not a zombie");
753  	}
754 	if (valptr != NULL)
755 		*valptr = thread->pt_exitval;
756 
757 	/* pthread__reap() will drop the lock. */
758 	pthread__reap(thread);
759 	return 0;
760 }
761 
762 static void
763 pthread__reap(pthread_t thread)
764 {
765 	char *name;
766 
767 	name = thread->pt_name;
768 	thread->pt_name = NULL;
769 	thread->pt_state = PT_STATE_DEAD;
770 	pthread_mutex_unlock(&thread->pt_lock);
771 
772 	pthread_mutex_lock(&pthread__deadqueue_lock);
773 	PTQ_INSERT_HEAD(&pthread__deadqueue, thread, pt_deadq);
774 	pthread_mutex_unlock(&pthread__deadqueue_lock);
775 
776 	if (name != NULL)
777 		free(name);
778 }
779 
780 int
781 pthread_equal(pthread_t t1, pthread_t t2)
782 {
783 
784 	if (__predict_false(__uselibcstub))
785 		return __libc_thr_equal_stub(t1, t2);
786 
787 	pthread__error(0, "Invalid thread",
788 	    (t1 != NULL) && (t1->pt_magic == PT_MAGIC));
789 
790 	pthread__error(0, "Invalid thread",
791 	    (t2 != NULL) && (t2->pt_magic == PT_MAGIC));
792 
793 	/* Nothing special here. */
794 	return (t1 == t2);
795 }
796 
797 
798 int
799 pthread_detach(pthread_t thread)
800 {
801 	int error;
802 
803 	pthread__error(EINVAL, "Invalid thread",
804 	    thread->pt_magic == PT_MAGIC);
805 
806 	if (pthread__find(thread) != 0)
807 		return ESRCH;
808 
809 	pthread_mutex_lock(&thread->pt_lock);
810 	if ((thread->pt_flags & PT_FLAG_DETACHED) != 0) {
811 		error = EINVAL;
812 	} else {
813 		error = _lwp_detach(thread->pt_lid);
814 		if (error == 0)
815 			thread->pt_flags |= PT_FLAG_DETACHED;
816 		else
817 			error = errno;
818 	}
819 	if (thread->pt_state == PT_STATE_ZOMBIE) {
820 		/* pthread__reap() will drop the lock. */
821 		pthread__reap(thread);
822 	} else
823 		pthread_mutex_unlock(&thread->pt_lock);
824 	return error;
825 }
826 
827 
828 int
829 pthread_getname_np(pthread_t thread, char *name, size_t len)
830 {
831 
832 	pthread__error(EINVAL, "Invalid thread",
833 	    thread->pt_magic == PT_MAGIC);
834 
835 	if (pthread__find(thread) != 0)
836 		return ESRCH;
837 
838 	pthread_mutex_lock(&thread->pt_lock);
839 	if (thread->pt_name == NULL)
840 		name[0] = '\0';
841 	else
842 		strlcpy(name, thread->pt_name, len);
843 	pthread_mutex_unlock(&thread->pt_lock);
844 
845 	return 0;
846 }
847 
848 
849 int
850 pthread_setname_np(pthread_t thread, const char *name, void *arg)
851 {
852 	char *oldname, *cp, newname[PTHREAD_MAX_NAMELEN_NP];
853 	int namelen;
854 
855 	pthread__error(EINVAL, "Invalid thread",
856 	    thread->pt_magic == PT_MAGIC);
857 
858 	if (pthread__find(thread) != 0)
859 		return ESRCH;
860 
861 	namelen = snprintf(newname, sizeof(newname), name, arg);
862 	if (namelen >= PTHREAD_MAX_NAMELEN_NP)
863 		return EINVAL;
864 
865 	cp = strdup(newname);
866 	if (cp == NULL)
867 		return ENOMEM;
868 
869 	pthread_mutex_lock(&thread->pt_lock);
870 	oldname = thread->pt_name;
871 	thread->pt_name = cp;
872 	(void)_lwp_setname(thread->pt_lid, cp);
873 	pthread_mutex_unlock(&thread->pt_lock);
874 
875 	if (oldname != NULL)
876 		free(oldname);
877 
878 	return 0;
879 }
880 
881 
882 pthread_t
883 pthread_self(void)
884 {
885 	if (__predict_false(__uselibcstub))
886 		return (pthread_t)__libc_thr_self_stub();
887 
888 	return pthread__self();
889 }
890 
891 
892 int
893 pthread_cancel(pthread_t thread)
894 {
895 
896 	pthread__error(EINVAL, "Invalid thread",
897 	    thread->pt_magic == PT_MAGIC);
898 
899 	if (pthread__find(thread) != 0)
900 		return ESRCH;
901 	pthread_mutex_lock(&thread->pt_lock);
902 	thread->pt_flags |= PT_FLAG_CS_PENDING;
903 	if ((thread->pt_flags & PT_FLAG_CS_DISABLED) == 0) {
904 		thread->pt_cancel = 1;
905 		pthread_mutex_unlock(&thread->pt_lock);
906 		_lwp_wakeup(thread->pt_lid);
907 	} else
908 		pthread_mutex_unlock(&thread->pt_lock);
909 
910 	return 0;
911 }
912 
913 
914 int
915 pthread_setcancelstate(int state, int *oldstate)
916 {
917 	pthread_t self;
918 	int retval;
919 
920 	if (__predict_false(__uselibcstub))
921 		return __libc_thr_setcancelstate_stub(state, oldstate);
922 
923 	self = pthread__self();
924 	retval = 0;
925 
926 	pthread_mutex_lock(&self->pt_lock);
927 
928 	if (oldstate != NULL) {
929 		if (self->pt_flags & PT_FLAG_CS_DISABLED)
930 			*oldstate = PTHREAD_CANCEL_DISABLE;
931 		else
932 			*oldstate = PTHREAD_CANCEL_ENABLE;
933 	}
934 
935 	if (state == PTHREAD_CANCEL_DISABLE) {
936 		self->pt_flags |= PT_FLAG_CS_DISABLED;
937 		if (self->pt_cancel) {
938 			self->pt_flags |= PT_FLAG_CS_PENDING;
939 			self->pt_cancel = 0;
940 		}
941 	} else if (state == PTHREAD_CANCEL_ENABLE) {
942 		self->pt_flags &= ~PT_FLAG_CS_DISABLED;
943 		/*
944 		 * If a cancellation was requested while cancellation
945 		 * was disabled, note that fact for future
946 		 * cancellation tests.
947 		 */
948 		if (self->pt_flags & PT_FLAG_CS_PENDING) {
949 			self->pt_cancel = 1;
950 			/* This is not a deferred cancellation point. */
951 			if (self->pt_flags & PT_FLAG_CS_ASYNC) {
952 				pthread_mutex_unlock(&self->pt_lock);
953 				pthread__cancelled();
954 			}
955 		}
956 	} else
957 		retval = EINVAL;
958 
959 	pthread_mutex_unlock(&self->pt_lock);
960 
961 	return retval;
962 }
963 
964 
965 int
966 pthread_setcanceltype(int type, int *oldtype)
967 {
968 	pthread_t self;
969 	int retval;
970 
971 	self = pthread__self();
972 	retval = 0;
973 
974 	pthread_mutex_lock(&self->pt_lock);
975 
976 	if (oldtype != NULL) {
977 		if (self->pt_flags & PT_FLAG_CS_ASYNC)
978 			*oldtype = PTHREAD_CANCEL_ASYNCHRONOUS;
979 		else
980 			*oldtype = PTHREAD_CANCEL_DEFERRED;
981 	}
982 
983 	if (type == PTHREAD_CANCEL_ASYNCHRONOUS) {
984 		self->pt_flags |= PT_FLAG_CS_ASYNC;
985 		if (self->pt_cancel) {
986 			pthread_mutex_unlock(&self->pt_lock);
987 			pthread__cancelled();
988 		}
989 	} else if (type == PTHREAD_CANCEL_DEFERRED)
990 		self->pt_flags &= ~PT_FLAG_CS_ASYNC;
991 	else
992 		retval = EINVAL;
993 
994 	pthread_mutex_unlock(&self->pt_lock);
995 
996 	return retval;
997 }
998 
999 
1000 void
1001 pthread_testcancel(void)
1002 {
1003 	pthread_t self;
1004 
1005 	self = pthread__self();
1006 	if (self->pt_cancel)
1007 		pthread__cancelled();
1008 }
1009 
1010 
1011 /*
1012  * POSIX requires that certain functions return an error rather than
1013  * invoking undefined behavior even when handed completely bogus
1014  * pthread_t values, e.g. stack garbage.
1015  */
1016 int
1017 pthread__find(pthread_t id)
1018 {
1019 	pthread_t target;
1020 	int error;
1021 
1022 	pthread_rwlock_rdlock(&pthread__alltree_lock);
1023 	target = rb_tree_find_node(&pthread__alltree, id);
1024 	error = (target && target->pt_state != PT_STATE_DEAD) ? 0 : ESRCH;
1025 	pthread_rwlock_unlock(&pthread__alltree_lock);
1026 
1027 	return error;
1028 }
1029 
1030 
1031 void
1032 pthread__testcancel(pthread_t self)
1033 {
1034 
1035 	if (self->pt_cancel)
1036 		pthread__cancelled();
1037 }
1038 
1039 
1040 void
1041 pthread__cancelled(void)
1042 {
1043 
1044 	pthread_exit(PTHREAD_CANCELED);
1045 }
1046 
1047 
1048 void
1049 pthread__cleanup_push(void (*cleanup)(void *), void *arg, void *store)
1050 {
1051 	pthread_t self;
1052 	struct pt_clean_t *entry;
1053 
1054 	self = pthread__self();
1055 	entry = store;
1056 	entry->ptc_cleanup = cleanup;
1057 	entry->ptc_arg = arg;
1058 	PTQ_INSERT_HEAD(&self->pt_cleanup_stack, entry, ptc_next);
1059 }
1060 
1061 
1062 void
1063 pthread__cleanup_pop(int ex, void *store)
1064 {
1065 	pthread_t self;
1066 	struct pt_clean_t *entry;
1067 
1068 	self = pthread__self();
1069 	entry = store;
1070 
1071 	PTQ_REMOVE(&self->pt_cleanup_stack, entry, ptc_next);
1072 	if (ex)
1073 		(*entry->ptc_cleanup)(entry->ptc_arg);
1074 }
1075 
1076 
1077 int *
1078 pthread__errno(void)
1079 {
1080 	pthread_t self;
1081 
1082 	if (__predict_false(__uselibcstub)) {
1083     		pthread__errorfunc(__FILE__, __LINE__, __func__,
1084 		    "pthread__errno() requires linking with -lpthread");
1085 		return __libc_thr_errno_stub();
1086 	}
1087 
1088 	self = pthread__self();
1089 
1090 	return &(self->pt_errno);
1091 }
1092 
1093 ssize_t	_sys_write(int, const void *, size_t);
1094 
1095 void
1096 pthread__assertfunc(const char *file, int line, const char *function,
1097 		    const char *expr)
1098 {
1099 	char buf[1024];
1100 	int len;
1101 
1102 	/*
1103 	 * snprintf should not acquire any locks, or we could
1104 	 * end up deadlocked if the assert caller held locks.
1105 	 */
1106 	len = snprintf(buf, 1024,
1107 	    "assertion \"%s\" failed: file \"%s\", line %d%s%s%s\n",
1108 	    expr, file, line,
1109 	    function ? ", function \"" : "",
1110 	    function ? function : "",
1111 	    function ? "\"" : "");
1112 
1113 	_sys_write(STDERR_FILENO, buf, (size_t)len);
1114 	(void)_lwp_kill(_lwp_self(), SIGABRT);
1115 	_exit(1);
1116 }
1117 
1118 
1119 void
1120 pthread__errorfunc(const char *file, int line, const char *function,
1121 		   const char *msg)
1122 {
1123 	char buf[1024];
1124 	size_t len;
1125 
1126 	if (pthread__diagassert == 0)
1127 		return;
1128 
1129 	/*
1130 	 * snprintf should not acquire any locks, or we could
1131 	 * end up deadlocked if the assert caller held locks.
1132 	 */
1133 	len = snprintf(buf, 1024,
1134 	    "%s: Error detected by libpthread: %s.\n"
1135 	    "Detected by file \"%s\", line %d%s%s%s.\n"
1136 	    "See pthread(3) for information.\n",
1137 	    getprogname(), msg, file, line,
1138 	    function ? ", function \"" : "",
1139 	    function ? function : "",
1140 	    function ? "\"" : "");
1141 
1142 	if (pthread__diagassert & DIAGASSERT_STDERR)
1143 		_sys_write(STDERR_FILENO, buf, len);
1144 
1145 	if (pthread__diagassert & DIAGASSERT_SYSLOG)
1146 		syslog(LOG_DEBUG | LOG_USER, "%s", buf);
1147 
1148 	if (pthread__diagassert & DIAGASSERT_ABORT) {
1149 		(void)_lwp_kill(_lwp_self(), SIGABRT);
1150 		_exit(1);
1151 	}
1152 }
1153 
1154 /*
1155  * Thread park/unpark operations.  The kernel operations are
1156  * modelled after a brief description from "Multithreading in
1157  * the Solaris Operating Environment":
1158  *
1159  * http://www.sun.com/software/whitepapers/solaris9/multithread.pdf
1160  */
1161 
1162 #define	OOPS(msg)			\
1163     pthread__errorfunc(__FILE__, __LINE__, __func__, msg)
1164 
1165 int
1166 pthread__park(pthread_t self, pthread_mutex_t *lock,
1167 	      pthread_queue_t *queue, const struct timespec *abstime,
1168 	      int cancelpt, const void *hint)
1169 {
1170 	int rv, error;
1171 	void *obj;
1172 
1173 	self->pt_willpark = 1;
1174 	pthread_mutex_unlock(lock);
1175 	self->pt_willpark = 0;
1176 
1177 	/*
1178 	 * Wait until we are awoken by a pending unpark operation,
1179 	 * a signal, an unpark posted after we have gone asleep,
1180 	 * or an expired timeout.
1181 	 *
1182 	 * It is fine to test the value of pt_sleepobj without
1183 	 * holding any locks, because:
1184 	 *
1185 	 * o Only the blocking thread (this thread) ever sets them
1186 	 *   to a non-NULL value.
1187 	 *
1188 	 * o Other threads may set them NULL, but if they do so they
1189 	 *   must also make this thread return from _lwp_park.
1190 	 *
1191 	 * o _lwp_park, _lwp_unpark and _lwp_unpark_all are system
1192 	 *   calls and all make use of spinlocks in the kernel.  So
1193 	 *   these system calls act as full memory barriers, and will
1194 	 *   ensure that the calling CPU's store buffers are drained.
1195 	 *   In combination with the spinlock release before unpark,
1196 	 *   this means that modification of pt_sleepobj/onq by another
1197 	 *   thread will become globally visible before that thread
1198 	 *   schedules an unpark operation on this thread.
1199 	 *
1200 	 * Note: the test in the while() statement dodges the park op if
1201 	 * we have already been awoken, unless there is another thread to
1202 	 * awaken.  This saves a syscall - if we were already awakened,
1203 	 * the next call to _lwp_park() would need to return early in order
1204 	 * to eat the previous wakeup.
1205 	 */
1206 	rv = 0;
1207 	do {
1208 		/*
1209 		 * If we deferred unparking a thread, arrange to
1210 		 * have _lwp_park() restart it before blocking.
1211 		 */
1212 		error = _lwp_park(CLOCK_REALTIME, TIMER_ABSTIME,
1213 		    __UNCONST(abstime), self->pt_unpark, hint, hint);
1214 		self->pt_unpark = 0;
1215 		if (error != 0) {
1216 			switch (rv = errno) {
1217 			case EINTR:
1218 			case EALREADY:
1219 				rv = 0;
1220 				break;
1221 			case ETIMEDOUT:
1222 				break;
1223 			default:
1224 				OOPS("_lwp_park failed");
1225 				break;
1226 			}
1227 		}
1228 		/* Check for cancellation. */
1229 		if (cancelpt && self->pt_cancel)
1230 			rv = EINTR;
1231 	} while (self->pt_sleepobj != NULL && rv == 0);
1232 
1233 	/*
1234 	 * If we have been awoken early but are still on the queue,
1235 	 * then remove ourself.  Again, it's safe to do the test
1236 	 * without holding any locks.
1237 	 */
1238 	if (__predict_false(self->pt_sleepobj != NULL)) {
1239 		pthread_mutex_lock(lock);
1240 		if ((obj = self->pt_sleepobj) != NULL) {
1241 			PTQ_REMOVE(queue, self, pt_sleep);
1242 			self->pt_sleepobj = NULL;
1243 			if (obj != NULL && self->pt_early != NULL)
1244 				(*self->pt_early)(obj);
1245 		}
1246 		pthread_mutex_unlock(lock);
1247 	}
1248 	self->pt_early = NULL;
1249 
1250 	return rv;
1251 }
1252 
1253 void
1254 pthread__unpark(pthread_queue_t *queue, pthread_t self,
1255 		pthread_mutex_t *interlock)
1256 {
1257 	pthread_t target;
1258 	u_int max;
1259 	size_t nwaiters;
1260 
1261 	max = pthread__unpark_max;
1262 	nwaiters = self->pt_nwaiters;
1263 	target = PTQ_FIRST(queue);
1264 	if (nwaiters == max) {
1265 		/* Overflow. */
1266 		(void)_lwp_unpark_all(self->pt_waiters, nwaiters,
1267 		    __UNVOLATILE(&interlock->ptm_waiters));
1268 		nwaiters = 0;
1269 	}
1270 	target->pt_sleepobj = NULL;
1271 	self->pt_waiters[nwaiters++] = target->pt_lid;
1272 	PTQ_REMOVE(queue, target, pt_sleep);
1273 	self->pt_nwaiters = nwaiters;
1274 	pthread__mutex_deferwake(self, interlock);
1275 }
1276 
1277 void
1278 pthread__unpark_all(pthread_queue_t *queue, pthread_t self,
1279 		    pthread_mutex_t *interlock)
1280 {
1281 	pthread_t target;
1282 	u_int max;
1283 	size_t nwaiters;
1284 
1285 	max = pthread__unpark_max;
1286 	nwaiters = self->pt_nwaiters;
1287 	PTQ_FOREACH(target, queue, pt_sleep) {
1288 		if (nwaiters == max) {
1289 			/* Overflow. */
1290 			(void)_lwp_unpark_all(self->pt_waiters, nwaiters,
1291 			    __UNVOLATILE(&interlock->ptm_waiters));
1292 			nwaiters = 0;
1293 		}
1294 		target->pt_sleepobj = NULL;
1295 		self->pt_waiters[nwaiters++] = target->pt_lid;
1296 	}
1297 	self->pt_nwaiters = nwaiters;
1298 	PTQ_INIT(queue);
1299 	pthread__mutex_deferwake(self, interlock);
1300 }
1301 
1302 #undef	OOPS
1303 
1304 static void
1305 pthread__initmainstack(void)
1306 {
1307 	struct rlimit slimit;
1308 	const AuxInfo *aux;
1309 	size_t size, len;
1310 	int mib[2];
1311 	unsigned int value;
1312 
1313 	_DIAGASSERT(_dlauxinfo() != NULL);
1314 
1315 	if (getrlimit(RLIMIT_STACK, &slimit) == -1)
1316 		err(EXIT_FAILURE,
1317 		    "Couldn't get stack resource consumption limits");
1318 	size = slimit.rlim_cur;
1319 	pthread__main->pt_stack.ss_size = size;
1320 	pthread__main->pt_guardsize = pthread__pagesize;
1321 
1322 	mib[0] = CTL_VM;
1323 	mib[1] = VM_GUARD_SIZE;
1324 	len = sizeof(value);
1325 	if (sysctl(mib, __arraycount(mib), &value, &len, NULL, 0) == 0)
1326 		pthread__main->pt_guardsize = value;
1327 
1328 	for (aux = _dlauxinfo(); aux->a_type != AT_NULL; ++aux) {
1329 		if (aux->a_type == AT_STACKBASE) {
1330 #ifdef __MACHINE_STACK_GROWS_UP
1331 			pthread__main->pt_stack.ss_sp = (void *)aux->a_v;
1332 #else
1333 			pthread__main->pt_stack.ss_sp = (char *)aux->a_v - size;
1334 #endif
1335 			break;
1336 		}
1337 	}
1338 	pthread__copy_tsd(pthread__main);
1339 }
1340 
1341 /*
1342  * Set up the slightly special stack for the "initial" thread, which
1343  * runs on the normal system stack, and thus gets slightly different
1344  * treatment.
1345  */
1346 static void
1347 pthread__initmain(pthread_t *newt)
1348 {
1349 	char *value;
1350 
1351 	pthread__initmainstack();
1352 
1353 	value = pthread__getenv("PTHREAD_STACKSIZE");
1354 	if (value != NULL) {
1355 		pthread__stacksize = atoi(value) * 1024;
1356 		if (pthread__stacksize > pthread__main->pt_stack.ss_size)
1357 			pthread__stacksize = pthread__main->pt_stack.ss_size;
1358 	}
1359 	if (pthread__stacksize == 0)
1360 		pthread__stacksize = pthread__main->pt_stack.ss_size;
1361 	pthread__stacksize += pthread__pagesize - 1;
1362 	pthread__stacksize &= ~(pthread__pagesize - 1);
1363 	if (pthread__stacksize < 4 * pthread__pagesize)
1364 		errx(1, "Stacksize limit is too low, minimum %zd kbyte.",
1365 		    4 * pthread__pagesize / 1024);
1366 
1367 	*newt = pthread__main;
1368 #if defined(_PTHREAD_GETTCB_EXT)
1369 	pthread__main->pt_tls = _PTHREAD_GETTCB_EXT();
1370 #elif defined(__HAVE___LWP_GETTCB_FAST)
1371 	pthread__main->pt_tls = __lwp_gettcb_fast();
1372 #else
1373 	pthread__main->pt_tls = _lwp_getprivate();
1374 #endif
1375 	pthread__main->pt_tls->tcb_pthread = pthread__main;
1376 }
1377 
1378 static signed int
1379 /*ARGSUSED*/
1380 pthread__cmp(void *ctx, const void *n1, const void *n2)
1381 {
1382 	const uintptr_t p1 = (const uintptr_t)n1;
1383 	const uintptr_t p2 = (const uintptr_t)n2;
1384 
1385 	if (p1 < p2)
1386 		return -1;
1387 	if (p1 > p2)
1388 		return 1;
1389 	return 0;
1390 }
1391 
1392 /* Because getenv() wants to use locks. */
1393 char *
1394 pthread__getenv(const char *name)
1395 {
1396 	extern char **environ;
1397 	size_t l_name, offset;
1398 
1399 	if (issetugid())
1400 		return (NULL);
1401 
1402 	l_name = strlen(name);
1403 	for (offset = 0; environ[offset] != NULL; offset++) {
1404 		if (strncmp(name, environ[offset], l_name) == 0 &&
1405 		    environ[offset][l_name] == '=') {
1406 			return environ[offset] + l_name + 1;
1407 		}
1408 	}
1409 
1410 	return NULL;
1411 }
1412 
1413 pthread_mutex_t *
1414 pthread__hashlock(volatile const void *p)
1415 {
1416 	uintptr_t v;
1417 
1418 	v = (uintptr_t)p;
1419 	return &hashlocks[((v >> 9) ^ (v >> 3)) & (NHASHLOCK - 1)].mutex;
1420 }
1421 
1422 int
1423 pthread__checkpri(int pri)
1424 {
1425 	static int havepri;
1426 	static long min, max;
1427 
1428 	if (!havepri) {
1429 		min = sysconf(_SC_SCHED_PRI_MIN);
1430 		max = sysconf(_SC_SCHED_PRI_MAX);
1431 		havepri = 1;
1432 	}
1433 	return (pri < min || pri > max) ? EINVAL : 0;
1434 }
1435