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