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