56c810819b56964d2d16c83fe29075fbde8f3e5b
[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 /*
54  * Check if the process context switch can be performed directly by the
55  * architecture-dependent asm_switch_context() or if it must be delayed
56  * because we're in the middle of an ISR.
57  *
58  * Return true if asm_switch_context() can be executed, false
59  * otherwise.
60  *
61  * NOTE: if an architecture does not implement IRQ_RUNNING() this function
62  * always returns true.
63  */
64 #define CONTEXT_SWITCH_FROM_ISR()       (!IRQ_RUNNING())
65
66 #ifndef asm_switch_context
67 /**
68  * CPU dependent context switching routines.
69  *
70  * Saving and restoring the context on the stack is done by a CPU-dependent
71  * support routine which usually needs to be written in assembly.
72  */
73 EXTERN_C void asm_switch_context(cpu_stack_t **new_sp, cpu_stack_t **save_sp);
74 #endif
75
76 /*
77  * Save context of old process and switch to new process.
78   */
79 INLINE void proc_switchTo(Process *next, Process *prev)
80 {
81         cpu_stack_t *dummy;
82
83         if (UNLIKELY(next == prev))
84                 return;
85         /*
86          * If there is no old process, we save the old stack pointer into a
87          * dummy variable that we ignore.  In fact, this happens only when the
88          * old process has just exited.
89          */
90         asm_switch_context(&next->stack, prev ? &prev->stack : &dummy);
91 }
92
93 /**
94  * \name Flags for Process.flags.
95  * \{
96  */
97 #define PF_FREESTACK  BV(0)  /**< Free the stack when process dies */
98 /*\}*/
99
100
101 /** Track running processes. */
102 extern REGISTER Process *current_process;
103
104 /**
105  * Track ready processes.
106  *
107  * Access to this list must be performed with interrupts disabled
108  */
109 extern REGISTER List     proc_ready_list;
110
111 #if CONFIG_KERN_PRI
112         #define prio_next()     (LIST_EMPTY(&proc_ready_list) ? INT_MIN : \
113                                         ((PriNode *)LIST_HEAD(&proc_ready_list))->pri)
114         #define prio_proc(proc) (proc->link.pri)
115         #define prio_curr()     prio_proc(current_process)
116
117         #define SCHED_ENQUEUE_INTERNAL(proc) \
118                         LIST_ENQUEUE(&proc_ready_list, &(proc)->link)
119         #define SCHED_ENQUEUE_HEAD_INTERNAL(proc) \
120                         LIST_ENQUEUE_HEAD(&proc_ready_list, &(proc)->link)
121 #else
122         #define prio_next()     0
123         #define prio_proc(proc) 0
124         #define prio_curr()     0
125
126         #define SCHED_ENQUEUE_INTERNAL(proc) ADDTAIL(&proc_ready_list, &(proc)->link)
127         #define SCHED_ENQUEUE_HEAD_INTERNAL(proc) ADDHEAD(&proc_ready_list, &(proc)->link)
128 #endif
129
130 /**
131  * Enqueue a process in the ready list.
132  *
133  * Always use this macro to instert a process in the ready list, as its
134  * might vary to implement a different scheduling algorithms.
135  *
136  * \note Access to the scheduler ready list must be performed with
137  *       interrupts disabled.
138  */
139 #define SCHED_ENQUEUE(proc)  do { \
140                 IRQ_ASSERT_DISABLED(); \
141                 LIST_ASSERT_VALID(&proc_ready_list); \
142                 SCHED_ENQUEUE_INTERNAL(proc); \
143         } while (0)
144
145 #define SCHED_ENQUEUE_HEAD(proc)  do { \
146                 IRQ_ASSERT_DISABLED(); \
147                 LIST_ASSERT_VALID(&proc_ready_list); \
148                 SCHED_ENQUEUE_HEAD_INTERNAL(proc); \
149         } while (0)
150
151
152 #if CONFIG_KERN_PRI
153 /**
154  * Changes the priority of an already enqueued process.
155  *
156  * Searches and removes the process from the ready list, then uses LIST_ENQUEUE(()
157  * to insert again to fix priority.
158  *
159  * No action is performed for processes that aren't in the ready list, eg. in semaphore queues.
160  *
161  * \note Performance could be improved with a different implementation of priority list.
162  */
163 INLINE void sched_reenqueue(struct Process *proc)
164 {
165         IRQ_ASSERT_DISABLED();
166         LIST_ASSERT_VALID(&proc_ready_list);
167         Node *n;
168         PriNode *pos = NULL;
169         FOREACH_NODE(n, &proc_ready_list)
170         {
171                 if (n == &proc->link.link)
172                 {
173                         pos = (PriNode *)n;
174                         break;
175                 }
176         }
177
178         // only remove and enqueue again if process is already in the ready list
179         // otherwise leave it alone
180         if (pos)
181         {
182                 REMOVE(&proc->link.link);
183                 LIST_ENQUEUE(&proc_ready_list, &proc->link);
184         }
185 }
186 #endif //CONFIG_KERN_PRI
187
188 /* Process trampoline */
189 void proc_entry(void);
190
191 /* Schedule another process *without* adding the current one to the ready list. */
192 void proc_switch(void);
193
194 /* Low level scheduling routine. */
195 void proc_schedule(void);
196
197 /* Low level context switch routine. */
198 void proc_switchTo(Process *next, Process *prev);
199
200 /* Initialize a scheduler class. */
201 void proc_schedInit(void);
202
203 #if CONFIG_KERN_MONITOR
204         /** Initialize the monitor */
205         void monitor_init(void);
206
207         /** Register a process into the monitor */
208         void monitor_add(Process *proc, const char *name);
209
210         /** Unregister a process from the monitor */
211         void monitor_remove(Process *proc);
212
213         /** Rename a process */
214         void monitor_rename(Process *proc, const char *name);
215 #endif /* CONFIG_KERN_MONITOR */
216
217 #endif /* KERN_PROC_P_H */