xref: /netbsd-src/lib/libpthread/pthread.c (revision 74809f044aa1c34df701bf79c7bfb9794896fdf8)
1 /*	$NetBSD: pthread.c,v 1.106 2008/10/08 10:03: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.106 2008/10/08 10:03: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;
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 	    (nattr.pta_flags & PT_FLAG_EXPLICIT_SCHED) != 0)
420 		flag |= LWP_SUSPENDED;
421 	ret = _lwp_create(&newthread->pt_uc, flag, &newthread->pt_lid);
422 	if (ret != 0) {
423 		free(name);
424 		newthread->pt_state = PT_STATE_DEAD;
425 		pthread_mutex_lock(&pthread__deadqueue_lock);
426 		PTQ_INSERT_HEAD(&pthread__deadqueue, newthread, pt_deadq);
427 		pthread_mutex_unlock(&pthread__deadqueue_lock);
428 		return ret;
429 	}
430 
431 	if ((nattr.pta_flags & PT_FLAG_EXPLICIT_SCHED) != 0) {
432 		if (p != NULL) {
433 			(void)pthread_setschedparam(newthread, p->ptap_policy,
434 			    &p->ptap_sp);
435 		}
436 		if ((newthread->pt_flags & PT_FLAG_SUSPENDED) == 0) {
437 			(void)_lwp_continue(newthread->pt_lid);
438 		}
439 	}
440 
441 	*thread = newthread;
442 
443 	return 0;
444 }
445 
446 
447 static void
448 pthread__create_tramp(pthread_t self, void *(*start)(void *), void *arg)
449 {
450 	void *retval;
451 
452 #ifdef PTHREAD__HAVE_THREADREG
453 	/* Set up identity register. */
454 	pthread__threadreg_set(self);
455 #endif
456 
457 	/*
458 	 * Throw away some stack in a feeble attempt to reduce cache
459 	 * thrash.  May help for SMT processors.  XXX We should not
460 	 * be allocating stacks on fixed 2MB boundaries.  Needs a
461 	 * thread register or decent thread local storage.  Note
462 	 * that pt_lid may not be set by this point, but we don't
463 	 * care.
464 	 */
465 	(void)alloca(((unsigned)self->pt_lid & 7) << 8);
466 
467 	if (self->pt_name != NULL) {
468 		pthread_mutex_lock(&self->pt_lock);
469 		if (self->pt_name != NULL)
470 			(void)_lwp_setname(0, self->pt_name);
471 		pthread_mutex_unlock(&self->pt_lock);
472 	}
473 
474 	if (_lwp_ctl(LWPCTL_FEATURE_CURCPU, &self->pt_lwpctl)) {
475 		err(1, "_lwp_ctl");
476 	}
477 
478 	retval = (*start)(arg);
479 
480 	pthread_exit(retval);
481 
482 	/*NOTREACHED*/
483 	pthread__abort();
484 }
485 
486 int
487 pthread_suspend_np(pthread_t thread)
488 {
489 	pthread_t self;
490 
491 	self = pthread__self();
492 	if (self == thread) {
493 		return EDEADLK;
494 	}
495 	if (pthread__find(thread) != 0)
496 		return ESRCH;
497 	if (_lwp_suspend(thread->pt_lid) == 0)
498 		return 0;
499 	return errno;
500 }
501 
502 int
503 pthread_resume_np(pthread_t thread)
504 {
505 
506 	if (pthread__find(thread) != 0)
507 		return ESRCH;
508 	if (_lwp_continue(thread->pt_lid) == 0)
509 		return 0;
510 	return errno;
511 }
512 
513 void
514 pthread_exit(void *retval)
515 {
516 	pthread_t self;
517 	struct pt_clean_t *cleanup;
518 	char *name;
519 
520 	self = pthread__self();
521 
522 	/* Disable cancellability. */
523 	pthread_mutex_lock(&self->pt_lock);
524 	self->pt_flags |= PT_FLAG_CS_DISABLED;
525 	self->pt_cancel = 0;
526 
527 	/* Call any cancellation cleanup handlers */
528 	if (!PTQ_EMPTY(&self->pt_cleanup_stack)) {
529 		pthread_mutex_unlock(&self->pt_lock);
530 		while (!PTQ_EMPTY(&self->pt_cleanup_stack)) {
531 			cleanup = PTQ_FIRST(&self->pt_cleanup_stack);
532 			PTQ_REMOVE(&self->pt_cleanup_stack, cleanup, ptc_next);
533 			(*cleanup->ptc_cleanup)(cleanup->ptc_arg);
534 		}
535 		pthread_mutex_lock(&self->pt_lock);
536 	}
537 
538 	/* Perform cleanup of thread-specific data */
539 	pthread__destroy_tsd(self);
540 
541 	/* Signal our exit. */
542 	self->pt_exitval = retval;
543 	if (self->pt_flags & PT_FLAG_DETACHED) {
544 		self->pt_state = PT_STATE_DEAD;
545 		name = self->pt_name;
546 		self->pt_name = NULL;
547 		pthread_mutex_unlock(&self->pt_lock);
548 		if (name != NULL)
549 			free(name);
550 		pthread_mutex_lock(&pthread__deadqueue_lock);
551 		PTQ_INSERT_TAIL(&pthread__deadqueue, self, pt_deadq);
552 		pthread_mutex_unlock(&pthread__deadqueue_lock);
553 		_lwp_exit();
554 	} else {
555 		self->pt_state = PT_STATE_ZOMBIE;
556 		pthread_cond_broadcast(&self->pt_joiners);
557 		pthread_mutex_unlock(&self->pt_lock);
558 		/* Note: name will be freed by the joiner. */
559 		_lwp_exit();
560 	}
561 
562 	/*NOTREACHED*/
563 	pthread__abort();
564 	exit(1);
565 }
566 
567 
568 int
569 pthread_join(pthread_t thread, void **valptr)
570 {
571 	pthread_t self;
572 	int error;
573 
574 	self = pthread__self();
575 
576 	if (pthread__find(thread) != 0)
577 		return ESRCH;
578 
579 	if (thread->pt_magic != PT_MAGIC)
580 		return EINVAL;
581 
582 	if (thread == self)
583 		return EDEADLK;
584 
585 	self->pt_droplock = &thread->pt_lock;
586 	pthread_mutex_lock(&thread->pt_lock);
587 	for (;;) {
588 		if (thread->pt_state == PT_STATE_ZOMBIE)
589 			break;
590 		if (thread->pt_state == PT_STATE_DEAD) {
591 			pthread_mutex_unlock(&thread->pt_lock);
592 			self->pt_droplock = NULL;
593 			return ESRCH;
594 		}
595 		if ((thread->pt_flags & PT_FLAG_DETACHED) != 0) {
596 			pthread_mutex_unlock(&thread->pt_lock);
597 			self->pt_droplock = NULL;
598 			return EINVAL;
599 		}
600 		error = pthread_cond_wait(&thread->pt_joiners,
601 		    &thread->pt_lock);
602 		if (error != 0) {
603 			pthread__errorfunc(__FILE__, __LINE__,
604 			    __func__, "unexpected return from cond_wait()");
605 		}
606 
607 	}
608 	pthread__testcancel(self);
609 	if (valptr != NULL)
610 		*valptr = thread->pt_exitval;
611 	/* pthread__reap() will drop the lock. */
612 	pthread__reap(thread);
613 	self->pt_droplock = NULL;
614 
615 	return 0;
616 }
617 
618 static void
619 pthread__reap(pthread_t thread)
620 {
621 	char *name;
622 
623 	name = thread->pt_name;
624 	thread->pt_name = NULL;
625 	thread->pt_state = PT_STATE_DEAD;
626 	pthread_mutex_unlock(&thread->pt_lock);
627 
628 	pthread_mutex_lock(&pthread__deadqueue_lock);
629 	PTQ_INSERT_HEAD(&pthread__deadqueue, thread, pt_deadq);
630 	pthread_mutex_unlock(&pthread__deadqueue_lock);
631 
632 	if (name != NULL)
633 		free(name);
634 }
635 
636 int
637 pthread_equal(pthread_t t1, pthread_t t2)
638 {
639 
640 	/* Nothing special here. */
641 	return (t1 == t2);
642 }
643 
644 
645 int
646 pthread_detach(pthread_t thread)
647 {
648 
649 	if (pthread__find(thread) != 0)
650 		return ESRCH;
651 
652 	if (thread->pt_magic != PT_MAGIC)
653 		return EINVAL;
654 
655 	pthread_mutex_lock(&thread->pt_lock);
656 	thread->pt_flags |= PT_FLAG_DETACHED;
657 	if (thread->pt_state == PT_STATE_ZOMBIE) {
658 		/* pthread__reap() will drop the lock. */
659 		pthread__reap(thread);
660 	} else {
661 		/*
662 		 * Not valid for threads to be waiting in
663 		 * pthread_join() (there are intractable
664 		 * sync issues from the application
665 		 * perspective), but give those threads
666 		 * a chance anyway.
667 		 */
668 		pthread_cond_broadcast(&thread->pt_joiners);
669 		pthread_mutex_unlock(&thread->pt_lock);
670 	}
671 
672 	return 0;
673 }
674 
675 
676 int
677 pthread_getname_np(pthread_t thread, char *name, size_t len)
678 {
679 
680 	if (pthread__find(thread) != 0)
681 		return ESRCH;
682 
683 	if (thread->pt_magic != PT_MAGIC)
684 		return EINVAL;
685 
686 	pthread_mutex_lock(&thread->pt_lock);
687 	if (thread->pt_name == NULL)
688 		name[0] = '\0';
689 	else
690 		strlcpy(name, thread->pt_name, len);
691 	pthread_mutex_unlock(&thread->pt_lock);
692 
693 	return 0;
694 }
695 
696 
697 int
698 pthread_setname_np(pthread_t thread, const char *name, void *arg)
699 {
700 	char *oldname, *cp, newname[PTHREAD_MAX_NAMELEN_NP];
701 	int namelen;
702 
703 	if (pthread__find(thread) != 0)
704 		return ESRCH;
705 
706 	if (thread->pt_magic != PT_MAGIC)
707 		return EINVAL;
708 
709 	namelen = snprintf(newname, sizeof(newname), name, arg);
710 	if (namelen >= PTHREAD_MAX_NAMELEN_NP)
711 		return EINVAL;
712 
713 	cp = strdup(newname);
714 	if (cp == NULL)
715 		return ENOMEM;
716 
717 	pthread_mutex_lock(&thread->pt_lock);
718 	oldname = thread->pt_name;
719 	thread->pt_name = cp;
720 	(void)_lwp_setname(thread->pt_lid, cp);
721 	pthread_mutex_unlock(&thread->pt_lock);
722 
723 	if (oldname != NULL)
724 		free(oldname);
725 
726 	return 0;
727 }
728 
729 
730 
731 /*
732  * XXX There should be a way for applications to use the efficent
733  *  inline version, but there are opacity/namespace issues.
734  */
735 pthread_t
736 pthread_self(void)
737 {
738 
739 	return pthread__self();
740 }
741 
742 
743 int
744 pthread_cancel(pthread_t thread)
745 {
746 
747 	if (pthread__find(thread) != 0)
748 		return ESRCH;
749 	pthread_mutex_lock(&thread->pt_lock);
750 	thread->pt_flags |= PT_FLAG_CS_PENDING;
751 	if ((thread->pt_flags & PT_FLAG_CS_DISABLED) == 0) {
752 		thread->pt_cancel = 1;
753 		pthread_mutex_unlock(&thread->pt_lock);
754 		_lwp_wakeup(thread->pt_lid);
755 	} else
756 		pthread_mutex_unlock(&thread->pt_lock);
757 
758 	return 0;
759 }
760 
761 
762 int
763 pthread_setcancelstate(int state, int *oldstate)
764 {
765 	pthread_t self;
766 	int retval;
767 
768 	self = pthread__self();
769 	retval = 0;
770 
771 	pthread_mutex_lock(&self->pt_lock);
772 
773 	if (oldstate != NULL) {
774 		if (self->pt_flags & PT_FLAG_CS_DISABLED)
775 			*oldstate = PTHREAD_CANCEL_DISABLE;
776 		else
777 			*oldstate = PTHREAD_CANCEL_ENABLE;
778 	}
779 
780 	if (state == PTHREAD_CANCEL_DISABLE) {
781 		self->pt_flags |= PT_FLAG_CS_DISABLED;
782 		if (self->pt_cancel) {
783 			self->pt_flags |= PT_FLAG_CS_PENDING;
784 			self->pt_cancel = 0;
785 		}
786 	} else if (state == PTHREAD_CANCEL_ENABLE) {
787 		self->pt_flags &= ~PT_FLAG_CS_DISABLED;
788 		/*
789 		 * If a cancellation was requested while cancellation
790 		 * was disabled, note that fact for future
791 		 * cancellation tests.
792 		 */
793 		if (self->pt_flags & PT_FLAG_CS_PENDING) {
794 			self->pt_cancel = 1;
795 			/* This is not a deferred cancellation point. */
796 			if (self->pt_flags & PT_FLAG_CS_ASYNC) {
797 				pthread_mutex_unlock(&self->pt_lock);
798 				pthread__cancelled();
799 			}
800 		}
801 	} else
802 		retval = EINVAL;
803 
804 	pthread_mutex_unlock(&self->pt_lock);
805 
806 	return retval;
807 }
808 
809 
810 int
811 pthread_setcanceltype(int type, int *oldtype)
812 {
813 	pthread_t self;
814 	int retval;
815 
816 	self = pthread__self();
817 	retval = 0;
818 
819 	pthread_mutex_lock(&self->pt_lock);
820 
821 	if (oldtype != NULL) {
822 		if (self->pt_flags & PT_FLAG_CS_ASYNC)
823 			*oldtype = PTHREAD_CANCEL_ASYNCHRONOUS;
824 		else
825 			*oldtype = PTHREAD_CANCEL_DEFERRED;
826 	}
827 
828 	if (type == PTHREAD_CANCEL_ASYNCHRONOUS) {
829 		self->pt_flags |= PT_FLAG_CS_ASYNC;
830 		if (self->pt_cancel) {
831 			pthread_mutex_unlock(&self->pt_lock);
832 			pthread__cancelled();
833 		}
834 	} else if (type == PTHREAD_CANCEL_DEFERRED)
835 		self->pt_flags &= ~PT_FLAG_CS_ASYNC;
836 	else
837 		retval = EINVAL;
838 
839 	pthread_mutex_unlock(&self->pt_lock);
840 
841 	return retval;
842 }
843 
844 
845 void
846 pthread_testcancel(void)
847 {
848 	pthread_t self;
849 
850 	self = pthread__self();
851 	if (self->pt_cancel)
852 		pthread__cancelled();
853 }
854 
855 
856 /*
857  * POSIX requires that certain functions return an error rather than
858  * invoking undefined behavior even when handed completely bogus
859  * pthread_t values, e.g. stack garbage or (pthread_t)666. This
860  * utility routine searches the list of threads for the pthread_t
861  * value without dereferencing it.
862  */
863 int
864 pthread__find(pthread_t id)
865 {
866 	pthread_t target;
867 
868 	pthread_rwlock_rdlock(&pthread__alltree_lock);
869 	/* LINTED */
870 	target = RB_FIND(__pthread__alltree, &pthread__alltree, id);
871 	pthread_rwlock_unlock(&pthread__alltree_lock);
872 
873 	if (target == NULL || target->pt_state == PT_STATE_DEAD)
874 		return ESRCH;
875 
876 	return 0;
877 }
878 
879 
880 void
881 pthread__testcancel(pthread_t self)
882 {
883 
884 	if (self->pt_cancel)
885 		pthread__cancelled();
886 }
887 
888 
889 void
890 pthread__cancelled(void)
891 {
892 	pthread_mutex_t *droplock;
893 	pthread_t self;
894 
895 	self = pthread__self();
896 	droplock = self->pt_droplock;
897 	self->pt_droplock = NULL;
898 
899 	if (droplock != NULL && pthread_mutex_held_np(droplock))
900 		pthread_mutex_unlock(droplock);
901 
902 	pthread_exit(PTHREAD_CANCELED);
903 }
904 
905 
906 void
907 pthread__cleanup_push(void (*cleanup)(void *), void *arg, void *store)
908 {
909 	pthread_t self;
910 	struct pt_clean_t *entry;
911 
912 	self = pthread__self();
913 	entry = store;
914 	entry->ptc_cleanup = cleanup;
915 	entry->ptc_arg = arg;
916 	PTQ_INSERT_HEAD(&self->pt_cleanup_stack, entry, ptc_next);
917 }
918 
919 
920 void
921 pthread__cleanup_pop(int ex, void *store)
922 {
923 	pthread_t self;
924 	struct pt_clean_t *entry;
925 
926 	self = pthread__self();
927 	entry = store;
928 
929 	PTQ_REMOVE(&self->pt_cleanup_stack, entry, ptc_next);
930 	if (ex)
931 		(*entry->ptc_cleanup)(entry->ptc_arg);
932 }
933 
934 
935 int *
936 pthread__errno(void)
937 {
938 	pthread_t self;
939 
940 	self = pthread__self();
941 
942 	return &(self->pt_errno);
943 }
944 
945 ssize_t	_sys_write(int, const void *, size_t);
946 
947 void
948 pthread__assertfunc(const char *file, int line, const char *function,
949 		    const char *expr)
950 {
951 	char buf[1024];
952 	int len;
953 
954 	/*
955 	 * snprintf should not acquire any locks, or we could
956 	 * end up deadlocked if the assert caller held locks.
957 	 */
958 	len = snprintf(buf, 1024,
959 	    "assertion \"%s\" failed: file \"%s\", line %d%s%s%s\n",
960 	    expr, file, line,
961 	    function ? ", function \"" : "",
962 	    function ? function : "",
963 	    function ? "\"" : "");
964 
965 	_sys_write(STDERR_FILENO, buf, (size_t)len);
966 	(void)kill(getpid(), SIGABRT);
967 
968 	_exit(1);
969 }
970 
971 
972 void
973 pthread__errorfunc(const char *file, int line, const char *function,
974 		   const char *msg)
975 {
976 	char buf[1024];
977 	size_t len;
978 
979 	if (pthread__diagassert == 0)
980 		return;
981 
982 	/*
983 	 * snprintf should not acquire any locks, or we could
984 	 * end up deadlocked if the assert caller held locks.
985 	 */
986 	len = snprintf(buf, 1024,
987 	    "%s: Error detected by libpthread: %s.\n"
988 	    "Detected by file \"%s\", line %d%s%s%s.\n"
989 	    "See pthread(3) for information.\n",
990 	    getprogname(), msg, file, line,
991 	    function ? ", function \"" : "",
992 	    function ? function : "",
993 	    function ? "\"" : "");
994 
995 	if (pthread__diagassert & DIAGASSERT_STDERR)
996 		_sys_write(STDERR_FILENO, buf, len);
997 
998 	if (pthread__diagassert & DIAGASSERT_SYSLOG)
999 		syslog(LOG_DEBUG | LOG_USER, "%s", buf);
1000 
1001 	if (pthread__diagassert & DIAGASSERT_ABORT) {
1002 		(void)kill(getpid(), SIGABRT);
1003 		_exit(1);
1004 	}
1005 }
1006 
1007 /*
1008  * Thread park/unpark operations.  The kernel operations are
1009  * modelled after a brief description from "Multithreading in
1010  * the Solaris Operating Environment":
1011  *
1012  * http://www.sun.com/software/whitepapers/solaris9/multithread.pdf
1013  */
1014 
1015 #define	OOPS(msg)			\
1016     pthread__errorfunc(__FILE__, __LINE__, __func__, msg)
1017 
1018 int
1019 pthread__park(pthread_t self, pthread_mutex_t *lock,
1020 	      pthread_queue_t *queue, const struct timespec *abstime,
1021 	      int cancelpt, const void *hint)
1022 {
1023 	int rv, error;
1024 	void *obj;
1025 
1026 	/*
1027 	 * For non-interlocked release of mutexes we need a store
1028 	 * barrier before incrementing pt_blocking away from zero.
1029 	 * This is provided by pthread_mutex_unlock().
1030 	 */
1031 	self->pt_willpark = 1;
1032 	pthread_mutex_unlock(lock);
1033 	self->pt_willpark = 0;
1034 	self->pt_blocking++;
1035 
1036 	/*
1037 	 * Wait until we are awoken by a pending unpark operation,
1038 	 * a signal, an unpark posted after we have gone asleep,
1039 	 * or an expired timeout.
1040 	 *
1041 	 * It is fine to test the value of pt_sleepobj without
1042 	 * holding any locks, because:
1043 	 *
1044 	 * o Only the blocking thread (this thread) ever sets them
1045 	 *   to a non-NULL value.
1046 	 *
1047 	 * o Other threads may set them NULL, but if they do so they
1048 	 *   must also make this thread return from _lwp_park.
1049 	 *
1050 	 * o _lwp_park, _lwp_unpark and _lwp_unpark_all are system
1051 	 *   calls and all make use of spinlocks in the kernel.  So
1052 	 *   these system calls act as full memory barriers, and will
1053 	 *   ensure that the calling CPU's store buffers are drained.
1054 	 *   In combination with the spinlock release before unpark,
1055 	 *   this means that modification of pt_sleepobj/onq by another
1056 	 *   thread will become globally visible before that thread
1057 	 *   schedules an unpark operation on this thread.
1058 	 *
1059 	 * Note: the test in the while() statement dodges the park op if
1060 	 * we have already been awoken, unless there is another thread to
1061 	 * awaken.  This saves a syscall - if we were already awakened,
1062 	 * the next call to _lwp_park() would need to return early in order
1063 	 * to eat the previous wakeup.
1064 	 */
1065 	rv = 0;
1066 	do {
1067 		/*
1068 		 * If we deferred unparking a thread, arrange to
1069 		 * have _lwp_park() restart it before blocking.
1070 		 */
1071 		error = _lwp_park(abstime, self->pt_unpark, hint, hint);
1072 		self->pt_unpark = 0;
1073 		if (error != 0) {
1074 			switch (rv = errno) {
1075 			case EINTR:
1076 			case EALREADY:
1077 				rv = 0;
1078 				break;
1079 			case ETIMEDOUT:
1080 				break;
1081 			default:
1082 				OOPS("_lwp_park failed");
1083 				break;
1084 			}
1085 		}
1086 		/* Check for cancellation. */
1087 		if (cancelpt && self->pt_cancel)
1088 			rv = EINTR;
1089 	} while (self->pt_sleepobj != NULL && rv == 0);
1090 
1091 	/*
1092 	 * If we have been awoken early but are still on the queue,
1093 	 * then remove ourself.  Again, it's safe to do the test
1094 	 * without holding any locks.
1095 	 */
1096 	if (__predict_false(self->pt_sleepobj != NULL)) {
1097 		pthread_mutex_lock(lock);
1098 		if ((obj = self->pt_sleepobj) != NULL) {
1099 			PTQ_REMOVE(queue, self, pt_sleep);
1100 			self->pt_sleepobj = NULL;
1101 			if (obj != NULL && self->pt_early != NULL)
1102 				(*self->pt_early)(obj);
1103 		}
1104 		pthread_mutex_unlock(lock);
1105 	}
1106 	self->pt_early = NULL;
1107 	self->pt_blocking--;
1108 	membar_sync();
1109 
1110 	return rv;
1111 }
1112 
1113 void
1114 pthread__unpark(pthread_queue_t *queue, pthread_t self,
1115 		pthread_mutex_t *interlock)
1116 {
1117 	pthread_t target;
1118 	u_int max;
1119 	size_t nwaiters;
1120 
1121 	max = pthread__unpark_max;
1122 	nwaiters = self->pt_nwaiters;
1123 	target = PTQ_FIRST(queue);
1124 	if (nwaiters == max) {
1125 		/* Overflow. */
1126 		(void)_lwp_unpark_all(self->pt_waiters, nwaiters,
1127 		    __UNVOLATILE(&interlock->ptm_waiters));
1128 		nwaiters = 0;
1129 	}
1130 	target->pt_sleepobj = NULL;
1131 	self->pt_waiters[nwaiters++] = target->pt_lid;
1132 	PTQ_REMOVE(queue, target, pt_sleep);
1133 	self->pt_nwaiters = nwaiters;
1134 	pthread__mutex_deferwake(self, interlock);
1135 }
1136 
1137 void
1138 pthread__unpark_all(pthread_queue_t *queue, pthread_t self,
1139 		    pthread_mutex_t *interlock)
1140 {
1141 	pthread_t target;
1142 	u_int max;
1143 	size_t nwaiters;
1144 
1145 	max = pthread__unpark_max;
1146 	nwaiters = self->pt_nwaiters;
1147 	PTQ_FOREACH(target, queue, pt_sleep) {
1148 		if (nwaiters == max) {
1149 			/* Overflow. */
1150 			(void)_lwp_unpark_all(self->pt_waiters, nwaiters,
1151 			    __UNVOLATILE(&interlock->ptm_waiters));
1152 			nwaiters = 0;
1153 		}
1154 		target->pt_sleepobj = NULL;
1155 		self->pt_waiters[nwaiters++] = target->pt_lid;
1156 	}
1157 	self->pt_nwaiters = nwaiters;
1158 	PTQ_INIT(queue);
1159 	pthread__mutex_deferwake(self, interlock);
1160 }
1161 
1162 #undef	OOPS
1163 
1164 /*
1165  * Allocate a stack for a thread, and set it up. It needs to be aligned, so
1166  * that a thread can find itself by its stack pointer.
1167  */
1168 static int
1169 pthread__stackalloc(pthread_t *newt)
1170 {
1171 	void *addr;
1172 
1173 	addr = mmap(NULL, pthread__stacksize, PROT_READ|PROT_WRITE,
1174 	    MAP_ANON|MAP_PRIVATE | MAP_ALIGNED(pthread__stacksize_lg),
1175 	    -1, (off_t)0);
1176 
1177 	if (addr == MAP_FAILED)
1178 		return ENOMEM;
1179 
1180 	pthread__assert(((intptr_t)addr & pthread__stackmask) == 0);
1181 
1182 	return pthread__stackid_setup(addr, pthread__stacksize, newt);
1183 }
1184 
1185 
1186 /*
1187  * Set up the slightly special stack for the "initial" thread, which
1188  * runs on the normal system stack, and thus gets slightly different
1189  * treatment.
1190  */
1191 static void
1192 pthread__initmain(pthread_t *newt)
1193 {
1194 	struct rlimit slimit;
1195 	size_t pagesize;
1196 	pthread_t t;
1197 	void *base;
1198 	size_t size;
1199 	int error, ret;
1200 	char *value;
1201 
1202 	pagesize = (size_t)sysconf(_SC_PAGESIZE);
1203 	pthread__stacksize = 0;
1204 	ret = getrlimit(RLIMIT_STACK, &slimit);
1205 	if (ret == -1)
1206 		err(1, "Couldn't get stack resource consumption limits");
1207 
1208 	value = pthread__getenv("PTHREAD_STACKSIZE");
1209 	if (value != NULL) {
1210 		pthread__stacksize = atoi(value) * 1024;
1211 		if (pthread__stacksize > slimit.rlim_cur)
1212 			pthread__stacksize = (size_t)slimit.rlim_cur;
1213 	}
1214 	if (pthread__stacksize == 0)
1215 		pthread__stacksize = (size_t)slimit.rlim_cur;
1216 	if (pthread__stacksize < 4 * pagesize)
1217 		errx(1, "Stacksize limit is too low, minimum %zd kbyte.",
1218 		    4 * pagesize / 1024);
1219 
1220 	pthread__stacksize_lg = -1;
1221 	while (pthread__stacksize) {
1222 		pthread__stacksize >>= 1;
1223 		pthread__stacksize_lg++;
1224 	}
1225 
1226 	pthread__stacksize = (1 << pthread__stacksize_lg);
1227 	pthread__stackmask = pthread__stacksize - 1;
1228 	pthread__threadmask = ~pthread__stackmask;
1229 
1230 	base = (void *)(pthread__sp() & pthread__threadmask);
1231 	size = pthread__stacksize;
1232 
1233 	error = pthread__stackid_setup(base, size, &t);
1234 	if (error) {
1235 		/* XXX */
1236 		errx(2, "failed to setup main thread: error=%d", error);
1237 	}
1238 
1239 	*newt = t;
1240 
1241 #ifdef PTHREAD__HAVE_THREADREG
1242 	/* Set up identity register. */
1243 	pthread__threadreg_set(t);
1244 #endif
1245 }
1246 
1247 static int
1248 /*ARGSUSED*/
1249 pthread__stackid_setup(void *base, size_t size, pthread_t *tp)
1250 {
1251 	pthread_t t;
1252 	void *redaddr;
1253 	size_t pagesize;
1254 	int ret;
1255 
1256 	t = base;
1257 	pagesize = (size_t)sysconf(_SC_PAGESIZE);
1258 
1259 	/*
1260 	 * Put a pointer to the pthread in the bottom (but
1261          * redzone-protected section) of the stack.
1262 	 */
1263 	redaddr = STACK_SHRINK(STACK_MAX(base, size), pagesize);
1264 	t->pt_stack.ss_size = size - 2 * pagesize;
1265 #ifdef __MACHINE_STACK_GROWS_UP
1266 	t->pt_stack.ss_sp = (char *)(void *)base + pagesize;
1267 #else
1268 	t->pt_stack.ss_sp = (char *)(void *)base + 2 * pagesize;
1269 #endif
1270 
1271 	/* Protect the next-to-bottom stack page as a red zone. */
1272 	ret = mprotect(redaddr, pagesize, PROT_NONE);
1273 	if (ret == -1) {
1274 		return errno;
1275 	}
1276 	*tp = t;
1277 	return 0;
1278 }
1279 
1280 #ifndef lint
1281 static int
1282 pthread__cmp(struct __pthread_st *a, struct __pthread_st *b)
1283 {
1284 	return b - a;
1285 }
1286 RB_GENERATE_STATIC(__pthread__alltree, __pthread_st, pt_alltree, pthread__cmp)
1287 #endif
1288 
1289 /* Because getenv() wants to use locks. */
1290 char *
1291 pthread__getenv(const char *name)
1292 {
1293 	extern char *__findenv(const char *, int *);
1294 	int off;
1295 
1296 	return __findenv(name, &off);
1297 }
1298 
1299 pthread_mutex_t *
1300 pthread__hashlock(volatile const void *p)
1301 {
1302 	uintptr_t v;
1303 
1304 	v = (uintptr_t)p;
1305 	return &hashlocks[((v >> 9) ^ (v >> 3)) & (NHASHLOCK - 1)].mutex;
1306 }
1307 
1308 int
1309 pthread__checkpri(int pri)
1310 {
1311 	static int havepri;
1312 	static long min, max;
1313 
1314 	if (!havepri) {
1315 		min = sysconf(_SC_SCHED_PRI_MIN);
1316 		max = sysconf(_SC_SCHED_PRI_MAX);
1317 		havepri = 1;
1318 	}
1319 	return (pri < min || pri > max) ? EINVAL : 0;
1320 }
1321