Refactor to use new protocol module and sipo.
[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  * \author Bernie Innocenti <bernie@codewiz.org>
37  */
38
39 #ifndef KERN_PROC_P_H
40 #define KERN_PROC_P_H
41
42 #include "cfg/cfg_proc.h"
43 #include "cfg/cfg_monitor.h"
44
45 #include <cfg/compiler.h>
46
47 #include <cpu/types.h>        /* for cpu_stack_t */
48 #include <cpu/irq.h>          // IRQ_ASSERT_DISABLED()
49
50 #include <kern/proc.h>   // struct Process
51
52 #ifndef asm_switch_context
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 #endif
61
62 /**
63  * \name Flags for Process.flags.
64  * \{
65  */
66 #define PF_FREESTACK  BV(0)  /**< Free the stack when process dies */
67 /*\}*/
68
69
70 /** Track running processes. */
71 extern REGISTER Process *current_process;
72
73 /**
74  * Track ready processes.
75  *
76  * Access to this list must be performed with interrupts disabled
77  */
78 extern REGISTER List     proc_ready_list;
79
80 #if CONFIG_KERN_PRI
81 # if CONFIG_KERN_PRI_INHERIT
82         #define __prio_orig(proc) (proc->orig_pri)
83         #define __prio_inh(proc) (LIST_EMPTY(&(proc)->inh_list) ? INT_MIN : \
84                                         ((PriNode *)LIST_HEAD(&proc->inh_list))->pri)
85         #define __prio_proc(proc) (__prio_inh(proc) > __prio_orig(proc) ? \
86                                         __prio_inh(proc) : __prio_orig(proc))
87 # endif
88         #define prio_next()     (LIST_EMPTY(&proc_ready_list) ? INT_MIN : \
89                                         ((PriNode *)LIST_HEAD(&proc_ready_list))->pri)
90         #define prio_proc(proc) (proc->link.pri)
91         #define prio_curr()     prio_proc(current_process)
92
93         #define SCHED_ENQUEUE_INTERNAL(proc) \
94                         LIST_ENQUEUE(&proc_ready_list, &(proc)->link)
95         #define SCHED_ENQUEUE_HEAD_INTERNAL(proc) \
96                         LIST_ENQUEUE_HEAD(&proc_ready_list, &(proc)->link)
97 #else
98         #define prio_next()     0
99         #define prio_proc(proc) 0
100         #define prio_curr()     0
101
102         #define SCHED_ENQUEUE_INTERNAL(proc) ADDTAIL(&proc_ready_list, &(proc)->link)
103         #define SCHED_ENQUEUE_HEAD_INTERNAL(proc) ADDHEAD(&proc_ready_list, &(proc)->link)
104 #endif
105
106 /**
107  * Enqueue a process in the ready list.
108  *
109  * Always use this macro to instert a process in the ready list, as its
110  * might vary to implement a different scheduling algorithms.
111  *
112  * \note Access to the scheduler ready list must be performed with
113  *       interrupts disabled.
114  */
115 #define SCHED_ENQUEUE(proc)  do { \
116                 IRQ_ASSERT_DISABLED(); \
117                 LIST_ASSERT_VALID(&proc_ready_list); \
118                 SCHED_ENQUEUE_INTERNAL(proc); \
119         } while (0)
120
121 #define SCHED_ENQUEUE_HEAD(proc)  do { \
122                 IRQ_ASSERT_DISABLED(); \
123                 LIST_ASSERT_VALID(&proc_ready_list); \
124                 SCHED_ENQUEUE_HEAD_INTERNAL(proc); \
125         } while (0)
126
127
128 #if CONFIG_KERN_PRI
129 /**
130  * Changes the priority of an already enqueued process.
131  *
132  * Searches and removes the process from the ready list, then uses LIST_ENQUEUE(()
133  * to insert again to fix priority.
134  *
135  * No action is performed for processes that aren't in the ready list, eg. in semaphore queues.
136  *
137  * \note Performance could be improved with a different implementation of priority list.
138  */
139 INLINE void sched_reenqueue(struct Process *proc)
140 {
141         IRQ_ASSERT_DISABLED();
142         LIST_ASSERT_VALID(&proc_ready_list);
143         Node *n;
144         PriNode *pos = NULL;
145         FOREACH_NODE(n, &proc_ready_list)
146         {
147                 if (n == &proc->link.link)
148                 {
149                         pos = (PriNode *)n;
150                         break;
151                 }
152         }
153
154         // only remove and enqueue again if process is already in the ready list
155         // otherwise leave it alone
156         if (pos)
157         {
158                 REMOVE(&proc->link.link);
159                 LIST_ENQUEUE(&proc_ready_list, &proc->link);
160         }
161 }
162 #endif //CONFIG_KERN_PRI
163
164 /* Process trampoline */
165 void proc_entry(void);
166
167 /* Schedule another process *without* adding the current one to the ready list. */
168 void proc_switch(void);
169
170 /* Immediately schedule a particular process bypassing the scheduler. */
171 void proc_wakeup(Process *proc);
172
173 /* Initialize a scheduler class. */
174 void proc_schedInit(void);
175
176 #if CONFIG_KERN_MONITOR
177         /** Initialize the monitor */
178         void monitor_init(void);
179
180         /** Register a process into the monitor */
181         void monitor_add(Process *proc, const char *name);
182
183         /** Unregister a process from the monitor */
184         void monitor_remove(Process *proc);
185
186         /** Rename a process */
187         void monitor_rename(Process *proc, const char *name);
188 #endif /* CONFIG_KERN_MONITOR */
189
190 /*
191  * Quantum related macros are used in the
192  * timer module and must be empty when
193  * kernel is disabled.
194  */
195 #if (CONFIG_KERN && CONFIG_KERN_PREEMPT)
196 INLINE int preempt_quantum(void)
197 {
198         extern int _proc_quantum;
199         return _proc_quantum;
200 }
201
202 INLINE void proc_decQuantum(void)
203 {
204         extern int _proc_quantum;
205         if (_proc_quantum > 0)
206                 _proc_quantum--;
207 }
208
209 INLINE void preempt_reset_quantum(void)
210 {
211         extern int _proc_quantum;
212         _proc_quantum = CONFIG_KERN_QUANTUM;
213 }
214 #else /* !(CONFIG_KERN && CONFIG_KERN_PREEMPT) */
215 INLINE int preempt_quantum(void)
216 {
217         return 0;
218 }
219
220 INLINE void proc_decQuantum(void)
221 {
222 }
223
224 INLINE void preempt_reset_quantum(void)
225 {
226 }
227 #endif /* (CONFIG_KERN && CONFIG_KERN_PREEMPT) */
228
229 #endif /* KERN_PROC_P_H */