Rename SCHED_CHANGE_PRI to better describe functionality.
[bertos.git] / bertos / kern / proc_p.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2001, 2004 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999, 2000, 2001 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \brief Internal scheduler structures and definitions for processes.
35  *
36  * \version $Id$
37  * \author Bernie Innocenti <bernie@codewiz.org>
38  */
39
40 #ifndef KERN_PROC_P_H
41 #define KERN_PROC_P_H
42
43 #include "cfg/cfg_proc.h"
44 #include "cfg/cfg_signal.h"
45 #include "cfg/cfg_monitor.h"
46
47 #include <cfg/compiler.h>
48
49 #include <cpu/types.h>        /* for cpu_stack_t */
50 #include <cpu/irq.h>          // IRQ_ASSERT_DISABLED()
51
52 #include <struct/list.h>
53
54 #if CONFIG_KERN_PREEMPT
55         #include <ucontext.h> // XXX
56 #endif
57
58 typedef struct Process
59 {
60 #if CONFIG_KERN_PRI
61         PriNode      link;        /**< Link Process into scheduler lists */
62 #else
63         Node         link;        /**< Link Process into scheduler lists */
64 #endif
65         cpu_stack_t  *stack;       /**< Per-process SP */
66         iptr_t       user_data;   /**< Custom data passed to the process */
67
68 #if CONFIG_KERN_SIGNALS
69         sigmask_t    sig_wait;    /**< Signals the process is waiting for */
70         sigmask_t    sig_recv;    /**< Received signals */
71 #endif
72
73 #if CONFIG_KERN_HEAP
74         uint16_t     flags;       /**< Flags */
75 #endif
76
77 #if CONFIG_KERN_HEAP | CONFIG_KERN_MONITOR | (ARCH & ARCH_EMUL)
78         cpu_stack_t  *stack_base;  /**< Base of process stack */
79         size_t       stack_size;  /**< Size of process stack */
80 #endif
81
82 #if CONFIG_KERN_PREEMPT
83         ucontext_t   context;
84 #endif
85
86 #if CONFIG_KERN_MONITOR
87         struct ProcMonitor
88         {
89                 Node        link;
90                 const char *name;
91         } monitor;
92 #endif
93
94 } Process;
95
96
97 /**
98  * \name Flags for Process.flags.
99  * \{
100  */
101 #define PF_FREESTACK  BV(0)  /**< Free the stack when process dies */
102 /*\}*/
103
104
105 /** Track running processes. */
106 extern REGISTER Process *CurrentProcess;
107
108 /**
109  * Track ready processes.
110  *
111  * Access to this list must be performed with interrupts disabled
112  */
113 extern REGISTER List     ProcReadyList;
114
115 #if CONFIG_KERN_PRI
116         #define SCHED_ENQUEUE_INTERNAL(proc) LIST_ENQUEUE(&ProcReadyList, &(proc)->link)
117 #else
118         #define SCHED_ENQUEUE_INTERNAL(proc) ADDTAIL(&ProcReadyList, &(proc)->link)
119 #endif
120
121 /**
122  * Enqueue a process in the ready list.
123  *
124  * Always use this macro to instert a process in the ready list, as its
125  * might vary to implement a different scheduling algorithms.
126  *
127  * \note Access to the scheduler ready list must be performed with
128  *       interrupts disabled.
129  */
130 #define SCHED_ENQUEUE(proc)  do { \
131                 IRQ_ASSERT_DISABLED(); \
132                 LIST_ASSERT_VALID(&ProcReadyList); \
133                 SCHED_ENQUEUE_INTERNAL(proc); \
134         } while (0)
135
136 #if CONFIG_KERN_PRI
137 /**
138  * Changes the priority of an already enqueued process.
139  *
140  * Searches and removes the process from the ready list, then uses LIST_ENQUEUE(()
141  * to insert again to fix priority.
142  *
143  * No action is performed for processes that aren't in the ready list, eg. in semaphore queues.
144  *
145  * \note Performance could be improved with a different implementation of priority list.
146  */
147 INLINE void sched_reenqueue(struct Process *proc)
148 {
149         IRQ_ASSERT_DISABLED();
150         LIST_ASSERT_VALID(&ProcReadyList);
151         Node *n;
152         PriNode *pos = NULL;
153         FOREACH_NODE(n, &ProcReadyList)
154         {
155                 if (n == &proc->link.link)
156                 {
157                         pos = (PriNode *)n;
158                         break;
159                 }
160         }
161
162         // only remove and enqueue again if process is already in the ready list
163         // otherwise leave it alone
164         if (pos)
165         {
166                 REMOVE(&proc->link.link);
167                 LIST_ENQUEUE(&ProcReadyList, &proc->link);
168         }
169 }
170 #endif //CONFIG_KERN_PRI
171
172 /// Schedule another process *without* adding the current one to the ready list.
173 void proc_switch(void);
174
175 #if CONFIG_KERN_PREEMPT
176 void proc_entry(void (*user_entry)(void));
177 void preempt_init(void);
178 #endif
179
180 #if CONFIG_KERN_MONITOR
181         /** Initialize the monitor */
182         void monitor_init(void);
183
184         /** Register a process into the monitor */
185         void monitor_add(Process *proc, const char *name);
186
187         /** Unregister a process from the monitor */
188         void monitor_remove(Process *proc);
189
190         /** Rename a process */
191         void monitor_rename(Process *proc, const char *name);
192 #endif /* CONFIG_KERN_MONITOR */
193
194 #endif /* KERN_PROC_P_H */