c874ca36f8bdaac539f3078c4b70efc5fe6e79c5
[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  * Define function prototypes exported outside.
63  *
64  * Required to silent gcc "no previous prototype" warnings.
65  */
66 void coop_yield(void);
67 void coop_switch(void);
68
69 /**
70  * System scheduler: pass CPU control to the next process in
71  * the ready queue.
72  */
73 static void coop_schedule(void)
74 {
75         cpu_flags_t flags;
76
77         ATOMIC(LIST_ASSERT_VALID(&proc_ready_list));
78         ASSERT_USER_CONTEXT();
79         IRQ_ASSERT_ENABLED();
80
81         /* Poll on the ready queue for the first ready process */
82         IRQ_SAVE_DISABLE(flags);
83         while (!(current_process = (struct Process *)list_remHead(&proc_ready_list)))
84         {
85                 /*
86                  * Make sure we physically reenable interrupts here, no matter what
87                  * the current task status is. This is important because if we
88                  * are idle-spinning, we must allow interrupts, otherwise no
89                  * process will ever wake up.
90                  *
91                  * During idle-spinning, an interrupt can occur and it may
92                  * modify \p proc_ready_list. To ensure that compiler reload this
93                  * variable every while cycle we call CPU_MEMORY_BARRIER.
94                  * The memory barrier ensure that all variables used in this context
95                  * are reloaded.
96                  * \todo If there was a way to write sig_wait() so that it does not
97                  * disable interrupts while waiting, there would not be any
98                  * reason to do this.
99                  */
100                 IRQ_ENABLE;
101                 CPU_IDLE;
102                 MEMORY_BARRIER;
103                 IRQ_DISABLE;
104         }
105         IRQ_RESTORE(flags);
106 }
107
108 void coop_switch(void)
109 {
110         /* Remember old process to save its context later */
111         Process * const old_process = current_process;
112
113         coop_schedule();
114
115         /*
116          * Optimization: don't switch contexts when the active
117          * process has not changed.
118          */
119         if (current_process != old_process)
120         {
121                 cpu_stack_t *dummy;
122
123                 #if CONFIG_KERN_MONITOR
124                         LOG_INFO("Switch from %p(%s) to %p(%s)\n",
125                                 old_process,    proc_name(old_process),
126                                 current_process, proc_currentName());
127                 #endif
128
129                 /* Save context of old process and switch to new process. If there is no
130                  * old process, we save the old stack pointer into a dummy variable that
131                  * we ignore. In fact, this happens only when the old process has just
132                  * exited.
133                  * TODO: Instead of physically clearing the process at exit time, a zombie
134                  * list should be created.
135                  */
136                 asm_switch_context(&current_process->stack, old_process ? &old_process->stack : &dummy);
137         }
138
139         /* This RET resumes the execution on the new process */
140 }
141
142 /**
143  * Co-operative context switch
144  */
145 void coop_yield(void)
146 {
147         ATOMIC(SCHED_ENQUEUE(current_process));
148         proc_switch();
149 }