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 * 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(cpu_stack_t **new_sp, cpu_stack_t **save_sp);
62 * Save context of old process and switch to new process.
64 INLINE void proc_switchTo(Process *next, Process *prev)
68 if (UNLIKELY(next == prev))
71 * If there is no old process, we save the old stack pointer into a
72 * dummy variable that we ignore. In fact, this happens only when the
73 * old process has just exited.
75 asm_switch_context(&next->stack, prev ? &prev->stack : &dummy);
79 * \name Flags for Process.flags.
82 #define PF_FREESTACK BV(0) /**< Free the stack when process dies */
86 /** Track running processes. */
87 extern REGISTER Process *current_process;
90 * Track ready processes.
92 * Access to this list must be performed with interrupts disabled
94 extern REGISTER List proc_ready_list;
97 #define prio_next() (LIST_EMPTY(&proc_ready_list) ? INT_MIN : \
98 ((PriNode *)LIST_HEAD(&proc_ready_list))->pri)
99 #define prio_proc(proc) (proc->link.pri)
100 #define prio_curr() prio_proc(current_process)
102 #define SCHED_ENQUEUE_INTERNAL(proc) \
103 LIST_ENQUEUE(&proc_ready_list, &(proc)->link)
104 #define SCHED_ENQUEUE_HEAD_INTERNAL(proc) \
105 LIST_ENQUEUE_HEAD(&proc_ready_list, &(proc)->link)
107 #define prio_next() 0
108 #define prio_proc(proc) 0
109 #define prio_curr() 0
111 #define SCHED_ENQUEUE_INTERNAL(proc) ADDTAIL(&proc_ready_list, &(proc)->link)
112 #define SCHED_ENQUEUE_HEAD_INTERNAL(proc) ADDHEAD(&proc_ready_list, &(proc)->link)
116 * Enqueue a process in the ready list.
118 * Always use this macro to instert a process in the ready list, as its
119 * might vary to implement a different scheduling algorithms.
121 * \note Access to the scheduler ready list must be performed with
122 * interrupts disabled.
124 #define SCHED_ENQUEUE(proc) do { \
125 IRQ_ASSERT_DISABLED(); \
126 LIST_ASSERT_VALID(&proc_ready_list); \
127 SCHED_ENQUEUE_INTERNAL(proc); \
130 #define SCHED_ENQUEUE_HEAD(proc) do { \
131 IRQ_ASSERT_DISABLED(); \
132 LIST_ASSERT_VALID(&proc_ready_list); \
133 SCHED_ENQUEUE_HEAD_INTERNAL(proc); \
139 * Changes the priority of an already enqueued process.
141 * Searches and removes the process from the ready list, then uses LIST_ENQUEUE(()
142 * to insert again to fix priority.
144 * No action is performed for processes that aren't in the ready list, eg. in semaphore queues.
146 * \note Performance could be improved with a different implementation of priority list.
148 INLINE void sched_reenqueue(struct Process *proc)
150 IRQ_ASSERT_DISABLED();
151 LIST_ASSERT_VALID(&proc_ready_list);
154 FOREACH_NODE(n, &proc_ready_list)
156 if (n == &proc->link.link)
163 // only remove and enqueue again if process is already in the ready list
164 // otherwise leave it alone
167 REMOVE(&proc->link.link);
168 LIST_ENQUEUE(&proc_ready_list, &proc->link);
171 #endif //CONFIG_KERN_PRI
173 /* Process trampoline */
174 void proc_entry(void);
176 /* Schedule another process *without* adding the current one to the ready list. */
177 void proc_switch(void);
179 /* Low level scheduling routine. */
180 void proc_schedule(void);
182 /* Low level context switch routine. */
183 void proc_switchTo(Process *next, Process *prev);
185 /* Initialize a scheduler class. */
186 void proc_schedInit(void);
188 #if CONFIG_KERN_MONITOR
189 /** Initialize the monitor */
190 void monitor_init(void);
192 /** Register a process into the monitor */
193 void monitor_add(Process *proc, const char *name);
195 /** Unregister a process from the monitor */
196 void monitor_remove(Process *proc);
198 /** Rename a process */
199 void monitor_rename(Process *proc, const char *name);
200 #endif /* CONFIG_KERN_MONITOR */
202 #endif /* KERN_PROC_P_H */