Parametric scheduler approach.
[bertos.git] / bertos / kern / coop.c
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, 2008 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999, 2000, 2001, 2008 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \brief Simple cooperative multitasking scheduler.
34  *
35  * \version $Id$
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  * \author Stefano Fedrigo <aleph@develer.com>
38  */
39
40 #include "proc_p.h"
41 #include "proc.h"
42
43 // Log settings for cfg/log.h.
44 #define LOG_LEVEL   KERN_LOG_LEVEL
45 #define LOG_FORMAT  KERN_LOG_FORMAT
46 #include <cfg/log.h>
47
48 #include <cpu/irq.h>
49 #include <cpu/types.h>
50 #include <cpu/attr.h>
51 #include <cpu/frame.h>
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 /**
63  * System scheduler: pass CPU control to the next process in
64  * the ready queue.
65  */
66 static void coop_schedule(void)
67 {
68         cpu_flags_t flags;
69
70         ATOMIC(LIST_ASSERT_VALID(&proc_ready_list));
71         ASSERT_USER_CONTEXT();
72         IRQ_ASSERT_ENABLED();
73
74         /* Poll on the ready queue for the first ready process */
75         IRQ_SAVE_DISABLE(flags);
76         while (!(current_process = (struct Process *)list_remHead(&proc_ready_list)))
77         {
78                 /*
79                  * Make sure we physically reenable interrupts here, no matter what
80                  * the current task status is. This is important because if we
81                  * are idle-spinning, we must allow interrupts, otherwise no
82                  * process will ever wake up.
83                  *
84                  * During idle-spinning, an interrupt can occur and it may
85                  * modify \p proc_ready_list. To ensure that compiler reload this
86                  * variable every while cycle we call CPU_MEMORY_BARRIER.
87                  * The memory barrier ensure that all variables used in this context
88                  * are reloaded.
89                  * \todo If there was a way to write sig_wait() so that it does not
90                  * disable interrupts while waiting, there would not be any
91                  * reason to do this.
92                  */
93                 IRQ_ENABLE;
94                 CPU_IDLE;
95                 MEMORY_BARRIER;
96                 IRQ_DISABLE;
97         }
98         IRQ_RESTORE(flags);
99 }
100
101 void coop_switch(void)
102 {
103         /* Remember old process to save its context later */
104         Process * const old_process = current_process;
105
106         coop_schedule();
107
108         /*
109          * Optimization: don't switch contexts when the active
110          * process has not changed.
111          */
112         if (current_process != old_process)
113         {
114                 cpu_stack_t *dummy;
115
116                 #if CONFIG_KERN_MONITOR
117                         LOG_INFO("Switch from %p(%s) to %p(%s)\n",
118                                 old_process,    proc_name(old_process),
119                                 current_process, proc_currentName());
120                 #endif
121
122                 /* Save context of old process and switch to new process. If there is no
123                  * old process, we save the old stack pointer into a dummy variable that
124                  * we ignore. In fact, this happens only when the old process has just
125                  * exited.
126                  * TODO: Instead of physically clearing the process at exit time, a zombie
127                  * list should be created.
128                  */
129                 asm_switch_context(&current_process->stack, old_process ? &old_process->stack : &dummy);
130         }
131
132         /* This RET resumes the execution on the new process */
133 }
134
135 /**
136  * Co-operative context switch
137  */
138 void coop_yield(void)
139 {
140         ATOMIC(SCHED_ENQUEUE(current_process));
141         proc_switch();
142 }