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