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