2d2f2da9bffcd2e8febec24cd7e38768b75ae43d
[dragonfly.git] / lib / libthread_xu / thread / thr_kern.c
1 /*
2  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
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.
14  *
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.
25  */
26
27 #include <sys/types.h>
28 #include <sys/signalvar.h>
29 #include <sys/rtprio.h>
30 #include <sys/lwp.h>
31 #include <pthread.h>
32
33 #include "thr_private.h"
34
35 /*#define DEBUG_THREAD_KERN */
36 #ifdef DEBUG_THREAD_KERN
37 #define DBG_MSG         stdout_debug
38 #else
39 #define DBG_MSG(x...)
40 #endif
41
42 /*
43  * This is called when the first thread (other than the initial
44  * thread) is created.
45  */
46 int
47 _thr_setthreaded(int threaded)
48 {
49         if (((threaded == 0) ^ (__isthreaded == 0)) == 0)
50                 return (0);
51
52         __isthreaded = threaded;
53 #if 0
54         if (threaded != 0) {
55                 _thr_rtld_init();
56         } else {
57                 _thr_rtld_fini();
58         }
59 #endif
60         return (0);
61 }
62
63 void
64 _thr_signal_block(struct pthread *curthread)
65 {
66         sigset_t set;
67
68         if (curthread->sigblock > 0) {
69                 curthread->sigblock++;
70                 return;
71         }
72         SIGFILLSET(set);
73         SIGDELSET(set, SIGBUS);
74         SIGDELSET(set, SIGILL);
75         SIGDELSET(set, SIGFPE);
76         SIGDELSET(set, SIGSEGV);
77         SIGDELSET(set, SIGTRAP);
78         __sys_sigprocmask(SIG_BLOCK, &set, &curthread->sigmask);
79         curthread->sigblock++;
80 }
81
82 void
83 _thr_signal_unblock(struct pthread *curthread)
84 {
85         if (--curthread->sigblock == 0)
86                 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
87 }
88
89 void
90 _thr_assert_lock_level(void)
91 {
92         PANIC("locklevel <= 0");
93 }
94
95 int
96 _thr_send_sig(struct pthread *thread, int sig)
97 {
98         return (lwp_kill(-1, thread->tid, sig));
99 }
100
101 int
102 _thr_get_tid(void)
103 {
104         return (lwp_gettid());
105 }
106
107 /*
108  * We don't use the priority for SCHED_OTHER, but
109  * some programs may depend on getting an error when
110  * setting a priority that is out of the range returned
111  * by sched_get_priority_{min,max}. Not sure if this
112  * falls into implementation defined behavior or not.
113  */
114 int
115 _thr_set_sched_other_prio(struct pthread *pth __unused, int prio)
116 {
117         static int max, min, init_status;
118
119         /*
120          * switch (init_status) {
121          * case 0: need initialization
122          * case 1: initialization successful
123          * case 2: initialization failed. can't happen, but if
124          *         it does, accept all and hope for the best.
125          *         It's not like we use it anyway.
126          */
127         if (!init_status) {
128                 int tmp = errno;
129
130                 errno = 0;
131                 init_status = 1;
132                 if (((min = sched_get_priority_min(SCHED_OTHER)) == -1) &&
133                                                                 (errno != 0))
134                         init_status = 2;
135                 if (((max = sched_get_priority_max(SCHED_OTHER)) == -1) &&
136                                                                 (errno != 0))
137                         init_status = 2;
138                 errno = tmp;
139         }
140         if ((init_status == 2) || ((prio >= min) && (prio <= max))) {
141                 return 0;
142         }
143         errno = ENOTSUP;
144         return -1;
145 }
146
147 int
148 _rtp_to_schedparam(const struct rtprio *rtp, int *policy,
149         struct sched_param *param)
150 {
151         switch(rtp->type) {
152         case RTP_PRIO_REALTIME:
153                 *policy = SCHED_RR;
154                 param->sched_priority = RTP_PRIO_MAX - rtp->prio;
155                 break;
156         case RTP_PRIO_FIFO:
157                 *policy = SCHED_FIFO;
158                 param->sched_priority = RTP_PRIO_MAX - rtp->prio;
159                 break;
160         default:
161                 *policy = SCHED_OTHER;
162                 param->sched_priority = 0;
163                 break;
164         }
165         return (0);
166 }
167
168 int
169 _schedparam_to_rtp(int policy, const struct sched_param *param,
170         struct rtprio *rtp)
171 {
172         switch(policy) {
173         case SCHED_RR:
174                 rtp->type = RTP_PRIO_REALTIME;
175                 rtp->prio = RTP_PRIO_MAX - param->sched_priority;
176                 break;
177         case SCHED_FIFO:
178                 rtp->type = RTP_PRIO_FIFO;
179                 rtp->prio = RTP_PRIO_MAX - param->sched_priority;
180                 break;
181         case SCHED_OTHER:
182         default:
183                 rtp->type = RTP_PRIO_NORMAL;
184                 rtp->prio = 0;
185                 break;
186         }
187         return (0);
188 }
189
190 int
191 _thr_getscheduler(lwpid_t lwpid, int *policy, struct sched_param *param)
192 {
193         struct pthread *curthread = tls_get_curthread();
194         struct rtprio rtp;
195         int ret;
196
197         if (lwpid == curthread->tid)
198                 lwpid = -1;
199         ret = lwp_rtprio(RTP_LOOKUP, 0, lwpid, &rtp);
200         if (ret == -1)
201                 return (ret);
202         _rtp_to_schedparam(&rtp, policy, param);
203         return (0);
204 }
205
206 int
207 _thr_setscheduler(lwpid_t lwpid, int policy, const struct sched_param *param)
208 {
209         struct pthread *curthread = tls_get_curthread();
210         struct rtprio rtp;
211
212         if (lwpid == curthread->tid)
213                 lwpid = -1;
214         _schedparam_to_rtp(policy, param, &rtp);
215         return (lwp_rtprio(RTP_SET, 0, lwpid, &rtp));
216 }