xref: /netbsd-src/lib/libpthread/pthread_misc.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: pthread_misc.c,v 1.3 2007/11/13 17:20:09 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 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.
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_misc.c,v 1.3 2007/11/13 17:20:09 ad Exp $");
41 
42 #include <sys/types.h>
43 #include <sys/signal.h>
44 #include <sys/time.h>
45 
46 #include <errno.h>
47 #include <lwp.h>
48 
49 #include "pthread.h"
50 #include "pthread_int.h"
51 
52 int	pthread__sched_yield(void);
53 
54 int	_sys___sigprocmask14(int, const sigset_t *, sigset_t *);
55 int	_sys_nanosleep(const struct timespec *, struct timespec *);
56 int	_sys_sched_yield(void);
57 
58 __strong_alias(_nanosleep, nanosleep)
59 __strong_alias(__libc_thr_sigsetmask,pthread_sigmask)
60 __strong_alias(__sigprocmask14,pthread_sigmask)
61 __strong_alias(__libc_thr_yield,pthread__sched_yield)
62 
63 /*ARGSUSED*/
64 int
65 pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
66 {
67 	if (param == NULL || policy == NULL)
68 		return EINVAL;
69 	param->sched_priority = 0;
70 	*policy = SCHED_RR;
71 	return 0;
72 }
73 
74 /*ARGSUSED*/
75 int
76 pthread_setschedparam(pthread_t thread, int policy,
77     const struct sched_param *param)
78 {
79 	if (param == NULL || policy < SCHED_FIFO || policy > SCHED_RR)
80 		return EINVAL;
81 	if (param->sched_priority > 0 || policy != SCHED_RR)
82 		return ENOTSUP;
83 	return 0;
84 }
85 
86 int
87 pthread_kill(pthread_t thread, int sig)
88 {
89 
90 	if ((sig < 0) || (sig >= _NSIG))
91 		return EINVAL;
92 	if (pthread__find(thread) != 0)
93 		return ESRCH;
94 
95 	return _lwp_kill(thread->pt_lid, sig);
96 }
97 
98 int
99 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
100 {
101 
102 	return _sys___sigprocmask14(how, set, oset);
103 }
104 
105 int
106 nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
107 {
108 
109 	/*
110 	 * For now, just nanosleep.  In the future, maybe pass a ucontext_t
111 	 * to _lwp_nanosleep() and allow it to recycle our kernel stack.
112 	 */
113 	return  _sys_nanosleep(rqtp, rmtp);
114 }
115 
116 int
117 pthread__sched_yield(void)
118 {
119 	pthread_t self;
120 	int error;
121 
122 	self = pthread__self();
123 
124 #ifdef PTHREAD__HAVE_ATOMIC
125 	/* Memory barrier for unlocked mutex release. */
126 	pthread__membar_producer();
127 #endif
128 	self->pt_blocking++;
129 	error = _sys_sched_yield();
130 	self->pt_blocking--;
131 
132 	return error;
133 }
134