sched.h revision 1.19 1 1.19 riastrad /* $NetBSD: sched.h,v 1.19 2021/12/19 11:49:12 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.2 riastrad #include <sys/proc.h>
40 1.2 riastrad
41 1.19 riastrad #include <asm/barrier.h>
42 1.6 riastrad #include <asm/param.h>
43 1.8 riastrad #include <asm/processor.h>
44 1.19 riastrad
45 1.18 riastrad #include <linux/device.h>
46 1.6 riastrad #include <linux/errno.h>
47 1.6 riastrad
48 1.18 riastrad struct seq_file;
49 1.18 riastrad
50 1.3 riastrad #define TASK_COMM_LEN MAXCOMLEN
51 1.3 riastrad
52 1.10 riastrad #define MAX_SCHEDULE_TIMEOUT (INT_MAX/2) /* paranoia */
53 1.10 riastrad
54 1.16 riastrad #define TASK_UNINTERRUPTIBLE __BIT(0)
55 1.16 riastrad
56 1.2 riastrad #define current curproc
57 1.2 riastrad
58 1.2 riastrad static inline pid_t
59 1.2 riastrad task_pid_nr(struct proc *p)
60 1.2 riastrad {
61 1.2 riastrad return p->p_pid;
62 1.2 riastrad }
63 1.2 riastrad
64 1.3 riastrad static inline long
65 1.3 riastrad schedule_timeout_uninterruptible(long timeout)
66 1.3 riastrad {
67 1.5 nonaka long remain;
68 1.3 riastrad int start, end;
69 1.3 riastrad
70 1.4 nonaka if (cold) {
71 1.11 riastrad unsigned us;
72 1.11 riastrad if (hz <= 1000) {
73 1.13 christos unsigned ms = hztoms(MIN((unsigned long)timeout,
74 1.13 christos mstohz(INT_MAX)));
75 1.11 riastrad us = MIN(ms, INT_MAX/1000)*1000;
76 1.11 riastrad } else if (hz <= 1000000) {
77 1.11 riastrad us = MIN(timeout, (INT_MAX/1000000)/hz)*hz*1000000;
78 1.11 riastrad } else {
79 1.11 riastrad us = timeout/(1000000/hz);
80 1.11 riastrad }
81 1.11 riastrad DELAY(us);
82 1.4 nonaka return 0;
83 1.4 nonaka }
84 1.4 nonaka
85 1.15 maxv start = getticks();
86 1.12 riastrad /* Caller is expected to loop anyway, so no harm in truncating. */
87 1.12 riastrad (void)kpause("loonix", false /*!intr*/, MIN(timeout, INT_MAX), NULL);
88 1.15 maxv end = getticks();
89 1.3 riastrad
90 1.5 nonaka remain = timeout - (end - start);
91 1.5 nonaka return remain > 0 ? remain : 0;
92 1.3 riastrad }
93 1.3 riastrad
94 1.16 riastrad static inline bool
95 1.16 riastrad need_resched(void)
96 1.16 riastrad {
97 1.16 riastrad /* XXX kpreempt_disable */
98 1.16 riastrad /* XXX ci_want_resched */
99 1.16 riastrad return (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD);
100 1.16 riastrad }
101 1.16 riastrad
102 1.9 riastrad static inline void
103 1.9 riastrad cond_resched(void)
104 1.9 riastrad {
105 1.9 riastrad
106 1.14 ad preempt_point();
107 1.9 riastrad }
108 1.9 riastrad
109 1.17 riastrad static inline bool
110 1.17 riastrad signal_pending_state(int state, struct proc *p)
111 1.17 riastrad {
112 1.17 riastrad
113 1.17 riastrad KASSERT(p == current);
114 1.17 riastrad if (state & TASK_UNINTERRUPTIBLE)
115 1.17 riastrad return false;
116 1.17 riastrad return sigispending(curlwp, 0);
117 1.17 riastrad }
118 1.17 riastrad
119 1.2 riastrad #endif /* _LINUX_SCHED_H_ */
120