1 1.23 riastrad /* $NetBSD: sched.h,v 1.23 2021/12/27 13:28:41 riastradh Exp $ */ 2 1.2 riastrad 3 1.2 riastrad /*- 4 1.2 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 1.2 riastrad * All rights reserved. 6 1.2 riastrad * 7 1.2 riastrad * This code is derived from software contributed to The NetBSD Foundation 8 1.2 riastrad * by Taylor R. Campbell. 9 1.2 riastrad * 10 1.2 riastrad * Redistribution and use in source and binary forms, with or without 11 1.2 riastrad * modification, are permitted provided that the following conditions 12 1.2 riastrad * are met: 13 1.2 riastrad * 1. Redistributions of source code must retain the above copyright 14 1.2 riastrad * notice, this list of conditions and the following disclaimer. 15 1.2 riastrad * 2. Redistributions in binary form must reproduce the above copyright 16 1.2 riastrad * notice, this list of conditions and the following disclaimer in the 17 1.2 riastrad * documentation and/or other materials provided with the distribution. 18 1.2 riastrad * 19 1.2 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.2 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.2 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.2 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.2 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.2 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.2 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.2 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.2 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.2 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.2 riastrad * POSSIBILITY OF SUCH DAMAGE. 30 1.2 riastrad */ 31 1.2 riastrad 32 1.2 riastrad #ifndef _LINUX_SCHED_H_ 33 1.2 riastrad #define _LINUX_SCHED_H_ 34 1.2 riastrad 35 1.3 riastrad #include <sys/param.h> 36 1.19 riastrad 37 1.16 riastrad #include <sys/cdefs.h> 38 1.3 riastrad #include <sys/kernel.h> 39 1.20 riastrad #include <sys/lwp.h> 40 1.2 riastrad #include <sys/proc.h> 41 1.20 riastrad #include <sys/sched.h> 42 1.2 riastrad 43 1.19 riastrad #include <asm/barrier.h> 44 1.6 riastrad #include <asm/param.h> 45 1.8 riastrad #include <asm/processor.h> 46 1.19 riastrad 47 1.18 riastrad #include <linux/device.h> 48 1.6 riastrad #include <linux/errno.h> 49 1.6 riastrad 50 1.18 riastrad struct seq_file; 51 1.18 riastrad 52 1.3 riastrad #define TASK_COMM_LEN MAXCOMLEN 53 1.3 riastrad 54 1.10 riastrad #define MAX_SCHEDULE_TIMEOUT (INT_MAX/2) /* paranoia */ 55 1.10 riastrad 56 1.16 riastrad #define TASK_UNINTERRUPTIBLE __BIT(0) 57 1.23 riastrad #define TASK_INTERRUPTIBLE __BIT(1) 58 1.16 riastrad 59 1.2 riastrad #define current curproc 60 1.2 riastrad 61 1.2 riastrad static inline pid_t 62 1.2 riastrad task_pid_nr(struct proc *p) 63 1.2 riastrad { 64 1.2 riastrad return p->p_pid; 65 1.2 riastrad } 66 1.2 riastrad 67 1.3 riastrad static inline long 68 1.3 riastrad schedule_timeout_uninterruptible(long timeout) 69 1.3 riastrad { 70 1.21 riastrad unsigned start, end; 71 1.21 riastrad 72 1.21 riastrad KASSERT(timeout >= 0); 73 1.21 riastrad KASSERT(timeout < MAX_SCHEDULE_TIMEOUT); 74 1.3 riastrad 75 1.4 nonaka if (cold) { 76 1.11 riastrad unsigned us; 77 1.11 riastrad if (hz <= 1000) { 78 1.13 christos unsigned ms = hztoms(MIN((unsigned long)timeout, 79 1.21 riastrad mstohz(INT_MAX/2))); 80 1.11 riastrad us = MIN(ms, INT_MAX/1000)*1000; 81 1.11 riastrad } else if (hz <= 1000000) { 82 1.11 riastrad us = MIN(timeout, (INT_MAX/1000000)/hz)*hz*1000000; 83 1.11 riastrad } else { 84 1.11 riastrad us = timeout/(1000000/hz); 85 1.11 riastrad } 86 1.11 riastrad DELAY(us); 87 1.4 nonaka return 0; 88 1.4 nonaka } 89 1.4 nonaka 90 1.15 maxv start = getticks(); 91 1.12 riastrad /* Caller is expected to loop anyway, so no harm in truncating. */ 92 1.21 riastrad (void)kpause("loonix", /*intr*/false, MIN(timeout, INT_MAX/2), NULL); 93 1.15 maxv end = getticks(); 94 1.3 riastrad 95 1.21 riastrad return timeout - MIN(timeout, (end - start)); 96 1.3 riastrad } 97 1.3 riastrad 98 1.16 riastrad static inline bool 99 1.16 riastrad need_resched(void) 100 1.16 riastrad { 101 1.22 riastrad 102 1.22 riastrad return preempt_needed(); 103 1.16 riastrad } 104 1.16 riastrad 105 1.9 riastrad static inline void 106 1.9 riastrad cond_resched(void) 107 1.9 riastrad { 108 1.9 riastrad 109 1.14 ad preempt_point(); 110 1.9 riastrad } 111 1.9 riastrad 112 1.17 riastrad static inline bool 113 1.17 riastrad signal_pending_state(int state, struct proc *p) 114 1.17 riastrad { 115 1.17 riastrad 116 1.17 riastrad KASSERT(p == current); 117 1.23 riastrad KASSERT(state & (TASK_INTERRUPTIBLE|TASK_UNINTERRUPTIBLE)); 118 1.17 riastrad if (state & TASK_UNINTERRUPTIBLE) 119 1.17 riastrad return false; 120 1.17 riastrad return sigispending(curlwp, 0); 121 1.17 riastrad } 122 1.17 riastrad 123 1.20 riastrad static inline void 124 1.20 riastrad sched_setscheduler(struct proc *p, int class, struct sched_param *param) 125 1.20 riastrad { 126 1.20 riastrad 127 1.20 riastrad KASSERT(p == curproc); 128 1.20 riastrad KASSERT(class == SCHED_FIFO); 129 1.20 riastrad KASSERT(param->sched_priority == 1); 130 1.20 riastrad 131 1.20 riastrad lwp_lock(curlwp); 132 1.20 riastrad curlwp->l_class = class; 133 1.20 riastrad lwp_changepri(curlwp, PRI_KERNEL_RT); 134 1.20 riastrad lwp_unlock(curlwp); 135 1.20 riastrad } 136 1.20 riastrad 137 1.2 riastrad #endif /* _LINUX_SCHED_H_ */ 138