00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef SePlatform_h
00011 #define SePlatform_h
00012 #define SePlatform_h
00013
00018
00019 #if defined(_WIN32) || defined(_WINDOWS) || defined(_MSC_VER)
00020 # ifndef WINDOWS
00021 # define WINDOWS
00022 # endif
00023 # define _CRT_NONSTDC_NO_DEPRECATE 1
00024 # define _CRT_SECURE_NO_DEPRECATE 1
00025 # define NOMINMAX 1
00026
00027
00028 # include <Windows.h>
00029 # include <malloc.h>
00030 # include <io.h>
00031 # include <tchar.h>
00032 # include <process.h>
00033
00034 #else
00035
00036
00037 # include <stdlib.h>
00038 # include <alloca.h>
00039 # include <string.h>
00040 # include <pthread.h>
00041 # include <inttypes.h>
00042
00043 # ifdef __APPLE__
00044 # include <libkern/OSAtomic.h>
00045 # include <sys/types.h>
00046 # endif
00047 #endif // defined(_WIN32)...
00048
00049
00050 #include <stdio.h>
00051 #include <math.h>
00052 #include <assert.h>
00053
00054
00055 #ifdef WINDOWS
00056 # define snprintf sprintf_s
00057 # define strtok_r strtok_s
00058 typedef __int64 FilePos;
00059 # define fseeko _fseeki64
00060 # define ftello _ftelli64
00061
00062 inline double log2(double x) {
00063 return log(x) * 1.4426950408889634;
00064 }
00065
00066 typedef unsigned int uint32_t;
00067 # define M_E (2.7182818284590452354)
00068 # define M_PI (3.141592653589793238)
00069 # define UINT32_MAX (0xffffffff)
00070 # define UINT32_MIN (0)
00071 #else
00072 typedef off_t FilePos;
00073 #endif
00074
00075
00076 namespace SeExprInternal {
00077
00078
00079
00080
00081
00082 #ifdef WINDOWS
00083
00084 class _Mutex {
00085 public:
00086 _Mutex() { _mutex = CreateMutex(NULL, FALSE, NULL); }
00087 ~_Mutex() { CloseHandle(_mutex); }
00088 void lock() { WaitForSingleObject(_mutex, INFINITE); }
00089 void unlock() { ReleaseMutex(_mutex); }
00090 private:
00091 HANDLE _mutex;
00092 };
00093
00094 class _SpinLock {
00095 public:
00096 _SpinLock() { InitializeCriticalSection(&_spinlock); }
00097 ~_SpinLock() { DeleteCriticalSection(&_spinlock); }
00098 void lock() { EnterCriticalSection(&_spinlock); }
00099 void unlock() { LeaveCriticalSection(&_spinlock); }
00100 private:
00101 CRITICAL_SECTION _spinlock;
00102 };
00103
00104 #else
00105
00106 class _Mutex {
00107 public:
00108 _Mutex() { pthread_mutex_init(&_mutex, 0); }
00109 ~_Mutex() { pthread_mutex_destroy(&_mutex); }
00110 void lock() { pthread_mutex_lock(&_mutex); }
00111 void unlock() { pthread_mutex_unlock(&_mutex); }
00112 private:
00113 pthread_mutex_t _mutex;
00114 };
00115
00116 # ifdef __APPLE__
00117 class _SpinLock {
00118 public:
00119 _SpinLock() { _spinlock = 0; }
00120 ~_SpinLock() { }
00121 void lock() { OSSpinLockLock(&_spinlock); }
00122 void unlock() { OSSpinLockUnlock(&_spinlock); }
00123 private:
00124 OSSpinLock _spinlock;
00125 };
00126 # else
00127 class _SpinLock {
00128 public:
00129 _SpinLock() { pthread_spin_init(&_spinlock, PTHREAD_PROCESS_PRIVATE); }
00130 ~_SpinLock() { pthread_spin_destroy(&_spinlock); }
00131 void lock() { pthread_spin_lock(&_spinlock); }
00132 void unlock() { pthread_spin_unlock(&_spinlock); }
00133 private:
00134 pthread_spinlock_t _spinlock;
00135 };
00136 # endif // __APPLE__
00137 #endif
00138 }
00139
00140 #endif // SePlatform_h