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