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