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