a5c7dbe3120ef6ff134039ecd4adca68c63e52f0
[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_monitor.h"
45
46 #include <cfg/compiler.h>
47
48 #include <cpu/types.h>        /* for cpu_stack_t */
49 #include <cpu/irq.h>          // IRQ_ASSERT_DISABLED()
50
51 #if CONFIG_KERN_PREEMPT
52         #include <ucontext.h> // XXX
53 #endif
54
55 #include <kern/proc.h>   // struct Process
56
57
58 /**
59  * \name Flags for Process.flags.
60  * \{
61  */
62 #define PF_FREESTACK  BV(0)  /**< Free the stack when process dies */
63 /*\}*/
64
65
66 /** Track running processes. */
67 extern REGISTER Process *CurrentProcess;
68
69 /**
70  * Track ready processes.
71  *
72  * Access to this list must be performed with interrupts disabled
73  */
74 extern REGISTER List     ProcReadyList;
75
76 #if CONFIG_KERN_PRI
77         #define SCHED_ENQUEUE_INTERNAL(proc) LIST_ENQUEUE(&ProcReadyList, &(proc)->link)
78 #else
79         #define SCHED_ENQUEUE_INTERNAL(proc) ADDTAIL(&ProcReadyList, &(proc)->link)
80 #endif
81
82 /**
83  * Enqueue a process in the ready list.
84  *
85  * Always use this macro to instert a process in the ready list, as its
86  * might vary to implement a different scheduling algorithms.
87  *
88  * \note Access to the scheduler ready list must be performed with
89  *       interrupts disabled.
90  */
91 #define SCHED_ENQUEUE(proc)  do { \
92                 IRQ_ASSERT_DISABLED(); \
93                 LIST_ASSERT_VALID(&ProcReadyList); \
94                 SCHED_ENQUEUE_INTERNAL(proc); \
95         } while (0)
96
97 #if CONFIG_KERN_PRI
98 /**
99  * Changes the priority of an already enqueued process.
100  *
101  * Searches and removes the process from the ready list, then uses LIST_ENQUEUE(()
102  * to insert again to fix priority.
103  *
104  * No action is performed for processes that aren't in the ready list, eg. in semaphore queues.
105  *
106  * \note Performance could be improved with a different implementation of priority list.
107  */
108 INLINE void sched_reenqueue(struct Process *proc)
109 {
110         IRQ_ASSERT_DISABLED();
111         LIST_ASSERT_VALID(&ProcReadyList);
112         Node *n;
113         PriNode *pos = NULL;
114         FOREACH_NODE(n, &ProcReadyList)
115         {
116                 if (n == &proc->link.link)
117                 {
118                         pos = (PriNode *)n;
119                         break;
120                 }
121         }
122
123         // only remove and enqueue again if process is already in the ready list
124         // otherwise leave it alone
125         if (pos)
126         {
127                 REMOVE(&proc->link.link);
128                 LIST_ENQUEUE(&ProcReadyList, &proc->link);
129         }
130 }
131 #endif //CONFIG_KERN_PRI
132
133 /// Schedule another process *without* adding the current one to the ready list.
134 void proc_switch(void);
135
136 #if CONFIG_KERN_PREEMPT
137 void proc_entry(void (*user_entry)(void));
138 void preempt_init(void);
139 #endif
140
141 #if CONFIG_KERN_MONITOR
142         /** Initialize the monitor */
143         void monitor_init(void);
144
145         /** Register a process into the monitor */
146         void monitor_add(Process *proc, const char *name);
147
148         /** Unregister a process from the monitor */
149         void monitor_remove(Process *proc);
150
151         /** Rename a process */
152         void monitor_rename(Process *proc, const char *name);
153 #endif /* CONFIG_KERN_MONITOR */
154
155 #endif /* KERN_PROC_P_H */