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 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 1999, 2000, 2001, 2008 Bernie Innocenti <bernie@codewiz.org>
33 * \brief Simple realtime multitasking scheduler.
34 * Context switching is only done cooperatively.
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>
44 #include <cpu/frame.h> // CPU_IDLE
46 #include <unistd.h> // XXX alarm()
50 * The time sharing scheduler forces a task switch when the current
51 * process has exhausted its quantum.
56 * Disable preemptive task switching.
58 * The scheduler maintains a per-process nesting counter. Task switching is
59 * effectively re-enabled only when the number of calls to proc_permit()
60 * matches the number of calls to proc_forbid().
62 * Calling functions that could sleep while task switching is disabled
63 * is dangerous, although supported. Preemptive task switching is
64 * resumed while the process is sleeping and disabled again as soon as
69 void proc_forbid(void)
71 /* No need to protect against interrupts here. */
72 ++CurrentProcess->forbid_cnt;
76 * Re-enable preemptive task switching.
80 void proc_permit(void)
82 /* No need to protect against interrupts here. */
83 --CurrentProcess->forbid_cnt;
86 static void (*irq_handlers[100])(void); // FIXME
89 void proc_preempt(void)
93 ATOMIC(LIST_ASSERT_VALID(&ProcReadyList));
97 /* Poll on the ready queue for the first ready process */
98 while (!(CurrentProcess = (struct Process *)list_remHead(&ProcReadyList)))
102 * Make sure we physically reenable interrupts here, no matter what
103 * the current task status is. This is important because if we
104 * are idle-spinning, we must allow interrupts, otherwise no
105 * process will ever wake up.
107 * During idle-spinning, an interrupt can occur and it may
108 * modify \p ProcReadyList. To ensure that compiler reload this
109 * variable every while cycle we call CPU_MEMORY_BARRIER.
110 * The memory barrier ensure that all variables used in this context
114 //FIXME: calls Qt stuff from sighandler! CPU_IDLE;
123 void proc_preempt_timer(void)
125 // TODO: check Quantum
131 TRACEMSG("preempting %p:%s", CurrentProcess, CurrentProcess->monitor.name);
132 ATOMIC(SCHED_ENQUEUE(CurrentProcess));
137 void proc_schedule(void)
141 // Will invoke proc_preempt() in interrupt context
145 void proc_yield(void)
147 ATOMIC(SCHED_ENQUEUE(CurrentProcess));
152 void proc_entry(void (*user_entry)(void))
159 void irq_entry(int signum)
161 Process *old_process;
163 // TRACEMSG("storing %p:%s", CurrentProcess, CurrentProcess->monitor.name);
164 // CurrentProcess->leaving = false;
165 // getcontext(&CurrentProcess->context);
166 /* We get here in two ways: directly, and after setcontext() below */
168 // if (CurrentProcess->leaving)
170 // TRACEMSG("leaving to %p:%s", CurrentProcess, CurrentProcess->monitor.name);
174 old_process = CurrentProcess;
176 irq_handlers[signum]();
178 if (old_process != CurrentProcess)
180 TRACEMSG("switching from %p:%s to %p:%s",
181 old_process, old_process->monitor.name,
182 CurrentProcess, CurrentProcess->monitor.name);
183 swapcontext(&old_process->context, &CurrentProcess->context);
184 // TRACEMSG("launching %p:%s", CurrentProcess, CurrentProcess->monitor.name);
185 // CurrentProcess->leaving = true;
186 // setcontext(&CurrentProcess->context);
190 TRACEMSG("keeping %p:%s", CurrentProcess, CurrentProcess->monitor.name);
193 void preempt_init(void)
195 struct sigaction act;
196 act.sa_handler = irq_entry;
197 sigemptyset(&act.sa_mask);
198 sigaddset(&act.sa_mask, SIGUSR1);
199 sigaddset(&act.sa_mask, SIGALRM);
200 act.sa_flags = SA_RESTART; /* | SA_SIGINFO; */
202 irq_handlers[SIGUSR1] = proc_preempt;
203 irq_handlers[SIGALRM] = proc_preempt_timer;
204 sigaction(SIGUSR1, &act, NULL);
205 sigaction(SIGALRM, &act, NULL);