1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
5 * Copyright (C) 2003 Daniel M. Eischen <deischen@freebsd.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
13 * disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 #include <sys/types.h>
32 #include <sys/signalvar.h>
33 #include <sys/rtprio.h>
34 #include <sys/mman.h>
35 #include <pthread.h>
36
37 #include "thr_private.h"
38
39 /*#define DEBUG_THREAD_KERN */
40 #ifdef DEBUG_THREAD_KERN
41 #define DBG_MSG stdout_debug
42 #else
43 #define DBG_MSG(x...)
44 #endif
45
46 static struct umutex addr_lock;
47 static struct wake_addr *wake_addr_head;
48 static struct wake_addr default_wake_addr;
49
50 /*
51 * This is called when the first thread (other than the initial
52 * thread) is created.
53 */
54 void
_thr_setthreaded(int threaded)55 _thr_setthreaded(int threaded)
56 {
57 __isthreaded = threaded;
58 }
59
60 void
_thr_assert_lock_level(void)61 _thr_assert_lock_level(void)
62 {
63 PANIC("locklevel <= 0");
64 }
65
66 int
_rtp_to_schedparam(const struct rtprio * rtp,int * policy,struct sched_param * param)67 _rtp_to_schedparam(const struct rtprio *rtp, int *policy,
68 struct sched_param *param)
69 {
70 switch(rtp->type) {
71 case RTP_PRIO_REALTIME:
72 *policy = SCHED_RR;
73 param->sched_priority = RTP_PRIO_MAX - rtp->prio;
74 break;
75 case RTP_PRIO_FIFO:
76 *policy = SCHED_FIFO;
77 param->sched_priority = RTP_PRIO_MAX - rtp->prio;
78 break;
79 default:
80 *policy = SCHED_OTHER;
81 param->sched_priority = 0;
82 break;
83 }
84 return (0);
85 }
86
87 int
_schedparam_to_rtp(int policy,const struct sched_param * param,struct rtprio * rtp)88 _schedparam_to_rtp(int policy, const struct sched_param *param,
89 struct rtprio *rtp)
90 {
91 switch(policy) {
92 case SCHED_RR:
93 rtp->type = RTP_PRIO_REALTIME;
94 rtp->prio = RTP_PRIO_MAX - param->sched_priority;
95 break;
96 case SCHED_FIFO:
97 rtp->type = RTP_PRIO_FIFO;
98 rtp->prio = RTP_PRIO_MAX - param->sched_priority;
99 break;
100 case SCHED_OTHER:
101 default:
102 rtp->type = RTP_PRIO_NORMAL;
103 rtp->prio = 0;
104 break;
105 }
106 return (0);
107 }
108
109 int
_thr_getscheduler(lwpid_t lwpid,int * policy,struct sched_param * param)110 _thr_getscheduler(lwpid_t lwpid, int *policy, struct sched_param *param)
111 {
112 struct rtprio rtp;
113 int ret;
114
115 ret = rtprio_thread(RTP_LOOKUP, lwpid, &rtp);
116 if (ret == -1)
117 return (ret);
118 _rtp_to_schedparam(&rtp, policy, param);
119 return (0);
120 }
121
122 int
_thr_setscheduler(lwpid_t lwpid,int policy,const struct sched_param * param)123 _thr_setscheduler(lwpid_t lwpid, int policy, const struct sched_param *param)
124 {
125 struct rtprio rtp;
126
127 _schedparam_to_rtp(policy, param, &rtp);
128 return (rtprio_thread(RTP_SET, lwpid, &rtp));
129 }
130
131 void
_thr_wake_addr_init(void)132 _thr_wake_addr_init(void)
133 {
134 _thr_umutex_init(&addr_lock);
135 wake_addr_head = NULL;
136 }
137
138 /*
139 * Allocate wake-address, the memory area is never freed after
140 * allocated, this becauses threads may be referencing it.
141 */
142 struct wake_addr *
_thr_alloc_wake_addr(void)143 _thr_alloc_wake_addr(void)
144 {
145 struct pthread *curthread;
146 struct wake_addr *p;
147
148 if (_thr_initial == NULL) {
149 return &default_wake_addr;
150 }
151
152 curthread = _get_curthread();
153
154 THR_LOCK_ACQUIRE(curthread, &addr_lock);
155 if (wake_addr_head == NULL) {
156 unsigned i;
157 unsigned pagesize = getpagesize();
158 struct wake_addr *pp = (struct wake_addr *)
159 mmap(NULL, pagesize, PROT_READ|PROT_WRITE,
160 MAP_ANON|MAP_PRIVATE, -1, 0);
161 for (i = 1; i < pagesize/sizeof(struct wake_addr); ++i)
162 pp[i].link = &pp[i+1];
163 pp[i-1].link = NULL;
164 wake_addr_head = &pp[1];
165 p = &pp[0];
166 } else {
167 p = wake_addr_head;
168 wake_addr_head = p->link;
169 }
170 THR_LOCK_RELEASE(curthread, &addr_lock);
171 p->value = 0;
172 return (p);
173 }
174
175 void
_thr_release_wake_addr(struct wake_addr * wa)176 _thr_release_wake_addr(struct wake_addr *wa)
177 {
178 struct pthread *curthread = _get_curthread();
179
180 if (wa == &default_wake_addr)
181 return;
182 THR_LOCK_ACQUIRE(curthread, &addr_lock);
183 wa->link = wake_addr_head;
184 wake_addr_head = wa;
185 THR_LOCK_RELEASE(curthread, &addr_lock);
186 }
187
188 /* Sleep on thread wakeup address */
189 int
_thr_sleep(struct pthread * curthread,int clockid,const struct timespec * abstime)190 _thr_sleep(struct pthread *curthread, int clockid,
191 const struct timespec *abstime)
192 {
193
194 if (curthread->wake_addr->value != 0)
195 return (0);
196
197 return _thr_umtx_timedwait_uint(&curthread->wake_addr->value, 0,
198 clockid, abstime, 0);
199 }
200
201 void
_thr_wake_all(unsigned int * waddrs[],int count)202 _thr_wake_all(unsigned int *waddrs[], int count)
203 {
204 int i;
205
206 for (i = 0; i < count; ++i)
207 *waddrs[i] = 1;
208 _umtx_op(waddrs, UMTX_OP_NWAKE_PRIVATE, count, NULL, NULL);
209 }
210