00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef WT_OS_WINDOWS_H
00022 #define WT_OS_WINDOWS_H
00023
00024
00025 #ifndef _WIN32_WINNT
00026 #define _WIN32_WINNT 0x0400
00027 #endif
00028
00029 #ifdef _MSC_VER
00030 #pragma warning ( disable:4355 )
00031 #endif
00032
00033 #include <wefts_cleanup.h>
00034 #include <wefts_os_base.h>
00035 #include <wefts_error.h>
00036
00037
00038 #ifdef __extern_c
00039 extern "C" {
00040 #endif
00041
00042 #include <windows.h>
00043
00044 #ifdef __extern_c
00045 }
00046 #endif
00047
00053 namespace Wefts {
00054
00055 extern DWORD s_tlsThreadObj;
00056
00057 extern void (* s_cleanerPointer)(void *);
00058
00068 class OSMutexWindows: public OSMutexBase
00069 {
00070 bool m_canTryLock;
00071 protected:
00072 CRITICAL_SECTION m_sect;
00073
00074 friend class OSConditionWindows;
00075 public:
00076 OSMutexWindows() throw( InitError );
00077
00078 ~OSMutexWindows()
00079 {
00080 DeleteCriticalSection( &m_sect );
00081 }
00082
00083 virtual inline void lock() { EnterCriticalSection( &m_sect ); }
00084
00085 virtual inline bool trylock() {
00086 if( m_canTryLock )
00087 return TryEnterCriticalSection( &m_sect ) == TRUE;
00088 else
00089 return false;
00090 }
00091
00093 virtual inline void unlock() { LeaveCriticalSection( &m_sect ); }
00094 };
00095
00096 #if 0
00097 class OSMutexWindows: public OSMutexBase {
00098 protected:
00099 CRITICAL_SECTION m_sema;
00100
00101 friend class OSConditionWindows;
00102 public:
00103 OSMutexWindows() throw( InitError )
00104 : OSMutexBase()
00105 {
00106 InitializeCriticalSection(&m_sema);
00107
00108 }
00109
00110 ~OSMutexWindows()
00111 {
00112 DeleteCriticalSection( &m_sema );
00113 }
00114
00115 virtual inline void lock() { EnterCriticalSection( &m_sema); }
00116
00117 virtual inline bool trylock() {
00118 OSVERSIONINFO osVer;
00119 osVer.dwOSVersionInfoSize = sizeof( osVer );
00120
00121 if( GetVersionEx( &osVer ) &&
00122 ( osVer.dwPlatformId == VER_PLATFORM_WIN32_NT || osVer.dwMajorVersion >= 5 )
00123 return TryEnterCriticalSection( &m_sema);
00124 else
00125 return false;
00126 }
00127
00129 virtual inline void unlock() { LeaveCriticalSection(&m_sema); }
00130 };
00131 #endif
00132
00137 class OSConditionWindows: public OSConditionBase {
00138 private:
00139 HANDLE semBlockLock;
00140 HANDLE semBlockQueue;
00141 CRITICAL_SECTION mtxUnblockLock;
00142 int nWaitersGone;
00143 int nWaitersBlocked;
00144 int nWaitersToUnblock;
00145
00146 public:
00147 OSConditionWindows() throw( InitError );
00148 ~OSConditionWindows();
00149
00150 void signal();
00151 void signalOne();
00152
00154 bool wait( OSMutexBase &mtx, CleanupItem &guard );
00155
00157 bool timedWait( OSMutexBase &mtx, long seconds, long nanoseconds,
00158 CleanupItem &guard );
00159 };
00160
00164 class OSThreadWindows: public OSThreadBase {
00165 private:
00167 DWORD m_thid;
00168
00170 HANDLE m_hth;
00171
00173 bool m_valid;
00174
00176 HANDLE m_evtCancel;
00177
00182 bool m_wasCanceled;
00183
00185 bool m_canCancel;
00186
00191 void (*m_rawCleanup)(void *);
00192
00197 void *m_rawCleanData;
00198
00199 friend class OSConditionWindows;
00200 friend class OSFileFuncWin;
00201 friend bool OSSleep( long nSeconds, long nNanoseconds );
00202 public:
00203 OSThreadWindows();
00204 OSThreadWindows( bool val ): OSThreadBase( val ) { setCurrent(); }
00205 ~OSThreadWindows();
00206
00228 virtual bool priority( OSPriority prio_class, int boost = 0);
00229
00230 bool start( void *(*func)(void *), void *data );
00231 bool stop();
00232 void *join() throw( NotJoinableError );
00233
00235 int enableCancel();
00236
00238 int disableCancel();
00239
00240 inline bool setCancelState( int state )
00241 {
00242 m_canCancel = (state != 0);
00243 return true;
00244 }
00245
00246 virtual void *osRun( void * (*func)(void *), void *data, void (*cleanup)(void *) );
00247
00248 void testCancel();
00249
00251 inline void setCancelDeferred() {};
00252
00253 inline void detach() {}
00254
00255 void exit( void *data );
00256
00257 inline bool same() const {
00258 return (m_valid && m_thid == GetCurrentThreadId());
00259 }
00260
00261 bool equal( const OSThreadBase &th ) const {
00262 const OSThreadWindows *other =static_cast<const OSThreadWindows *>( &th );
00263 return ( m_valid && other->m_valid && other->m_thid == m_thid );
00264 }
00265
00266 virtual bool setCurrent();
00267 virtual void invalidate();
00268 };
00269
00270 #include <stdio.h>
00271
00273 class OSSpecificDataWindows: public OSSpecificDataBase
00274 {
00275 private:
00276 DWORD m_data;
00277
00278 public:
00282 OSSpecificDataWindows() throw( InitError );
00283
00288 ~OSSpecificDataWindows()
00289 {
00290 if ( m_data != TLS_OUT_OF_INDEXES )
00291 TlsFree( m_data );
00292 }
00293
00294 void *data() const { return TlsGetValue( m_data ); }
00295 void data(const void *dt) {
00296 void *dt1 = const_cast<void *>(dt);
00297 TlsSetValue( m_data, dt1);
00298 }
00299 };
00300
00301 typedef OSSpecificDataWindows OSSpecificData;
00302 typedef OSThreadWindows OSThread;
00303 typedef OSConditionWindows OSCondition;
00304 typedef OSMutexWindows OSMutex;
00305
00306 }
00307
00308 #endif