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