Remove Linux specific hack.
[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.8  2006/02/23 09:09:28  bernie
18  *#* Remove Linux specific hack.
19  *#*
20  *#* Revision 1.7  2006/02/20 01:46:59  bernie
21  *#* Port to MacOSX.
22  *#*
23  *#* Revision 1.6  2006/02/15 09:12:33  bernie
24  *#* Don't mask useful user signals on UNIX.
25  *#*
26  *#* Revision 1.5  2005/11/27 23:32:42  bernie
27  *#* Add CPU fallback for OS_ID.
28  *#*
29  *#* Revision 1.4  2005/11/27 03:07:13  bernie
30  *#* IRQ_SAVE_DISABLE(): Really block signals.
31  *#*
32  *#* Revision 1.3  2005/11/27 03:02:40  bernie
33  *#* Add POSIX emulation for IRQ_* macros; Add Qt support.
34  *#*
35  *#* Revision 1.2  2005/11/04 16:20:01  bernie
36  *#* Fix reference to README.devlib in header.
37  *#*
38  *#* Revision 1.1  2005/04/11 19:04:13  bernie
39  *#* Move top-level headers to cfg/ subdir.
40  *#*
41  *#* Revision 1.1  2004/12/31 17:40:24  bernie
42  *#* Add OS detection code.
43  *#*
44  *#*/
45 #ifndef DEVLIB_OS_H
46 #define DEVLIB_OS_H
47
48 /*! Macro to include OS-specific versions of the headers. */
49 #define OS_HEADER(module)  PP_STRINGIZE(PP_CAT3(module, _, OS_ID).h)
50 #define OS_CSOURCE(module) PP_STRINGIZE(PP_CAT3(module, _, OS_ID).c)
51
52 /*
53  * OS autodetection (Some systems trigger multiple OS definitions)
54  */
55 #ifdef _WIN32
56         #define OS_WIN32  1
57         #define OS_ID     win32
58 #else
59         #define OS_WIN32  0
60 #endif
61
62 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
63         #define OS_UNIX   1
64         #define OS_POSIX  1  /* Not strictly UNIX, but no way to autodetect it. */
65         #define OS_ID     posix
66
67         /*
68          * The POSIX moral equivalent of disabling IRQs is disabling signals.
69          */
70         #include <signal.h>
71         typedef sigset_t cpuflags_t;
72
73         #define SET_ALL_SIGNALS(sigs) \
74         do { \
75                 sigfillset(&sigs); \
76                 sigdelset(&sigs, SIGINT); \
77                 sigdelset(&sigs, SIGSTOP); \
78                 sigdelset(&sigs, SIGCONT); \
79         } while(0)
80
81         #define IRQ_DISABLE \
82         do { \
83                 sigset_t sigs; \
84                 SET_ALL_SIGNALS(sigs); \
85                 sigprocmask(SIG_BLOCK, &sigs, NULL); \
86         } while (0)
87
88         #define IRQ_ENABLE \
89         do { \
90                 sigset_t sigs; \
91                 SET_ALL_SIGNALS(sigs); \
92                 sigprocmask(SIG_UNBLOCK, &sigs, NULL); \
93         } while (0)
94
95         #define IRQ_SAVE_DISABLE(old_sigs) \
96         do { \
97                 sigset_t sigs; \
98                 SET_ALL_SIGNALS(sigs); \
99                 sigprocmask(SIG_BLOCK, &sigs, &old_sigs); \
100         } while (0)
101
102         #define IRQ_RESTORE(old_sigs) \
103         do { \
104                 sigprocmask(SIG_SETMASK, &old_sigs, NULL); \
105         } while (0)
106 #else
107         #define OS_UNIX   0
108         #define OS_POSIX  0
109 #endif
110
111 #ifdef __linux__
112         #define OS_LINUX  1
113 #else
114         #define OS_LINUX  0
115 #endif
116
117 #if defined(__APPLE__) && defined(__MACH__)
118         #define OS_DARWIN 1
119 #else
120         #define OS_DARWIN 0
121 #endif
122
123 /*
124  * We want Qt and other frameworks to look like OSes because you would
125  * tipically want their portable abstractions if you're using one of these.
126  */
127 #if defined(_QT)
128         #define OS_QT 1
129         #undef  OS_ID
130         #define OS_ID qt
131 #else
132         #define OS_QT 0
133 #endif
134
135 /*
136  * Summarize hosted environments as OS_HOSTED and embedded
137  * environment with OS_EMBEDDED.
138  */
139 #if OS_WIN32 || OS_UNIX || OS_DARWIN
140         #define OS_HOSTED   1
141         #define OS_EMBEDDED 0
142 #else
143         #define OS_HOSTED   0
144         #define OS_EMBEDDED 1
145
146         /* Embedded environments fall back to CPU-specific code. */
147         #define OS_ID       CPU_ID
148 #endif
149
150 /* Self-check for the detection */
151 #if !defined(OS_ID)
152         #error OS_ID not defined
153 #endif
154 #if OS_HOSTED && OS_EMBEDDED
155         #error Both hosted and embedded OS environment
156 #endif
157 #if !OS_HOSTED && !OS_EMBEDDED
158         #error Neither hosted nor embedded OS environment
159 #endif
160
161 #endif /* DEVLIB_OS_H */