os.h

Go to the documentation of this file.
00001 
00039 #ifndef CFG_OS_H
00040 #define CFG_OS_H
00041 
00042 /*
00043  * OS autodetection (Some systems trigger multiple OS definitions)
00044  */
00045 #ifdef _WIN32
00046     #define OS_WIN32  1
00047     #define OS_ID     win32
00048 
00049     // FIXME: Maybe disable Win32 exceptions?
00050     typedef int cpu_flags_t;
00051     #define IRQ_DISABLE                FIXME
00052     #define IRQ_ENABLE                 FIXME
00053     #define IRQ_SAVE_DISABLE(old_sigs) FIXME
00054     #define IRQ_RESTORE(old_sigs)      FIXME
00055 
00056 #else
00057     #define OS_WIN32  0
00058 #endif
00059 
00060 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
00061     #define OS_UNIX   1
00062     #define OS_POSIX  1  /* Not strictly UNIX, but no way to autodetect it. */
00063     #define OS_ID     posix
00064 
00065     /*
00066      * The POSIX moral equivalent of disabling IRQs is disabling signals.
00067      */
00068     #include <signal.h>
00069     typedef sigset_t cpu_flags_t;
00070 
00071     #define SET_ALL_SIGNALS(sigs) \
00072     do { \
00073         sigfillset(&sigs); \
00074         sigdelset(&sigs, SIGINT); \
00075         sigdelset(&sigs, SIGSTOP); \
00076         sigdelset(&sigs, SIGCONT); \
00077     } while(0)
00078 
00079     #define IRQ_DISABLE \
00080     do { \
00081         sigset_t sigs; \
00082         SET_ALL_SIGNALS(sigs); \
00083         sigprocmask(SIG_BLOCK, &sigs, NULL); \
00084     } while (0)
00085 
00086     #define IRQ_ENABLE \
00087     do { \
00088         sigset_t sigs; \
00089         SET_ALL_SIGNALS(sigs); \
00090         sigprocmask(SIG_UNBLOCK, &sigs, NULL); \
00091     } while (0)
00092 
00093     #define IRQ_SAVE_DISABLE(old_sigs) \
00094     do { \
00095         sigset_t sigs; \
00096         SET_ALL_SIGNALS(sigs); \
00097         sigprocmask(SIG_BLOCK, &sigs, &old_sigs); \
00098     } while (0)
00099 
00100     #define IRQ_RESTORE(old_sigs) \
00101     do { \
00102         sigprocmask(SIG_SETMASK, &old_sigs, NULL); \
00103     } while (0)
00104 
00105     #define IRQ_ENABLED() \
00106     ({ \
00107         sigset_t sigs__; \
00108         sigprocmask(SIG_SETMASK, NULL, &sigs__); \
00109         sigismember(&sigs__, SIGALRM) ? false : true; \
00110      })
00111 
00112 #else
00113     #define OS_UNIX   0
00114     #define OS_POSIX  0
00115 #endif
00116 
00117 #ifdef __linux__
00118     #define OS_LINUX  1
00119 #else
00120     #define OS_LINUX  0
00121 #endif
00122 
00123 #if defined(__APPLE__) && defined(__MACH__)
00124     #define OS_DARWIN 1
00125 #else
00126     #define OS_DARWIN 0
00127 #endif
00128 
00129 
00130 #include <cfg/cfg_arch.h> /* For ARCH_QT */
00131 
00132 /*
00133  * We want Qt and other frameworks to look like OSes because you would
00134  * tipically want their portable abstractions if you're using one of these.
00135  */
00136 #if defined(_QT) || (defined(ARCH_QT) && (ARCH & ARCH_QT))
00137     #define OS_QT 1
00138     #undef  OS_ID
00139     #define OS_ID qt
00140 #else
00141     #define OS_QT 0
00142 #endif
00143 
00144 /*
00145  * Summarize hosted environments as OS_HOSTED and embedded
00146  * environment with OS_EMBEDDED.
00147  */
00148 #if OS_WIN32 || OS_UNIX || OS_DARWIN || OS_QT
00149     #define OS_HOSTED   1
00150     #define OS_EMBEDDED 0
00151 #else
00152     #define OS_HOSTED   0
00153     #define OS_EMBEDDED 1
00154 
00155     /* Embedded environments fall back to CPU-specific code. */
00156     #define OS_ID       CPU_ID
00157 #endif
00158 
00159 /* Self-check for the detection */
00160 #if !defined(OS_ID)
00161     #error OS_ID not defined
00162 #endif
00163 #if OS_HOSTED && OS_EMBEDDED
00164     #error Both hosted and embedded OS environment
00165 #endif
00166 #if !OS_HOSTED && !OS_EMBEDDED
00167     #error Neither hosted nor embedded OS environment
00168 #endif
00169 
00170 #if OS_HOSTED
00171 
00173     #define OS_HEADER(module)  PP_STRINGIZE(emul/PP_CAT3(module, _, OS_ID).h)
00174 
00176     #define OS_CSOURCE(module) PP_STRINGIZE(emul/PP_CAT3(module, _, OS_ID).c)
00177 
00178 #else
00179     // Fallbacks for embedded systems
00180     #define OS_HEADER(module)  CPU_HEADER(module)
00181     #define OS_CSOURCE(module) CPU_CSOURCE(module)
00182 #endif
00183 
00184 #endif /* CFG_OS_H */