2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/types.h>
28 #include <sys/signalvar.h>
29 #include <sys/rtprio.h>
31 #include "thr_private.h"
33 /*#define DEBUG_THREAD_KERN */
34 #ifdef DEBUG_THREAD_KERN
35 #define DBG_MSG stdout_debug
41 * This is called when the first thread (other than the initial
45 _thr_setthreaded(int threaded)
47 if (((threaded == 0) ^ (__isthreaded == 0)) == 0)
50 __isthreaded = threaded;
62 _thr_signal_block(struct pthread *curthread)
66 if (curthread->sigblock > 0) {
67 curthread->sigblock++;
71 SIGDELSET(set, SIGBUS);
72 SIGDELSET(set, SIGILL);
73 SIGDELSET(set, SIGFPE);
74 SIGDELSET(set, SIGSEGV);
75 SIGDELSET(set, SIGTRAP);
76 __sys_sigprocmask(SIG_BLOCK, &set, &curthread->sigmask);
77 curthread->sigblock++;
81 _thr_signal_unblock(struct pthread *curthread)
83 if (--curthread->sigblock == 0)
84 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
88 _thr_assert_lock_level(void)
90 PANIC("locklevel <= 0");
94 _thr_send_sig(struct pthread *thread, int sig)
96 return (lwp_kill(-1, thread->tid, sig));
102 return (lwp_gettid());
106 * We don't use the priority for SCHED_OTHER, but
107 * some programs may depend on getting an error when
108 * setting a priority that is out of the range returned
109 * by sched_get_priority_{min,max}. Not sure if this
110 * falls into implementation defined behavior or not.
113 _thr_set_sched_other_prio(struct pthread *pth __unused, int prio)
115 static int max, min, init_status;
118 * switch (init_status) {
119 * case 0: need initialization
120 * case 1: initialization successful
121 * case 2: initialization failed. can't happen, but if
122 * it does, accept all and hope for the best.
123 * It's not like we use it anyway.
130 if (((min = sched_get_priority_min(SCHED_OTHER)) == -1) &&
133 if (((max = sched_get_priority_max(SCHED_OTHER)) == -1) &&
138 if ((init_status == 2) || ((prio >= min) && (prio <= max))) {
146 _rtp_to_schedparam(const struct rtprio *rtp, int *policy,
147 struct sched_param *param)
150 case RTP_PRIO_REALTIME:
152 param->sched_priority = RTP_PRIO_MAX - rtp->prio;
155 *policy = SCHED_FIFO;
156 param->sched_priority = RTP_PRIO_MAX - rtp->prio;
159 *policy = SCHED_OTHER;
160 param->sched_priority = 0;
167 _schedparam_to_rtp(int policy, const struct sched_param *param,
172 rtp->type = RTP_PRIO_REALTIME;
173 rtp->prio = RTP_PRIO_MAX - param->sched_priority;
176 rtp->type = RTP_PRIO_FIFO;
177 rtp->prio = RTP_PRIO_MAX - param->sched_priority;
181 rtp->type = RTP_PRIO_NORMAL;
189 _thr_getscheduler(lwpid_t lwpid, int *policy, struct sched_param *param)
191 struct pthread *curthread = tls_get_curthread();
195 if (lwpid == curthread->tid)
197 ret = lwp_rtprio(RTP_LOOKUP, 0, lwpid, &rtp);
200 _rtp_to_schedparam(&rtp, policy, param);
205 _thr_setscheduler(lwpid_t lwpid, int policy, const struct sched_param *param)
207 struct pthread *curthread = tls_get_curthread();
210 if (lwpid == curthread->tid)
212 _schedparam_to_rtp(policy, param, &rtp);
213 return (lwp_rtprio(RTP_SET, 0, lwpid, &rtp));