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>
33 #include "thr_private.h"
35 /*#define DEBUG_THREAD_KERN */
36 #ifdef DEBUG_THREAD_KERN
37 #define DBG_MSG stdout_debug
43 * This is called when the first thread (other than the initial
47 _thr_setthreaded(int threaded)
49 if (((threaded == 0) ^ (__isthreaded == 0)) == 0)
52 __isthreaded = threaded;
63 _thr_signal_block(struct pthread *curthread)
67 if (curthread->sigblock > 0) {
68 curthread->sigblock++;
72 SIGDELSET(set, SIGBUS);
73 SIGDELSET(set, SIGILL);
74 SIGDELSET(set, SIGFPE);
75 SIGDELSET(set, SIGSEGV);
76 SIGDELSET(set, SIGTRAP);
77 __sys_sigprocmask(SIG_BLOCK, &set, &curthread->sigmask);
78 curthread->sigblock++;
82 _thr_signal_unblock(struct pthread *curthread)
84 if (--curthread->sigblock == 0)
85 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
89 _thr_assert_lock_level(void)
91 PANIC("locklevel <= 0");
95 _thr_send_sig(struct pthread *thread, int sig)
97 return (lwp_kill(-1, thread->tid, sig));
103 return (lwp_gettid());
107 * We don't use the priority for SCHED_OTHER, but
108 * some programs may depend on getting an error when
109 * setting a priority that is out of the range returned
110 * by sched_get_priority_{min,max}. Not sure if this
111 * falls into implementation defined behavior or not.
114 _thr_set_sched_other_prio(struct pthread *pth __unused, int prio)
116 static int max, min, init_status;
119 * switch (init_status) {
120 * case 0: need initialization
121 * case 1: initialization successful
122 * case 2: initialization failed. can't happen, but if
123 * it does, accept all and hope for the best.
124 * It's not like we use it anyway.
131 if (((min = sched_get_priority_min(SCHED_OTHER)) == -1) &&
134 if (((max = sched_get_priority_max(SCHED_OTHER)) == -1) &&
139 if ((init_status == 2) || ((prio >= min) && (prio <= max))) {
147 _rtp_to_schedparam(const struct rtprio *rtp, int *policy,
148 struct sched_param *param)
151 case RTP_PRIO_REALTIME:
153 param->sched_priority = RTP_PRIO_MAX - rtp->prio;
156 *policy = SCHED_FIFO;
157 param->sched_priority = RTP_PRIO_MAX - rtp->prio;
160 *policy = SCHED_OTHER;
161 param->sched_priority = 0;
168 _schedparam_to_rtp(int policy, const struct sched_param *param,
173 rtp->type = RTP_PRIO_REALTIME;
174 rtp->prio = RTP_PRIO_MAX - param->sched_priority;
177 rtp->type = RTP_PRIO_FIFO;
178 rtp->prio = RTP_PRIO_MAX - param->sched_priority;
182 rtp->type = RTP_PRIO_NORMAL;
190 _thr_getscheduler(lwpid_t lwpid, int *policy, struct sched_param *param)
192 struct pthread *curthread = tls_get_curthread();
196 if (lwpid == curthread->tid)
198 ret = lwp_rtprio(RTP_LOOKUP, 0, lwpid, &rtp);
201 _rtp_to_schedparam(&rtp, policy, param);
206 _thr_setscheduler(lwpid_t lwpid, int policy, const struct sched_param *param)
208 struct pthread *curthread = tls_get_curthread();
211 if (lwpid == curthread->tid)
213 _schedparam_to_rtp(policy, param, &rtp);
214 return (lwp_rtprio(RTP_SET, 0, lwpid, &rtp));