b270d4069d52ab43540aa9e46d80947978d20d07
[bertos.git] / bertos / kern / proc.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, 2008 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \brief Bertos Kernel core (Process scheduler).
34  *
35  * \version $Id$
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  *
38  * $WIZ$ module_name = "kernel"
39  * $WIZ$ module_configuration = "bertos/cfg/cfg_proc.h"
40  */
41
42 #ifndef KERN_PROC_H
43 #define KERN_PROC_H
44
45 #include "cfg/cfg_proc.h"
46 #include "cfg/cfg_monitor.h"
47
48 #include <cfg/compiler.h>
49
50 #if CONFIG_KERN_PREEMPT
51         #include <cfg/debug.h> // ASSERT()
52 #endif
53
54 #include <cpu/types.h> // cpu_stack_t
55 #include <cpu/frame.h> // CPU_SAVED_REGS_CNT
56
57 /*
58  * Forward declaration. The definition of struct Process is private to the
59  * scheduler and hidden in proc_p.h.
60  */
61 struct Process;
62
63 void proc_init(void);
64 struct Process *proc_new_with_name(const char *name, void (*entry)(void), iptr_t data, size_t stacksize, cpu_stack_t *stack);
65
66 #if !CONFIG_KERN_MONITOR
67         #define proc_new(entry,data,size,stack) proc_new_with_name(NULL,(entry),(data),(size),(stack))
68 #else
69         #define proc_new(entry,data,size,stack) proc_new_with_name(#entry,(entry),(data),(size),(stack))
70 #endif
71
72 void proc_exit(void);
73 void proc_yield(void);
74 void proc_rename(struct Process *proc, const char *name);
75 const char *proc_name(struct Process *proc);
76 const char *proc_currentName(void);
77 iptr_t proc_currentUserData(void);
78
79 int proc_testSetup(void);
80 int proc_testRun(void);
81 int proc_testTearDown(void);
82
83 /**
84  * Return the context structure of the currently running process.
85  *
86  * The details of the Process structure are private to the scheduler.
87  * The address returned by this function is an opaque pointer that can
88  * be passed as an argument to other process-related functions.
89  */
90 INLINE struct Process *proc_current(void)
91 {
92         extern struct Process *CurrentProcess;
93         return CurrentProcess;
94 }
95
96 #if CONFIG_KERN_PRI
97         void proc_setPri(struct Process *proc, int pri);
98 #else
99         INLINE void proc_setPri(UNUSED_ARG(struct Process *,proc), UNUSED_ARG(int, pri))
100         {
101         }
102 #endif
103
104 /**
105  * Disable preemptive task switching.
106  *
107  * The scheduler maintains a global nesting counter.  Task switching is
108  * effectively re-enabled only when the number of calls to proc_permit()
109  * matches the number of calls to proc_forbid().
110  *
111  * \note Calling functions that could sleep while task switching is disabled
112  * is dangerous and unsupported.
113  *
114  * \note calling proc_forbid() from within an interrupt is illegal and
115  * meaningless.
116  *
117  * \note proc_permit() expands inline to 1-2 asm instructions, so it's a
118  * very efficient locking primitive in simple but performance-critical
119  * situations.  In all other cases, semaphores offer a more flexible and
120  * fine-grained locking primitive.
121  *
122  * \sa proc_permit()
123  */
124 INLINE void proc_forbid(void)
125 {
126         #if CONFIG_KERN_PREEMPT
127                 extern cpu_atomic_t _preempt_forbid_cnt;
128                 /*
129                  * We don't need to protect the counter against other processes.
130                  * The reason why is a bit subtle.
131                  *
132                  * If a process gets here, preempt_forbid_cnt can be either 0,
133                  * or != 0.  In the latter case, preemption is already disabled
134                  * and no concurrency issues can occur.
135                  *
136                  * In the former case, we could be preempted just after reading the
137                  * value 0 from memory, and a concurrent process might, in fact,
138                  * bump the value of preempt_forbid_cnt under our nose!
139                  *
140                  * BUT: if this ever happens, then we won't get another chance to
141                  * run until the other process calls proc_permit() to re-enable
142                  * preemption.  At this point, the value of preempt_forbid_cnt
143                  * must be back to 0, and thus what we had originally read from
144                  * memory happens to be valid.
145                  *
146                  * No matter how hard you think about it, and how complicated you
147                  * make your scenario, the above holds true as long as
148                  * "preempt_forbid_cnt != 0" means that no task switching is
149                  * possible.
150                  */
151                 ++_preempt_forbid_cnt;
152
153                 /*
154                  * Make sure _preempt_forbid_cnt is flushed to memory so the
155                  * preemption softirq will see the correct value from now on.
156                  */
157                 MEMORY_BARRIER;
158         #endif
159 }
160
161 /**
162  * Re-enable preemptive task switching.
163  *
164  * \sa proc_forbid()
165  */
166 INLINE void proc_permit(void)
167 {
168         #if CONFIG_KERN_PREEMPT
169
170                 /*
171                  * This is to ensure any global state changed by the process gets
172                  * flushed to memory before task switching is re-enabled.
173                  */
174                 MEMORY_BARRIER;
175                 extern cpu_atomic_t _preempt_forbid_cnt;
176                 /* No need to protect against interrupts here. */
177                 ASSERT(_preempt_forbid_cnt != 0);
178                 --_preempt_forbid_cnt;
179
180                 /*
181                  * This ensures _preempt_forbid_cnt is flushed to memory immediately
182                  * so the preemption interrupt sees the correct value.
183                  */
184                 MEMORY_BARRIER;
185
186         #endif
187 }
188
189 /**
190  * \return true if preemptive task switching is allowed.
191  * \note This accessor is needed because _preempt_forbid_cnt
192  *       must be absoultely private.
193  */
194 INLINE bool proc_allowed(void)
195 {
196         #if CONFIG_KERN_PREEMPT
197                 extern cpu_atomic_t _preempt_forbid_cnt;
198                 return (_preempt_forbid_cnt == 0);
199         #else
200                 return true;
201         #endif
202 }
203
204 /**
205  * Execute a block of \a CODE atomically with respect to task scheduling.
206  */
207 #define PROC_ATOMIC(CODE) \
208         do { \
209                 proc_forbid(); \
210                 CODE; \
211                 proc_permit(); \
212         } while(0)
213
214 #ifndef CONFIG_KERN_MINSTACKSIZE
215
216         #if (ARCH & ARCH_EMUL)
217                 /* We need a large stack because system libraries are bloated */
218                 #define CONFIG_KERN_MINSTACKSIZE  65536
219         #else
220                 /**
221                  * Default stack size for each thread, in bytes.
222                  *
223                  * The goal here is to allow a minimal task to save all of its
224                  * registers twice, plus push a maximum of 32 variables on the
225                  * stack.
226                  *
227                  * The actual size computed by the default formula is:
228                  *   AVR:    102
229                  *   i386:   156
230                  *   ARM:    164
231                  *   x86_64: 184
232                  *
233                  * Note that on most 16bit architectures, interrupts will also
234                  * run on the stack of the currently running process.  Nested
235                  * interrupts will greatly increases the amount of stack space
236                  * required per process.  Use irqmanager to minimize stack
237                  * usage.
238                  */
239                 #define CONFIG_KERN_MINSTACKSIZE  \
240                     (CPU_SAVED_REGS_CNT * 2 * sizeof(cpu_stack_t) \
241                     + 48 * sizeof(int))
242         #endif
243 #endif
244
245 /* Memory fill codes to help debugging */
246 #if CONFIG_KERN_MONITOR
247         #include <cpu/types.h>
248         #if (SIZEOF_CPUSTACK_T == 1)
249                 /* 8bit cpu_stack_t */
250                 #define CONFIG_KERN_STACKFILLCODE  0xA5
251                 #define CONFIG_KERN_MEMFILLCODE    0xDB
252         #elif (SIZEOF_CPUSTACK_T == 2)
253                 /* 16bit cpu_stack_t */
254                 #define CONFIG_KERN_STACKFILLCODE  0xA5A5
255                 #define CONFIG_KERN_MEMFILLCODE    0xDBDB
256         #elif (SIZEOF_CPUSTACK_T == 4)
257                 /* 32bit cpu_stack_t */
258                 #define CONFIG_KERN_STACKFILLCODE  0xA5A5A5A5UL
259                 #define CONFIG_KERN_MEMFILLCODE    0xDBDBDBDBUL
260         #elif (SIZEOF_CPUSTACK_T == 8)
261                 /* 64bit cpu_stack_t */
262                 #define CONFIG_KERN_STACKFILLCODE  0xA5A5A5A5A5A5A5A5ULL
263                 #define CONFIG_KERN_MEMFILLCODE    0xDBDBDBDBDBDBDBDBULL
264         #else
265                 #error No cpu_stack_t size supported!
266         #endif
267 #endif
268
269 #endif /* KERN_PROC_H */