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