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