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 <pthread.h> 34 35 #include "thr_private.h" 36 37 /*#define DEBUG_THREAD_KERN */ 38 #ifdef DEBUG_THREAD_KERN 39 #define DBG_MSG stdout_debug 40 #else 41 #define DBG_MSG(x...) 42 #endif 43 44 /* 45 * This is called when the first thread (other than the initial 46 * thread) is created. 47 */ 48 int 49 _thr_setthreaded(int threaded) 50 { 51 if (((threaded == 0) ^ (__isthreaded == 0)) == 0) 52 return (0); 53 54 __isthreaded = threaded; 55 if (threaded != 0) { 56 _thr_rtld_init(); 57 } else { 58 _thr_rtld_fini(); 59 } 60 return (0); 61 } 62 63 void 64 _thr_signal_block(struct pthread *curthread) 65 { 66 sigset_t set; 67 68 if (curthread->sigblock > 0) { 69 curthread->sigblock++; 70 return; 71 } 72 SIGFILLSET(set); 73 SIGDELSET(set, SIGBUS); 74 SIGDELSET(set, SIGILL); 75 SIGDELSET(set, SIGFPE); 76 SIGDELSET(set, SIGSEGV); 77 SIGDELSET(set, SIGTRAP); 78 __sys_sigprocmask(SIG_BLOCK, &set, &curthread->sigmask); 79 curthread->sigblock++; 80 } 81 82 void 83 _thr_signal_unblock(struct pthread *curthread) 84 { 85 if (--curthread->sigblock == 0) 86 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL); 87 } 88 89 int 90 _thr_send_sig(struct pthread *thread, int sig) 91 { 92 return thr_kill(thread->tid, sig); 93 } 94 95 void 96 _thr_assert_lock_level() 97 { 98 PANIC("locklevel <= 0"); 99 } 100 101 int 102 _rtp_to_schedparam(const struct rtprio *rtp, int *policy, 103 struct sched_param *param) 104 { 105 switch(rtp->type) { 106 case RTP_PRIO_REALTIME: 107 *policy = SCHED_RR; 108 param->sched_priority = RTP_PRIO_MAX - rtp->prio; 109 break; 110 case RTP_PRIO_FIFO: 111 *policy = SCHED_FIFO; 112 param->sched_priority = RTP_PRIO_MAX - rtp->prio; 113 break; 114 default: 115 *policy = SCHED_OTHER; 116 param->sched_priority = 0; 117 break; 118 } 119 return (0); 120 } 121 122 int 123 _schedparam_to_rtp(int policy, const struct sched_param *param, 124 struct rtprio *rtp) 125 { 126 switch(policy) { 127 case SCHED_RR: 128 rtp->type = RTP_PRIO_REALTIME; 129 rtp->prio = RTP_PRIO_MAX - param->sched_priority; 130 break; 131 case SCHED_FIFO: 132 rtp->type = RTP_PRIO_FIFO; 133 rtp->prio = RTP_PRIO_MAX - param->sched_priority; 134 break; 135 case SCHED_OTHER: 136 default: 137 rtp->type = RTP_PRIO_NORMAL; 138 rtp->prio = 0; 139 break; 140 } 141 return (0); 142 } 143 144 int 145 _thr_getscheduler(lwpid_t lwpid, int *policy, struct sched_param *param) 146 { 147 struct rtprio rtp; 148 int ret; 149 150 ret = rtprio_thread(RTP_LOOKUP, lwpid, &rtp); 151 if (ret == -1) 152 return (ret); 153 _rtp_to_schedparam(&rtp, policy, param); 154 return (0); 155 } 156 157 int 158 _thr_setscheduler(lwpid_t lwpid, int policy, const struct sched_param *param) 159 { 160 struct rtprio rtp; 161 162 _schedparam_to_rtp(policy, param, &rtp); 163 return (rtprio_thread(RTP_SET, lwpid, &rtp)); 164 } 165