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