kernel: preemptive and cooperative scheduler refactoring.
[bertos.git] / bertos / kern / proc_p.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2001, 2004 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999, 2000, 2001 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \brief Internal scheduler structures and definitions for processes.
35  *
36  * \version $Id$
37  * \author Bernie Innocenti <bernie@codewiz.org>
38  */
39
40 #ifndef KERN_PROC_P_H
41 #define KERN_PROC_P_H
42
43 #include "cfg/cfg_proc.h"
44 #include "cfg/cfg_monitor.h"
45
46 #include <cfg/compiler.h>
47
48 #include <cpu/types.h>        /* for cpu_stack_t */
49 #include <cpu/irq.h>          // IRQ_ASSERT_DISABLED()
50
51 #include <kern/proc.h>   // struct Process
52
53 #ifndef asm_switch_context
54 /**
55  * CPU dependent context switching routines.
56  *
57  * Saving and restoring the context on the stack is done by a CPU-dependent
58  * support routine which usually needs to be written in assembly.
59  */
60 EXTERN_C void asm_switch_context(cpu_stack_t **new_sp, cpu_stack_t **save_sp);
61 #endif
62
63 /**
64  * \name Flags for Process.flags.
65  * \{
66  */
67 #define PF_FREESTACK  BV(0)  /**< Free the stack when process dies */
68 /*\}*/
69
70
71 /** Track running processes. */
72 extern REGISTER Process *current_process;
73
74 /**
75  * Track ready processes.
76  *
77  * Access to this list must be performed with interrupts disabled
78  */
79 extern REGISTER List     proc_ready_list;
80
81 #if CONFIG_KERN_PRI
82         #define prio_next()     (LIST_EMPTY(&proc_ready_list) ? INT_MIN : \
83                                         ((PriNode *)LIST_HEAD(&proc_ready_list))->pri)
84         #define prio_proc(proc) (proc->link.pri)
85         #define prio_curr()     prio_proc(current_process)
86
87         #define SCHED_ENQUEUE_INTERNAL(proc) \
88                         LIST_ENQUEUE(&proc_ready_list, &(proc)->link)
89         #define SCHED_ENQUEUE_HEAD_INTERNAL(proc) \
90                         LIST_ENQUEUE_HEAD(&proc_ready_list, &(proc)->link)
91 #else
92         #define prio_next()     0
93         #define prio_proc(proc) 0
94         #define prio_curr()     0
95
96         #define SCHED_ENQUEUE_INTERNAL(proc) ADDTAIL(&proc_ready_list, &(proc)->link)
97         #define SCHED_ENQUEUE_HEAD_INTERNAL(proc) ADDHEAD(&proc_ready_list, &(proc)->link)
98 #endif
99
100 /**
101  * Enqueue a process in the ready list.
102  *
103  * Always use this macro to instert a process in the ready list, as its
104  * might vary to implement a different scheduling algorithms.
105  *
106  * \note Access to the scheduler ready list must be performed with
107  *       interrupts disabled.
108  */
109 #define SCHED_ENQUEUE(proc)  do { \
110                 IRQ_ASSERT_DISABLED(); \
111                 LIST_ASSERT_VALID(&proc_ready_list); \
112                 SCHED_ENQUEUE_INTERNAL(proc); \
113         } while (0)
114
115 #define SCHED_ENQUEUE_HEAD(proc)  do { \
116                 IRQ_ASSERT_DISABLED(); \
117                 LIST_ASSERT_VALID(&proc_ready_list); \
118                 SCHED_ENQUEUE_HEAD_INTERNAL(proc); \
119         } while (0)
120
121
122 #if CONFIG_KERN_PRI
123 /**
124  * Changes the priority of an already enqueued process.
125  *
126  * Searches and removes the process from the ready list, then uses LIST_ENQUEUE(()
127  * to insert again to fix priority.
128  *
129  * No action is performed for processes that aren't in the ready list, eg. in semaphore queues.
130  *
131  * \note Performance could be improved with a different implementation of priority list.
132  */
133 INLINE void sched_reenqueue(struct Process *proc)
134 {
135         IRQ_ASSERT_DISABLED();
136         LIST_ASSERT_VALID(&proc_ready_list);
137         Node *n;
138         PriNode *pos = NULL;
139         FOREACH_NODE(n, &proc_ready_list)
140         {
141                 if (n == &proc->link.link)
142                 {
143                         pos = (PriNode *)n;
144                         break;
145                 }
146         }
147
148         // only remove and enqueue again if process is already in the ready list
149         // otherwise leave it alone
150         if (pos)
151         {
152                 REMOVE(&proc->link.link);
153                 LIST_ENQUEUE(&proc_ready_list, &proc->link);
154         }
155 }
156 #endif //CONFIG_KERN_PRI
157
158 /* Process trampoline */
159 void proc_entry(void);
160
161 /* Schedule another process *without* adding the current one to the ready list. */
162 void proc_switch(void);
163
164 /* Immediately schedule a particular process bypassing the scheduler. */
165 void proc_wakeup(Process *proc);
166
167 /* Initialize a scheduler class. */
168 void proc_schedInit(void);
169
170 #if CONFIG_KERN_MONITOR
171         /** Initialize the monitor */
172         void monitor_init(void);
173
174         /** Register a process into the monitor */
175         void monitor_add(Process *proc, const char *name);
176
177         /** Unregister a process from the monitor */
178         void monitor_remove(Process *proc);
179
180         /** Rename a process */
181         void monitor_rename(Process *proc, const char *name);
182 #endif /* CONFIG_KERN_MONITOR */
183
184 #if CONFIG_KERN_PREEMPT
185 INLINE int preempt_quantum(void)
186 {
187         extern int _proc_quantum;
188         return _proc_quantum;
189 }
190
191 INLINE void proc_decQuantum(void)
192 {
193         extern int _proc_quantum;
194         if (_proc_quantum > 0)
195                 _proc_quantum--;
196 }
197
198 INLINE void preempt_reset_quantum(void)
199 {
200         extern int _proc_quantum;
201         _proc_quantum = CONFIG_KERN_QUANTUM;
202 }
203 #else /* !CONFIG_KERN_PREEMPT */
204 INLINE int preempt_quantum(void)
205 {
206         return 0;
207 }
208
209 INLINE void proc_decQuantum(void)
210 {
211 }
212
213 INLINE void preempt_reset_quantum(void)
214 {
215 }
216 #endif /* CONFIG_KERN_PREEMPT */
217
218 #endif /* KERN_PROC_P_H */