sched.h revision 1.20 1 1.20 riastrad /* $NetBSD: sched.h,v 1.20 2021/12/19 12:23:17 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.16 riastrad
58 1.2 riastrad #define current curproc
59 1.2 riastrad
60 1.2 riastrad static inline pid_t
61 1.2 riastrad task_pid_nr(struct proc *p)
62 1.2 riastrad {
63 1.2 riastrad return p->p_pid;
64 1.2 riastrad }
65 1.2 riastrad
66 1.3 riastrad static inline long
67 1.3 riastrad schedule_timeout_uninterruptible(long timeout)
68 1.3 riastrad {
69 1.5 nonaka long remain;
70 1.3 riastrad int start, end;
71 1.3 riastrad
72 1.4 nonaka if (cold) {
73 1.11 riastrad unsigned us;
74 1.11 riastrad if (hz <= 1000) {
75 1.13 christos unsigned ms = hztoms(MIN((unsigned long)timeout,
76 1.13 christos mstohz(INT_MAX)));
77 1.11 riastrad us = MIN(ms, INT_MAX/1000)*1000;
78 1.11 riastrad } else if (hz <= 1000000) {
79 1.11 riastrad us = MIN(timeout, (INT_MAX/1000000)/hz)*hz*1000000;
80 1.11 riastrad } else {
81 1.11 riastrad us = timeout/(1000000/hz);
82 1.11 riastrad }
83 1.11 riastrad DELAY(us);
84 1.4 nonaka return 0;
85 1.4 nonaka }
86 1.4 nonaka
87 1.15 maxv start = getticks();
88 1.12 riastrad /* Caller is expected to loop anyway, so no harm in truncating. */
89 1.12 riastrad (void)kpause("loonix", false /*!intr*/, MIN(timeout, INT_MAX), NULL);
90 1.15 maxv end = getticks();
91 1.3 riastrad
92 1.5 nonaka remain = timeout - (end - start);
93 1.5 nonaka return remain > 0 ? remain : 0;
94 1.3 riastrad }
95 1.3 riastrad
96 1.16 riastrad static inline bool
97 1.16 riastrad need_resched(void)
98 1.16 riastrad {
99 1.16 riastrad /* XXX kpreempt_disable */
100 1.16 riastrad /* XXX ci_want_resched */
101 1.16 riastrad return (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD);
102 1.16 riastrad }
103 1.16 riastrad
104 1.9 riastrad static inline void
105 1.9 riastrad cond_resched(void)
106 1.9 riastrad {
107 1.9 riastrad
108 1.14 ad preempt_point();
109 1.9 riastrad }
110 1.9 riastrad
111 1.17 riastrad static inline bool
112 1.17 riastrad signal_pending_state(int state, struct proc *p)
113 1.17 riastrad {
114 1.17 riastrad
115 1.17 riastrad KASSERT(p == current);
116 1.17 riastrad if (state & TASK_UNINTERRUPTIBLE)
117 1.17 riastrad return false;
118 1.17 riastrad return sigispending(curlwp, 0);
119 1.17 riastrad }
120 1.17 riastrad
121 1.20 riastrad static inline void
122 1.20 riastrad sched_setscheduler(struct proc *p, int class, struct sched_param *param)
123 1.20 riastrad {
124 1.20 riastrad
125 1.20 riastrad KASSERT(p == curproc);
126 1.20 riastrad KASSERT(class == SCHED_FIFO);
127 1.20 riastrad KASSERT(param->sched_priority == 1);
128 1.20 riastrad
129 1.20 riastrad lwp_lock(curlwp);
130 1.20 riastrad curlwp->l_class = class;
131 1.20 riastrad lwp_changepri(curlwp, PRI_KERNEL_RT);
132 1.20 riastrad lwp_unlock(curlwp);
133 1.20 riastrad }
134 1.20 riastrad
135 1.2 riastrad #endif /* _LINUX_SCHED_H_ */
136