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