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