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.
36 * \author Bernie Innocenti <bernie@codewiz.org>
42 #include "cfg/cfg_proc.h"
43 #include "cfg/cfg_monitor.h"
45 #include <cfg/compiler.h>
47 #include <cpu/types.h> /* for cpu_stack_t */
48 #include <cpu/irq.h> // IRQ_ASSERT_DISABLED()
50 #include <kern/proc.h> // struct Process
52 #ifndef asm_switch_context
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);
63 * \name Flags for Process.flags.
66 #define PF_FREESTACK BV(0) /**< Free the stack when process dies */
70 /** Track running processes. */
71 extern REGISTER Process *current_process;
74 * Track ready processes.
76 * Access to this list must be performed with interrupts disabled
78 extern REGISTER List proc_ready_list;
81 #define prio_next() (LIST_EMPTY(&proc_ready_list) ? INT_MIN : \
82 ((PriNode *)LIST_HEAD(&proc_ready_list))->pri)
83 #define prio_proc(proc) (proc->link.pri)
84 #define prio_curr() prio_proc(current_process)
86 #define SCHED_ENQUEUE_INTERNAL(proc) \
87 LIST_ENQUEUE(&proc_ready_list, &(proc)->link)
88 #define SCHED_ENQUEUE_HEAD_INTERNAL(proc) \
89 LIST_ENQUEUE_HEAD(&proc_ready_list, &(proc)->link)
92 #define prio_proc(proc) 0
95 #define SCHED_ENQUEUE_INTERNAL(proc) ADDTAIL(&proc_ready_list, &(proc)->link)
96 #define SCHED_ENQUEUE_HEAD_INTERNAL(proc) ADDHEAD(&proc_ready_list, &(proc)->link)
100 * Enqueue a process in the ready list.
102 * Always use this macro to instert a process in the ready list, as its
103 * might vary to implement a different scheduling algorithms.
105 * \note Access to the scheduler ready list must be performed with
106 * interrupts disabled.
108 #define SCHED_ENQUEUE(proc) do { \
109 IRQ_ASSERT_DISABLED(); \
110 LIST_ASSERT_VALID(&proc_ready_list); \
111 SCHED_ENQUEUE_INTERNAL(proc); \
114 #define SCHED_ENQUEUE_HEAD(proc) do { \
115 IRQ_ASSERT_DISABLED(); \
116 LIST_ASSERT_VALID(&proc_ready_list); \
117 SCHED_ENQUEUE_HEAD_INTERNAL(proc); \
123 * Changes the priority of an already enqueued process.
125 * Searches and removes the process from the ready list, then uses LIST_ENQUEUE(()
126 * to insert again to fix priority.
128 * No action is performed for processes that aren't in the ready list, eg. in semaphore queues.
130 * \note Performance could be improved with a different implementation of priority list.
132 INLINE void sched_reenqueue(struct Process *proc)
134 IRQ_ASSERT_DISABLED();
135 LIST_ASSERT_VALID(&proc_ready_list);
138 FOREACH_NODE(n, &proc_ready_list)
140 if (n == &proc->link.link)
147 // only remove and enqueue again if process is already in the ready list
148 // otherwise leave it alone
151 REMOVE(&proc->link.link);
152 LIST_ENQUEUE(&proc_ready_list, &proc->link);
155 #endif //CONFIG_KERN_PRI
157 /* Process trampoline */
158 void proc_entry(void);
160 /* Schedule another process *without* adding the current one to the ready list. */
161 void proc_switch(void);
163 /* Immediately schedule a particular process bypassing the scheduler. */
164 void proc_wakeup(Process *proc);
166 /* Initialize a scheduler class. */
167 void proc_schedInit(void);
169 #if CONFIG_KERN_MONITOR
170 /** Initialize the monitor */
171 void monitor_init(void);
173 /** Register a process into the monitor */
174 void monitor_add(Process *proc, const char *name);
176 /** Unregister a process from the monitor */
177 void monitor_remove(Process *proc);
179 /** Rename a process */
180 void monitor_rename(Process *proc, const char *name);
181 #endif /* CONFIG_KERN_MONITOR */
184 * Quantum related macros are used in the
185 * timer module and must be empty when
186 * kernel is disabled.
188 #if (CONFIG_KERN && CONFIG_KERN_PREEMPT)
189 INLINE int preempt_quantum(void)
191 extern int _proc_quantum;
192 return _proc_quantum;
195 INLINE void proc_decQuantum(void)
197 extern int _proc_quantum;
198 if (_proc_quantum > 0)
202 INLINE void preempt_reset_quantum(void)
204 extern int _proc_quantum;
205 _proc_quantum = CONFIG_KERN_QUANTUM;
207 #else /* !(CONFIG_KERN && CONFIG_KERN_PREEMPT) */
208 INLINE int preempt_quantum(void)
213 INLINE void proc_decQuantum(void)
217 INLINE void preempt_reset_quantum(void)
220 #endif /* (CONFIG_KERN && CONFIG_KERN_PREEMPT) */
222 #endif /* KERN_PROC_P_H */