Merge branch "preempt" in "trunk".
[bertos.git] / bertos / cfg / os.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 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2008 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \brief OS-specific definitions
34  *
35  * \version $Id$
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  */
38
39 #ifndef CFG_OS_H
40 #define CFG_OS_H
41
42 #include "cfg/cfg_proc.h"
43
44 /*
45  * OS autodetection (Some systems trigger multiple OS definitions)
46  */
47 #ifdef _WIN32
48         #define OS_WIN32  1
49         #define OS_ID     win32
50
51         // FIXME: Maybe disable Win32 exceptions?
52         typedef int cpu_flags_t;
53         #define IRQ_DISABLE                FIXME
54         #define IRQ_ENABLE                 FIXME
55         #define IRQ_SAVE_DISABLE(old_sigs) FIXME
56         #define IRQ_RESTORE(old_sigs)      FIXME
57
58 #else
59         #define OS_WIN32  0
60 #endif
61
62 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
63         #define OS_UNIX   1
64         #define OS_POSIX  1  /* Not strictly UNIX, but no way to autodetect it. */
65         #define OS_ID     posix
66
67         /*
68          * The POSIX moral equivalent of disabling IRQs is disabling signals.
69          */
70         #include <signal.h>
71         typedef sigset_t cpu_flags_t;
72
73         #define SET_ALL_SIGNALS(sigs) \
74         do { \
75                 sigfillset(&sigs); \
76                 sigdelset(&sigs, SIGINT); \
77                 sigdelset(&sigs, SIGSTOP); \
78                 sigdelset(&sigs, SIGCONT); \
79         } while(0)
80
81         #define IRQ_DISABLE \
82         do { \
83                 sigset_t sigs; \
84                 SET_ALL_SIGNALS(sigs); \
85                 sigprocmask(SIG_BLOCK, &sigs, NULL); \
86         } while (0)
87
88         #define IRQ_ENABLE \
89         do { \
90                 sigset_t sigs; \
91                 SET_ALL_SIGNALS(sigs); \
92                 sigprocmask(SIG_UNBLOCK, &sigs, NULL); \
93         } while (0)
94
95         #define IRQ_SAVE_DISABLE(old_sigs) \
96         do { \
97                 sigset_t sigs; \
98                 SET_ALL_SIGNALS(sigs); \
99                 sigprocmask(SIG_BLOCK, &sigs, &old_sigs); \
100         } while (0)
101
102         #define IRQ_RESTORE(old_sigs) \
103         do { \
104                 sigprocmask(SIG_SETMASK, &old_sigs, NULL); \
105         } while (0)
106
107         #define IRQ_ENABLED() \
108         ({ \
109                 sigset_t sigs__; \
110                 sigprocmask(SIG_SETMASK, NULL, &sigs__); \
111                 sigismember(&sigs__, SIGALRM) ? false : true; \
112          })
113
114         #if CONFIG_KERN_PREEMPT
115                 #define DECLARE_ISR_CONTEXT_SWITCH(vect)        \
116                         void vect(UNUSED_ARG(int, arg));        \
117                         INLINE void __isr_##vect(void);         \
118                         void vect(UNUSED_ARG(int, arg))         \
119                         {                                       \
120                                 __isr_##vect();                 \
121                                 IRQ_PREEMPT_HANDLER();          \
122                         }                                       \
123                         INLINE void __isr_##vect(void)
124                 /**
125                  * With task priorities enabled each ISR is used a point to
126                  * check if we need to perform a context switch.
127                  *
128                  * Instead, without priorities a context switch can occur only
129                  * when the running task expires its time quantum. In this last
130                  * case, the context switch can only occur in the timer ISR,
131                  * that must be always declared with the
132                  * DECLARE_ISR_CONTEXT_SWITCH() macro.
133                  */
134                 #if CONFIG_KERN_PRI
135                         #define DECLARE_ISR(vect) \
136                                 DECLARE_ISR_CONTEXT_SWITCH(vect)
137                 #endif /* CONFIG_KERN_PRI */
138         #endif
139         #ifndef DECLARE_ISR
140                 #define DECLARE_ISR(vect) \
141                                 void vect(UNUSED_ARG(int, arg))
142         #endif
143         #ifndef DECLARE_ISR_CONTEXT_SWITCH
144                 #define DECLARE_ISR_CONTEXT_SWITCH(vect) \
145                                 void vect(UNUSED_ARG(int, arg))
146         #endif
147
148 #else
149         #define OS_UNIX   0
150         #define OS_POSIX  0
151 #endif
152
153 #ifdef __linux__
154         #define OS_LINUX  1
155 #else
156         #define OS_LINUX  0
157 #endif
158
159 #if defined(__APPLE__) && defined(__MACH__)
160         #define OS_DARWIN 1
161 #else
162         #define OS_DARWIN 0
163 #endif
164
165
166 #include "cfg/cfg_arch.h" /* For ARCH_QT */
167
168 /*
169  * We want Qt and other frameworks to look like OSes because you would
170  * tipically want their portable abstractions if you're using one of these.
171  */
172 #if defined(_QT) || (defined(ARCH_QT) && (ARCH & ARCH_QT))
173         #define OS_QT 1
174         #undef  OS_ID
175         #define OS_ID qt
176 #else
177         #define OS_QT 0
178 #endif
179
180 /*
181  * Summarize hosted environments as OS_HOSTED and embedded
182  * environment with OS_EMBEDDED.
183  */
184 #if OS_WIN32 || OS_UNIX || OS_DARWIN || OS_QT
185         #define OS_HOSTED   1
186         #define OS_EMBEDDED 0
187 #else
188         #define OS_HOSTED   0
189         #define OS_EMBEDDED 1
190
191         /* Embedded environments fall back to CPU-specific code. */
192         #define OS_ID       CPU_ID
193 #endif
194
195 /* Self-check for the detection */
196 #if !defined(OS_ID)
197         #error OS_ID not defined
198 #endif
199 #if OS_HOSTED && OS_EMBEDDED
200         #error Both hosted and embedded OS environment
201 #endif
202 #if !OS_HOSTED && !OS_EMBEDDED
203         #error Neither hosted nor embedded OS environment
204 #endif
205
206 #if OS_HOSTED
207
208         /// Macro to include OS-specific headers.
209         #define OS_HEADER(module)  PP_STRINGIZE(emul/PP_CAT3(module, _, OS_ID).h)
210
211         /// Macro to include OS-specific source files.
212         #define OS_CSOURCE(module) PP_STRINGIZE(emul/PP_CAT3(module, _, OS_ID).c)
213
214 #else
215         // Fallbacks for embedded systems
216         #define OS_HEADER(module)  CPU_HEADER(module)
217         #define OS_CSOURCE(module) CPU_CSOURCE(module)
218 #endif
219
220 #endif /* CFG_OS_H */