4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2001, 2004, 2008 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 1999, 2000, 2001, 2008 Bernie Innocenti <bernie@codewiz.org>
33 * \brief Simple cooperative multitasking scheduler.
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>
43 // Log settings for cfg/log.h.
44 #define LOG_LEVEL KERN_LOG_LEVEL
45 #define LOG_FORMAT KERN_LOG_FORMAT
49 #include <cpu/types.h>
51 #include <cpu/frame.h>
54 * CPU dependent context switching routines.
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.
59 EXTERN_C void asm_switch_context(cpustack_t **new_sp, cpustack_t **save_sp);
63 * System scheduler: pass CPU control to the next process in
66 static void proc_schedule(void)
70 ATOMIC(LIST_ASSERT_VALID(&ProcReadyList));
71 ASSERT_USER_CONTEXT();
74 /* Poll on the ready queue for the first ready process */
75 IRQ_SAVE_DISABLE(flags);
76 while (!(CurrentProcess = (struct Process *)list_remHead(&ProcReadyList)))
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.
84 * During idle-spinning, an interrupt can occur and it may
85 * modify \p ProcReadyList. 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
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
101 void proc_switch(void)
103 /* Remember old process to save its context later */
104 const Process *old_process = CurrentProcess;
109 * Optimization: don't switch contexts when the active
110 * process has not changed.
112 if (CurrentProcess != old_process)
116 #if CONFIG_KERN_MONITOR
117 LOG_INFO("Switch from %p(%s) to %p(%s)\n",
118 old_process, proc_name(old_process),
119 CurrentProcess, proc_currentName());
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
126 * TODO: Instead of physically clearing the process at exit time, a zombie
127 * list should be created.
129 asm_switch_context(&CurrentProcess->stack, old_process ? &old_process->stack : &dummy);
132 /* This RET resumes the execution on the new process */
137 * Co-operative context switch
139 void proc_yield(void)
141 ATOMIC(SCHED_ENQUEUE(CurrentProcess));