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