os.h

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