Removed 'This file is part of DevLib ...'
[bertos.git] / 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  *
31  * -->
32  *
33  * \brief OS-specific definitions
34  *
35  * \version $Id$
36  *
37  * \author Bernardo Innocenti <bernie@develer.com>
38  */
39
40 /*#*
41  *#* $Log$
42  *#* Revision 1.11  2007/09/29 16:19:47  bernie
43  *#* Changes to compile with sparse.
44  *#*
45  *#* Revision 1.10  2006/07/19 12:56:25  bernie
46  *#* Convert to new Doxygen style.
47  *#*
48  *#* Revision 1.9  2006/03/22 13:34:34  bernie
49  *#* MSVC support.
50  *#*
51  *#* Revision 1.8  2006/02/23 09:09:28  bernie
52  *#* Remove Linux specific hack.
53  *#*
54  *#* Revision 1.7  2006/02/20 01:46:59  bernie
55  *#* Port to MacOSX.
56  *#*
57  *#* Revision 1.6  2006/02/15 09:12:33  bernie
58  *#* Don't mask useful user signals on UNIX.
59  *#*
60  *#* Revision 1.5  2005/11/27 23:32:42  bernie
61  *#* Add CPU fallback for OS_ID.
62  *#*
63  *#* Revision 1.4  2005/11/27 03:07:13  bernie
64  *#* IRQ_SAVE_DISABLE(): Really block signals.
65  *#*
66  *#* Revision 1.3  2005/11/27 03:02:40  bernie
67  *#* Add POSIX emulation for IRQ_* macros; Add Qt support.
68  *#*
69  *#* Revision 1.2  2005/11/04 16:20:01  bernie
70  *#* Fix reference to README.devlib in header.
71  *#*
72  *#* Revision 1.1  2005/04/11 19:04:13  bernie
73  *#* Move top-level headers to cfg/ subdir.
74  *#*
75  *#* Revision 1.1  2004/12/31 17:40:24  bernie
76  *#* Add OS detection code.
77  *#*
78  *#*/
79 #ifndef DEVLIB_OS_H
80 #define DEVLIB_OS_H
81
82 /** Macro to include OS-specific versions of the headers. */
83 #define OS_HEADER(module)  PP_STRINGIZE(PP_CAT3(module, _, OS_ID).h)
84 #define OS_CSOURCE(module) PP_STRINGIZE(PP_CAT3(module, _, OS_ID).c)
85
86 /*
87  * OS autodetection (Some systems trigger multiple OS definitions)
88  */
89 #ifdef _WIN32
90         #define OS_WIN32  1
91         #define OS_ID     win32
92
93         // FIXME: Maybe disable Win32 exceptions?
94         typedef int cpuflags_t;
95         #define IRQ_DISABLE                /* FIXME */
96         #define IRQ_ENABLE                 /* FIXME */
97         #define IRQ_SAVE_DISABLE(old_sigs) /* FIXME */
98         #define IRQ_RESTORE(old_sigs)      /* FIXME */
99
100 #else
101         #define OS_WIN32  0
102 #endif
103
104 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
105         #define OS_UNIX   1
106         #define OS_POSIX  1  /* Not strictly UNIX, but no way to autodetect it. */
107         #define OS_ID     posix
108
109         /*
110          * The POSIX moral equivalent of disabling IRQs is disabling signals.
111          */
112         #include <signal.h>
113         typedef sigset_t cpuflags_t;
114
115         #define SET_ALL_SIGNALS(sigs) \
116         do { \
117                 sigfillset(&sigs); \
118                 sigdelset(&sigs, SIGINT); \
119                 sigdelset(&sigs, SIGSTOP); \
120                 sigdelset(&sigs, SIGCONT); \
121         } while(0)
122
123         #define IRQ_DISABLE \
124         do { \
125                 sigset_t sigs; \
126                 SET_ALL_SIGNALS(sigs); \
127                 sigprocmask(SIG_BLOCK, &sigs, NULL); \
128         } while (0)
129
130         #define IRQ_ENABLE \
131         do { \
132                 sigset_t sigs; \
133                 SET_ALL_SIGNALS(sigs); \
134                 sigprocmask(SIG_UNBLOCK, &sigs, NULL); \
135         } while (0)
136
137         #define IRQ_SAVE_DISABLE(old_sigs) \
138         do { \
139                 sigset_t sigs; \
140                 SET_ALL_SIGNALS(sigs); \
141                 sigprocmask(SIG_BLOCK, &sigs, &old_sigs); \
142         } while (0)
143
144         #define IRQ_RESTORE(old_sigs) \
145         do { \
146                 sigprocmask(SIG_SETMASK, &old_sigs, NULL); \
147         } while (0)
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  * We want Qt and other frameworks to look like OSes because you would
167  * tipically want their portable abstractions if you're using one of these.
168  */
169 #if defined(_QT)
170         #define OS_QT 1
171         #undef  OS_ID
172         #define OS_ID qt
173 #else
174         #define OS_QT 0
175 #endif
176
177 /*
178  * Summarize hosted environments as OS_HOSTED and embedded
179  * environment with OS_EMBEDDED.
180  */
181 #if OS_WIN32 || OS_UNIX || OS_DARWIN || OS_QT
182         #define OS_HOSTED   1
183         #define OS_EMBEDDED 0
184 #else
185         #define OS_HOSTED   0
186         #define OS_EMBEDDED 1
187
188         /* Embedded environments fall back to CPU-specific code. */
189         #define OS_ID       CPU_ID
190 #endif
191
192 /* Self-check for the detection */
193 #if !defined(OS_ID)
194         #error OS_ID not defined
195 #endif
196 #if OS_HOSTED && OS_EMBEDDED
197         #error Both hosted and embedded OS environment
198 #endif
199 #if !OS_HOSTED && !OS_EMBEDDED
200         #error Neither hosted nor embedded OS environment
201 #endif
202
203 #endif /* DEVLIB_OS_H */