]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - lib/libthr/thread/thr_kern.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / lib / libthr / thread / thr_kern.c
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  * $FreeBSD$
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 int
54 _thr_setthreaded(int threaded)
55 {
56         if (((threaded == 0) ^ (__isthreaded == 0)) == 0)
57                 return (0);
58
59         __isthreaded = threaded;
60         return (0);
61 }
62
63 void
64 _thr_assert_lock_level()
65 {
66         PANIC("locklevel <= 0");
67 }
68
69 int
70 _rtp_to_schedparam(const struct rtprio *rtp, int *policy,
71         struct sched_param *param)
72 {
73         switch(rtp->type) {
74         case RTP_PRIO_REALTIME:
75                 *policy = SCHED_RR;
76                 param->sched_priority = RTP_PRIO_MAX - rtp->prio;
77                 break;
78         case RTP_PRIO_FIFO:
79                 *policy = SCHED_FIFO;
80                 param->sched_priority = RTP_PRIO_MAX - rtp->prio;
81                 break;
82         default:
83                 *policy = SCHED_OTHER;
84                 param->sched_priority = 0;
85                 break;
86         }
87         return (0);
88 }
89
90 int
91 _schedparam_to_rtp(int policy, const struct sched_param *param,
92         struct rtprio *rtp)
93 {
94         switch(policy) {
95         case SCHED_RR:
96                 rtp->type = RTP_PRIO_REALTIME;
97                 rtp->prio = RTP_PRIO_MAX - param->sched_priority;
98                 break;
99         case SCHED_FIFO:
100                 rtp->type = RTP_PRIO_FIFO;
101                 rtp->prio = RTP_PRIO_MAX - param->sched_priority;
102                 break;
103         case SCHED_OTHER:
104         default:
105                 rtp->type = RTP_PRIO_NORMAL;
106                 rtp->prio = 0;
107                 break;
108         }
109         return (0);
110 }
111
112 int
113 _thr_getscheduler(lwpid_t lwpid, int *policy, struct sched_param *param)
114 {
115         struct rtprio rtp;
116         int ret;
117
118         ret = rtprio_thread(RTP_LOOKUP, lwpid, &rtp);
119         if (ret == -1)
120                 return (ret);
121         _rtp_to_schedparam(&rtp, policy, param);
122         return (0);
123 }
124
125 int
126 _thr_setscheduler(lwpid_t lwpid, int policy, const struct sched_param *param)
127 {
128         struct rtprio rtp;
129
130         _schedparam_to_rtp(policy, param, &rtp);
131         return (rtprio_thread(RTP_SET, lwpid, &rtp));
132 }
133
134 void
135 _thr_wake_addr_init(void)
136 {
137         _thr_umutex_init(&addr_lock);
138         wake_addr_head = NULL;
139 }
140
141 /*
142  * Allocate wake-address, the memory area is never freed after
143  * allocated, this becauses threads may be referencing it.
144  */
145 struct wake_addr *
146 _thr_alloc_wake_addr(void)
147 {
148         struct pthread *curthread;
149         struct wake_addr *p;
150
151         if (_thr_initial == NULL) {
152                 return &default_wake_addr;
153         }
154
155         curthread = _get_curthread();
156
157         THR_LOCK_ACQUIRE(curthread, &addr_lock);
158         if (wake_addr_head == NULL) {
159                 unsigned i;
160                 unsigned pagesize = getpagesize();
161                 struct wake_addr *pp = (struct wake_addr *)
162                         mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE,
163                         MAP_ANON|MAP_PRIVATE, -1, 0);
164                 for (i = 1; i < pagesize/sizeof(struct wake_addr); ++i)
165                         pp[i].link = &pp[i+1];
166                 pp[i-1].link = NULL;    
167                 wake_addr_head = &pp[1];
168                 p = &pp[0];
169         } else {
170                 p = wake_addr_head;
171                 wake_addr_head = p->link;
172         }
173         THR_LOCK_RELEASE(curthread, &addr_lock);
174         p->value = 0;
175         return (p);
176 }
177
178 void
179 _thr_release_wake_addr(struct wake_addr *wa)
180 {
181         struct pthread *curthread = _get_curthread();
182
183         if (wa == &default_wake_addr)
184                 return;
185         THR_LOCK_ACQUIRE(curthread, &addr_lock);
186         wa->link = wake_addr_head;
187         wake_addr_head = wa;
188         THR_LOCK_RELEASE(curthread, &addr_lock);
189 }
190
191 /* Sleep on thread wakeup address */
192 int
193 _thr_sleep(struct pthread *curthread, int clockid,
194         const struct timespec *abstime)
195 {
196
197         if (curthread->wake_addr->value != 0)
198                 return (0);
199
200         return _thr_umtx_timedwait_uint(&curthread->wake_addr->value, 0,
201                  clockid, abstime, 0);
202 }
203
204 void
205 _thr_wake_all(unsigned int *waddrs[], int count)
206 {
207         int i;
208
209         for (i = 0; i < count; ++i)
210                 *waddrs[i] = 1;
211         _umtx_op(waddrs, UMTX_OP_NWAKE_PRIVATE, count, NULL, NULL);
212 }