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 Bernie Innocenti <bernie@codewiz.org>
34 * \brief Internal scheduler structures and definitions for processes.
37 * \author Bernie Innocenti <bernie@codewiz.org>
43 #include "cfg/cfg_proc.h"
44 #include "cfg/cfg_monitor.h"
46 #include <cfg/compiler.h>
48 #include <cpu/types.h> /* for cpu_stack_t */
49 #include <cpu/irq.h> // IRQ_ASSERT_DISABLED()
51 #include <kern/proc.h> // struct Process
54 * Check if the process context switch can be performed directly by the
55 * architecture-dependent asm_switch_context() or if it must be delayed
56 * because we're in the middle of an ISR.
58 * Return true if asm_switch_context() can be executed, false
61 * NOTE: if an architecture does not implement IRQ_RUNNING() this function
62 * always returns true.
64 #define CONTEXT_SWITCH_FROM_ISR() (!IRQ_RUNNING())
66 #ifndef asm_switch_context
68 * CPU dependent context switching routines.
70 * Saving and restoring the context on the stack is done by a CPU-dependent
71 * support routine which usually needs to be written in assembly.
73 EXTERN_C void asm_switch_context(cpu_stack_t **new_sp, cpu_stack_t **save_sp);
77 * Save context of old process and switch to new process.
79 INLINE void proc_switchTo(Process *next, Process *prev)
83 if (UNLIKELY(next == prev))
86 * If there is no old process, we save the old stack pointer into a
87 * dummy variable that we ignore. In fact, this happens only when the
88 * old process has just exited.
90 asm_switch_context(&next->stack, prev ? &prev->stack : &dummy);
94 * \name Flags for Process.flags.
97 #define PF_FREESTACK BV(0) /**< Free the stack when process dies */
101 /** Track running processes. */
102 extern REGISTER Process *current_process;
105 * Track ready processes.
107 * Access to this list must be performed with interrupts disabled
109 extern REGISTER List proc_ready_list;
112 #define prio_next() (LIST_EMPTY(&proc_ready_list) ? INT_MIN : \
113 ((PriNode *)LIST_HEAD(&proc_ready_list))->pri)
114 #define prio_proc(proc) (proc->link.pri)
115 #define prio_curr() prio_proc(current_process)
117 #define SCHED_ENQUEUE_INTERNAL(proc) \
118 LIST_ENQUEUE(&proc_ready_list, &(proc)->link)
119 #define SCHED_ENQUEUE_HEAD_INTERNAL(proc) \
120 LIST_ENQUEUE_HEAD(&proc_ready_list, &(proc)->link)
122 #define prio_next() 0
123 #define prio_proc(proc) 0
124 #define prio_curr() 0
126 #define SCHED_ENQUEUE_INTERNAL(proc) ADDTAIL(&proc_ready_list, &(proc)->link)
127 #define SCHED_ENQUEUE_HEAD_INTERNAL(proc) ADDHEAD(&proc_ready_list, &(proc)->link)
131 * Enqueue a process in the ready list.
133 * Always use this macro to instert a process in the ready list, as its
134 * might vary to implement a different scheduling algorithms.
136 * \note Access to the scheduler ready list must be performed with
137 * interrupts disabled.
139 #define SCHED_ENQUEUE(proc) do { \
140 IRQ_ASSERT_DISABLED(); \
141 LIST_ASSERT_VALID(&proc_ready_list); \
142 SCHED_ENQUEUE_INTERNAL(proc); \
145 #define SCHED_ENQUEUE_HEAD(proc) do { \
146 IRQ_ASSERT_DISABLED(); \
147 LIST_ASSERT_VALID(&proc_ready_list); \
148 SCHED_ENQUEUE_HEAD_INTERNAL(proc); \
154 * Changes the priority of an already enqueued process.
156 * Searches and removes the process from the ready list, then uses LIST_ENQUEUE(()
157 * to insert again to fix priority.
159 * No action is performed for processes that aren't in the ready list, eg. in semaphore queues.
161 * \note Performance could be improved with a different implementation of priority list.
163 INLINE void sched_reenqueue(struct Process *proc)
165 IRQ_ASSERT_DISABLED();
166 LIST_ASSERT_VALID(&proc_ready_list);
169 FOREACH_NODE(n, &proc_ready_list)
171 if (n == &proc->link.link)
178 // only remove and enqueue again if process is already in the ready list
179 // otherwise leave it alone
182 REMOVE(&proc->link.link);
183 LIST_ENQUEUE(&proc_ready_list, &proc->link);
186 #endif //CONFIG_KERN_PRI
188 /* Process trampoline */
189 void proc_entry(void);
191 /* Schedule another process *without* adding the current one to the ready list. */
192 void proc_switch(void);
194 /* Low level scheduling routine. */
195 void proc_schedule(void);
197 /* Low level context switch routine. */
198 void proc_switchTo(Process *next, Process *prev);
200 /* Initialize a scheduler class. */
201 void proc_schedInit(void);
203 #if CONFIG_KERN_MONITOR
204 /** Initialize the monitor */
205 void monitor_init(void);
207 /** Register a process into the monitor */
208 void monitor_add(Process *proc, const char *name);
210 /** Unregister a process from the monitor */
211 void monitor_remove(Process *proc);
213 /** Rename a process */
214 void monitor_rename(Process *proc, const char *name);
215 #endif /* CONFIG_KERN_MONITOR */
217 #endif /* KERN_PROC_P_H */