4630377e9eae19774e99197689bd8d81d77ea7ba
[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 /*
43  * OS autodetection (Some systems trigger multiple OS definitions)
44  */
45 #ifdef _WIN32
46         #define OS_WIN32  1
47         #define OS_ID     win32
48
49         // FIXME: Maybe disable Win32 exceptions?
50         typedef int cpu_flags_t;
51         #define IRQ_DISABLE                FIXME
52         #define IRQ_ENABLE                 FIXME
53         #define IRQ_SAVE_DISABLE(old_sigs) FIXME
54         #define IRQ_RESTORE(old_sigs)      FIXME
55
56 #else
57         #define OS_WIN32  0
58 #endif
59
60 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
61         #define OS_UNIX   1
62         #define OS_POSIX  1  /* Not strictly UNIX, but no way to autodetect it. */
63         #define OS_ID     posix
64
65         /*
66          * The POSIX moral equivalent of disabling IRQs is disabling signals.
67          */
68         #include <signal.h>
69         typedef sigset_t cpu_flags_t;
70
71         #define SET_ALL_SIGNALS(sigs) \
72         do { \
73                 sigfillset(&sigs); \
74                 sigdelset(&sigs, SIGINT); \
75                 sigdelset(&sigs, SIGSTOP); \
76                 sigdelset(&sigs, SIGCONT); \
77         } while(0)
78
79         #define IRQ_DISABLE \
80         do { \
81                 sigset_t sigs; \
82                 SET_ALL_SIGNALS(sigs); \
83                 sigprocmask(SIG_BLOCK, &sigs, NULL); \
84         } while (0)
85
86         #define IRQ_ENABLE \
87         do { \
88                 sigset_t sigs; \
89                 SET_ALL_SIGNALS(sigs); \
90                 sigprocmask(SIG_UNBLOCK, &sigs, NULL); \
91         } while (0)
92
93         #define IRQ_SAVE_DISABLE(old_sigs) \
94         do { \
95                 sigset_t sigs; \
96                 SET_ALL_SIGNALS(sigs); \
97                 sigprocmask(SIG_BLOCK, &sigs, &old_sigs); \
98         } while (0)
99
100         #define IRQ_RESTORE(old_sigs) \
101         do { \
102                 sigprocmask(SIG_SETMASK, &old_sigs, NULL); \
103         } while (0)
104
105         #define IRQ_ENABLED() \
106         ({ \
107                 sigset_t sigs__; \
108                 sigprocmask(SIG_SETMASK, NULL, &sigs__); \
109                 sigismember(&sigs__, SIGALRM) ? false : true; \
110          })
111
112 #else
113         #define OS_UNIX   0
114         #define OS_POSIX  0
115 #endif
116
117 #ifdef __linux__
118         #define OS_LINUX  1
119 #else
120         #define OS_LINUX  0
121 #endif
122
123 #if defined(__APPLE__) && defined(__MACH__)
124         #define OS_DARWIN 1
125 #else
126         #define OS_DARWIN 0
127 #endif
128
129
130 #include "cfg/cfg_arch.h" /* For ARCH_QT */
131
132 /*
133  * We want Qt and other frameworks to look like OSes because you would
134  * tipically want their portable abstractions if you're using one of these.
135  */
136 #if defined(_QT) || (defined(ARCH_QT) && (ARCH & ARCH_QT))
137         #define OS_QT 1
138         #undef  OS_ID
139         #define OS_ID qt
140 #else
141         #define OS_QT 0
142 #endif
143
144 /*
145  * Summarize hosted environments as OS_HOSTED and embedded
146  * environment with OS_EMBEDDED.
147  */
148 #if OS_WIN32 || OS_UNIX || OS_DARWIN || OS_QT
149         #define OS_HOSTED   1
150         #define OS_EMBEDDED 0
151 #else
152         #define OS_HOSTED   0
153         #define OS_EMBEDDED 1
154
155         /* Embedded environments fall back to CPU-specific code. */
156         #define OS_ID       CPU_ID
157 #endif
158
159 /* Self-check for the detection */
160 #if !defined(OS_ID)
161         #error OS_ID not defined
162 #endif
163 #if OS_HOSTED && OS_EMBEDDED
164         #error Both hosted and embedded OS environment
165 #endif
166 #if !OS_HOSTED && !OS_EMBEDDED
167         #error Neither hosted nor embedded OS environment
168 #endif
169
170 #if OS_HOSTED
171
172         /// Macro to include OS-specific headers.
173         #define OS_HEADER(module)  PP_STRINGIZE(emul/PP_CAT3(module, _, OS_ID).h)
174
175         /// Macro to include OS-specific source files.
176         #define OS_CSOURCE(module) PP_STRINGIZE(emul/PP_CAT3(module, _, OS_ID).c)
177
178 #else
179         // Fallbacks for embedded systems
180         #define OS_HEADER(module)  CPU_HEADER(module)
181         #define OS_CSOURCE(module) CPU_CSOURCE(module)
182 #endif
183
184 #endif /* CFG_OS_H */