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
55 * \name Flags for Process.flags.
58 #define PF_FREESTACK BV(0) /**< Free the stack when process dies */
62 /** Track running processes. */
63 extern REGISTER Process *current_process;
66 * Track ready processes.
68 * Access to this list must be performed with interrupts disabled
70 extern REGISTER List proc_ready_list;
73 #define prio_next() (LIST_EMPTY(&proc_ready_list) ? INT_MIN : \
74 ((PriNode *)LIST_HEAD(&proc_ready_list))->pri)
75 #define prio_curr() (current_process->link.pri)
77 #define SCHED_ENQUEUE_INTERNAL(proc) \
78 LIST_ENQUEUE(&proc_ready_list, &(proc)->link)
79 #define SCHED_ENQUEUE_HEAD_INTERNAL(proc) \
80 LIST_ENQUEUE_HEAD(&proc_ready_list, &(proc)->link)
85 #define SCHED_ENQUEUE_INTERNAL(proc) ADDTAIL(&proc_ready_list, &(proc)->link)
86 #define SCHED_ENQUEUE_HEAD_INTERNAL(proc) ADDHEAD(&proc_ready_list, &(proc)->link)
90 * Enqueue a process in the ready list.
92 * Always use this macro to instert a process in the ready list, as its
93 * might vary to implement a different scheduling algorithms.
95 * \note Access to the scheduler ready list must be performed with
96 * interrupts disabled.
98 #define SCHED_ENQUEUE(proc) do { \
99 IRQ_ASSERT_DISABLED(); \
100 LIST_ASSERT_VALID(&proc_ready_list); \
101 SCHED_ENQUEUE_INTERNAL(proc); \
104 #define SCHED_ENQUEUE_HEAD(proc) do { \
105 IRQ_ASSERT_DISABLED(); \
106 LIST_ASSERT_VALID(&proc_ready_list); \
107 SCHED_ENQUEUE_HEAD_INTERNAL(proc); \
113 * Changes the priority of an already enqueued process.
115 * Searches and removes the process from the ready list, then uses LIST_ENQUEUE(()
116 * to insert again to fix priority.
118 * No action is performed for processes that aren't in the ready list, eg. in semaphore queues.
120 * \note Performance could be improved with a different implementation of priority list.
122 INLINE void sched_reenqueue(struct Process *proc)
124 IRQ_ASSERT_DISABLED();
125 LIST_ASSERT_VALID(&proc_ready_list);
128 FOREACH_NODE(n, &proc_ready_list)
130 if (n == &proc->link.link)
137 // only remove and enqueue again if process is already in the ready list
138 // otherwise leave it alone
141 REMOVE(&proc->link.link);
142 LIST_ENQUEUE(&proc_ready_list, &proc->link);
145 #endif //CONFIG_KERN_PRI
147 /* Process trampoline */
148 void proc_entry(void);
150 /* Schedule another process *without* adding the current one to the ready list. */
151 void proc_switch(void);
153 /* Low level scheduling routine. */
154 void proc_schedule(void);
156 /* Initialize a scheduler class. */
157 void proc_schedInit(void);
159 #if CONFIG_KERN_MONITOR
160 /** Initialize the monitor */
161 void monitor_init(void);
163 /** Register a process into the monitor */
164 void monitor_add(Process *proc, const char *name);
166 /** Unregister a process from the monitor */
167 void monitor_remove(Process *proc);
169 /** Rename a process */
170 void monitor_rename(Process *proc, const char *name);
171 #endif /* CONFIG_KERN_MONITOR */
173 #endif /* KERN_PROC_P_H */