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