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