eb82043ae0d4c6f613e2d220f9fd6a5f8c2afaac
[bertos.git] / cfg / os.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief OS-specific definitions
9  *
10  * \version $Id$
11  *
12  * \author Bernardo Innocenti <bernie@develer.com>
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.9  2006/03/22 13:34:34  bernie
18  *#* MSVC support.
19  *#*
20  *#* Revision 1.8  2006/02/23 09:09:28  bernie
21  *#* Remove Linux specific hack.
22  *#*
23  *#* Revision 1.7  2006/02/20 01:46:59  bernie
24  *#* Port to MacOSX.
25  *#*
26  *#* Revision 1.6  2006/02/15 09:12:33  bernie
27  *#* Don't mask useful user signals on UNIX.
28  *#*
29  *#* Revision 1.5  2005/11/27 23:32:42  bernie
30  *#* Add CPU fallback for OS_ID.
31  *#*
32  *#* Revision 1.4  2005/11/27 03:07:13  bernie
33  *#* IRQ_SAVE_DISABLE(): Really block signals.
34  *#*
35  *#* Revision 1.3  2005/11/27 03:02:40  bernie
36  *#* Add POSIX emulation for IRQ_* macros; Add Qt support.
37  *#*
38  *#* Revision 1.2  2005/11/04 16:20:01  bernie
39  *#* Fix reference to README.devlib in header.
40  *#*
41  *#* Revision 1.1  2005/04/11 19:04:13  bernie
42  *#* Move top-level headers to cfg/ subdir.
43  *#*
44  *#* Revision 1.1  2004/12/31 17:40:24  bernie
45  *#* Add OS detection code.
46  *#*
47  *#*/
48 #ifndef DEVLIB_OS_H
49 #define DEVLIB_OS_H
50
51 /*! Macro to include OS-specific versions of the headers. */
52 #define OS_HEADER(module)  PP_STRINGIZE(PP_CAT3(module, _, OS_ID).h)
53 #define OS_CSOURCE(module) PP_STRINGIZE(PP_CAT3(module, _, OS_ID).c)
54
55 /*
56  * OS autodetection (Some systems trigger multiple OS definitions)
57  */
58 #ifdef _WIN32
59         #define OS_WIN32  1
60         #define OS_ID     win32
61
62         // FIXME: Maybe disable Win32 exceptions?
63         typedef int cpuflags_t;
64         #define IRQ_DISABLE                /* FIXME */
65         #define IRQ_ENABLE                 /* FIXME */
66         #define IRQ_SAVE_DISABLE(old_sigs) /* FIXME */
67         #define IRQ_RESTORE(old_sigs)      /* FIXME */
68
69 #else
70         #define OS_WIN32  0
71 #endif
72
73 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
74         #define OS_UNIX   1
75         #define OS_POSIX  1  /* Not strictly UNIX, but no way to autodetect it. */
76         #define OS_ID     posix
77
78         /*
79          * The POSIX moral equivalent of disabling IRQs is disabling signals.
80          */
81         #include <signal.h>
82         typedef sigset_t cpuflags_t;
83
84         #define SET_ALL_SIGNALS(sigs) \
85         do { \
86                 sigfillset(&sigs); \
87                 sigdelset(&sigs, SIGINT); \
88                 sigdelset(&sigs, SIGSTOP); \
89                 sigdelset(&sigs, SIGCONT); \
90         } while(0)
91
92         #define IRQ_DISABLE \
93         do { \
94                 sigset_t sigs; \
95                 SET_ALL_SIGNALS(sigs); \
96                 sigprocmask(SIG_BLOCK, &sigs, NULL); \
97         } while (0)
98
99         #define IRQ_ENABLE \
100         do { \
101                 sigset_t sigs; \
102                 SET_ALL_SIGNALS(sigs); \
103                 sigprocmask(SIG_UNBLOCK, &sigs, NULL); \
104         } while (0)
105
106         #define IRQ_SAVE_DISABLE(old_sigs) \
107         do { \
108                 sigset_t sigs; \
109                 SET_ALL_SIGNALS(sigs); \
110                 sigprocmask(SIG_BLOCK, &sigs, &old_sigs); \
111         } while (0)
112
113         #define IRQ_RESTORE(old_sigs) \
114         do { \
115                 sigprocmask(SIG_SETMASK, &old_sigs, NULL); \
116         } while (0)
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
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 */