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