xref: /freebsd/lib/libthr/thread/thr_kern.c (revision 4f52dfbb8d6c4d446500c5b097e3806ec219fbd4)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
32 
33 #include <sys/types.h>
34 #include <sys/signalvar.h>
35 #include <sys/rtprio.h>
36 #include <sys/mman.h>
37 #include <pthread.h>
38 
39 #include "thr_private.h"
40 
41 /*#define DEBUG_THREAD_KERN */
42 #ifdef DEBUG_THREAD_KERN
43 #define DBG_MSG		stdout_debug
44 #else
45 #define DBG_MSG(x...)
46 #endif
47 
48 static struct umutex	addr_lock;
49 static struct wake_addr *wake_addr_head;
50 static struct wake_addr default_wake_addr;
51 
52 /*
53  * This is called when the first thread (other than the initial
54  * thread) is created.
55  */
56 int
57 _thr_setthreaded(int threaded)
58 {
59 	if (((threaded == 0) ^ (__isthreaded == 0)) == 0)
60 		return (0);
61 
62 	__isthreaded = threaded;
63 	return (0);
64 }
65 
66 void
67 _thr_assert_lock_level(void)
68 {
69 	PANIC("locklevel <= 0");
70 }
71 
72 int
73 _rtp_to_schedparam(const struct rtprio *rtp, int *policy,
74 	struct sched_param *param)
75 {
76 	switch(rtp->type) {
77 	case RTP_PRIO_REALTIME:
78 		*policy = SCHED_RR;
79 		param->sched_priority = RTP_PRIO_MAX - rtp->prio;
80 		break;
81 	case RTP_PRIO_FIFO:
82 		*policy = SCHED_FIFO;
83 		param->sched_priority = RTP_PRIO_MAX - rtp->prio;
84 		break;
85 	default:
86 		*policy = SCHED_OTHER;
87 		param->sched_priority = 0;
88 		break;
89 	}
90 	return (0);
91 }
92 
93 int
94 _schedparam_to_rtp(int policy, const struct sched_param *param,
95 	struct rtprio *rtp)
96 {
97 	switch(policy) {
98 	case SCHED_RR:
99 		rtp->type = RTP_PRIO_REALTIME;
100 		rtp->prio = RTP_PRIO_MAX - param->sched_priority;
101 		break;
102 	case SCHED_FIFO:
103 		rtp->type = RTP_PRIO_FIFO;
104 		rtp->prio = RTP_PRIO_MAX - param->sched_priority;
105 		break;
106 	case SCHED_OTHER:
107 	default:
108 		rtp->type = RTP_PRIO_NORMAL;
109 		rtp->prio = 0;
110 		break;
111 	}
112 	return (0);
113 }
114 
115 int
116 _thr_getscheduler(lwpid_t lwpid, int *policy, struct sched_param *param)
117 {
118 	struct rtprio rtp;
119 	int ret;
120 
121 	ret = rtprio_thread(RTP_LOOKUP, lwpid, &rtp);
122 	if (ret == -1)
123 		return (ret);
124 	_rtp_to_schedparam(&rtp, policy, param);
125 	return (0);
126 }
127 
128 int
129 _thr_setscheduler(lwpid_t lwpid, int policy, const struct sched_param *param)
130 {
131 	struct rtprio rtp;
132 
133 	_schedparam_to_rtp(policy, param, &rtp);
134 	return (rtprio_thread(RTP_SET, lwpid, &rtp));
135 }
136 
137 void
138 _thr_wake_addr_init(void)
139 {
140 	_thr_umutex_init(&addr_lock);
141 	wake_addr_head = NULL;
142 }
143 
144 /*
145  * Allocate wake-address, the memory area is never freed after
146  * allocated, this becauses threads may be referencing it.
147  */
148 struct wake_addr *
149 _thr_alloc_wake_addr(void)
150 {
151 	struct pthread *curthread;
152 	struct wake_addr *p;
153 
154 	if (_thr_initial == NULL) {
155 		return &default_wake_addr;
156 	}
157 
158 	curthread = _get_curthread();
159 
160 	THR_LOCK_ACQUIRE(curthread, &addr_lock);
161 	if (wake_addr_head == NULL) {
162 		unsigned i;
163 		unsigned pagesize = getpagesize();
164 		struct wake_addr *pp = (struct wake_addr *)
165 			mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE,
166 			MAP_ANON|MAP_PRIVATE, -1, 0);
167 		for (i = 1; i < pagesize/sizeof(struct wake_addr); ++i)
168 			pp[i].link = &pp[i+1];
169 		pp[i-1].link = NULL;
170 		wake_addr_head = &pp[1];
171 		p = &pp[0];
172 	} else {
173 		p = wake_addr_head;
174 		wake_addr_head = p->link;
175 	}
176 	THR_LOCK_RELEASE(curthread, &addr_lock);
177 	p->value = 0;
178 	return (p);
179 }
180 
181 void
182 _thr_release_wake_addr(struct wake_addr *wa)
183 {
184 	struct pthread *curthread = _get_curthread();
185 
186 	if (wa == &default_wake_addr)
187 		return;
188 	THR_LOCK_ACQUIRE(curthread, &addr_lock);
189 	wa->link = wake_addr_head;
190 	wake_addr_head = wa;
191 	THR_LOCK_RELEASE(curthread, &addr_lock);
192 }
193 
194 /* Sleep on thread wakeup address */
195 int
196 _thr_sleep(struct pthread *curthread, int clockid,
197 	const struct timespec *abstime)
198 {
199 
200 	if (curthread->wake_addr->value != 0)
201 		return (0);
202 
203 	return _thr_umtx_timedwait_uint(&curthread->wake_addr->value, 0,
204                  clockid, abstime, 0);
205 }
206 
207 void
208 _thr_wake_all(unsigned int *waddrs[], int count)
209 {
210 	int i;
211 
212 	for (i = 0; i < count; ++i)
213 		*waddrs[i] = 1;
214 	_umtx_op(waddrs, UMTX_OP_NWAKE_PRIVATE, count, NULL, NULL);
215 }
216