Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libpthread / pthread_misc.c
blob1262bef9d77d40b61d32312e699ae70376fec058
1 /* $NetBSD: pthread_misc.c,v 1.13 2009/01/11 02:46:48 christos Exp $ */
3 /*-
4 * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams.
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.
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.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: pthread_misc.c,v 1.13 2009/01/11 02:46:48 christos Exp $");
35 #include <errno.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/pset.h>
41 #include <sys/signal.h>
42 #include <sys/time.h>
44 #include <lwp.h>
45 #include <sched.h>
47 #include "pthread.h"
48 #include "pthread_int.h"
50 int pthread__sched_yield(void);
52 int _sys___sigprocmask14(int, const sigset_t *, sigset_t *);
53 int _sys_sched_yield(void);
55 __strong_alias(__libc_thr_sigsetmask,pthread_sigmask)
56 __strong_alias(__sigprocmask14,pthread_sigmask)
57 __strong_alias(__libc_thr_yield,pthread__sched_yield)
59 int
60 pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
63 if (pthread__find(thread) != 0)
64 return ESRCH;
66 if (_sched_getparam(getpid(), thread->pt_lid, policy, param) < 0)
67 return errno;
69 return 0;
72 int
73 pthread_setschedparam(pthread_t thread, int policy,
74 const struct sched_param *param)
76 struct sched_param sp;
78 if (pthread__find(thread) != 0)
79 return ESRCH;
81 memcpy(&sp, param, sizeof(struct sched_param));
82 if (_sched_setparam(getpid(), thread->pt_lid, policy, &sp) < 0)
83 return errno;
85 return 0;
88 int
89 pthread_getaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
92 if (pthread__find(thread) != 0)
93 return ESRCH;
95 if (_sched_getaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
96 return errno;
98 return 0;
102 pthread_setaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
105 if (pthread__find(thread) != 0)
106 return ESRCH;
108 if (_sched_setaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
109 return errno;
111 return 0;
115 pthread_setschedprio(pthread_t thread, int prio)
117 struct sched_param sp;
119 if (pthread__find(thread) != 0)
120 return ESRCH;
122 sp.sched_priority = prio;
123 if (_sched_setparam(getpid(), thread->pt_lid, SCHED_NONE, &sp) < 0)
124 return errno;
126 return 0;
130 pthread_kill(pthread_t thread, int sig)
133 if ((sig < 0) || (sig >= _NSIG))
134 return EINVAL;
135 if (pthread__find(thread) != 0)
136 return ESRCH;
137 if (_lwp_kill(thread->pt_lid, sig))
138 return errno;
139 return 0;
143 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
146 if (_sys___sigprocmask14(how, set, oset))
147 return errno;
148 return 0;
152 pthread__sched_yield(void)
154 pthread_t self;
155 int error;
157 self = pthread__self();
159 /* Memory barrier for unlocked mutex release. */
160 membar_producer();
161 self->pt_blocking++;
162 error = _sys_sched_yield();
163 self->pt_blocking--;
164 membar_sync();
166 return error;