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