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