sched.h revision 1.21 1 1.21 riastrad /* $NetBSD: sched.h,v 1.21 2021/12/19 12:35:54 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.21 riastrad unsigned start, end;
70 1.21 riastrad
71 1.21 riastrad KASSERT(timeout >= 0);
72 1.21 riastrad KASSERT(timeout < MAX_SCHEDULE_TIMEOUT);
73 1.3 riastrad
74 1.4 nonaka if (cold) {
75 1.11 riastrad unsigned us;
76 1.11 riastrad if (hz <= 1000) {
77 1.13 christos unsigned ms = hztoms(MIN((unsigned long)timeout,
78 1.21 riastrad mstohz(INT_MAX/2)));
79 1.11 riastrad us = MIN(ms, INT_MAX/1000)*1000;
80 1.11 riastrad } else if (hz <= 1000000) {
81 1.11 riastrad us = MIN(timeout, (INT_MAX/1000000)/hz)*hz*1000000;
82 1.11 riastrad } else {
83 1.11 riastrad us = timeout/(1000000/hz);
84 1.11 riastrad }
85 1.11 riastrad DELAY(us);
86 1.4 nonaka return 0;
87 1.4 nonaka }
88 1.4 nonaka
89 1.15 maxv start = getticks();
90 1.12 riastrad /* Caller is expected to loop anyway, so no harm in truncating. */
91 1.21 riastrad (void)kpause("loonix", /*intr*/false, MIN(timeout, INT_MAX/2), NULL);
92 1.15 maxv end = getticks();
93 1.3 riastrad
94 1.21 riastrad return timeout - MIN(timeout, (end - start));
95 1.3 riastrad }
96 1.3 riastrad
97 1.16 riastrad static inline bool
98 1.16 riastrad need_resched(void)
99 1.16 riastrad {
100 1.16 riastrad /* XXX kpreempt_disable */
101 1.16 riastrad /* XXX ci_want_resched */
102 1.16 riastrad return (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD);
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.17 riastrad if (state & TASK_UNINTERRUPTIBLE)
118 1.17 riastrad return false;
119 1.17 riastrad return sigispending(curlwp, 0);
120 1.17 riastrad }
121 1.17 riastrad
122 1.20 riastrad static inline void
123 1.20 riastrad sched_setscheduler(struct proc *p, int class, struct sched_param *param)
124 1.20 riastrad {
125 1.20 riastrad
126 1.20 riastrad KASSERT(p == curproc);
127 1.20 riastrad KASSERT(class == SCHED_FIFO);
128 1.20 riastrad KASSERT(param->sched_priority == 1);
129 1.20 riastrad
130 1.20 riastrad lwp_lock(curlwp);
131 1.20 riastrad curlwp->l_class = class;
132 1.20 riastrad lwp_changepri(curlwp, PRI_KERNEL_RT);
133 1.20 riastrad lwp_unlock(curlwp);
134 1.20 riastrad }
135 1.20 riastrad
136 1.2 riastrad #endif /* _LINUX_SCHED_H_ */
137