00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef FALCON_MT_WIN_H
00018 #define FALCON_MT_WIN_H
00019
00020 #include <falcon/setup.h>
00021 #include <falcon/types.h>
00022 #include <falcon/fassert.h>
00023 #include <windows.h>
00024
00025 namespace Falcon
00026 {
00027
00035 class FALCON_DYN_CLASS Mutex
00036 {
00037 CRITICAL_SECTION m_mtx;
00038
00039 public:
00043 inline Mutex()
00044 {
00045 InitializeCriticalSectionAndSpinCount( &m_mtx, 512 );
00046 }
00047
00053 inline ~Mutex() {
00054 DeleteCriticalSection( &m_mtx );
00055 }
00056
00062 inline void lock()
00063 {
00064 EnterCriticalSection( &m_mtx );
00065 }
00066
00072 inline void unlock()
00073 {
00074 LeaveCriticalSection( &m_mtx );
00075 }
00076
00082 inline bool trylock()
00083 {
00084 return TryEnterCriticalSection( &m_mtx ) == TRUE;
00085 }
00086
00087 };
00088
00094 class FALCON_DYN_CLASS ThreadSpecific
00095 {
00096 private:
00097 DWORD m_key;
00098 void (*m_destructor)(void*);
00099 ThreadSpecific* m_nextDestructor;
00100
00101 public:
00102 ThreadSpecific()
00103 {
00104 m_key = TlsAlloc();
00105 m_destructor = 0;
00106 }
00107
00108 ThreadSpecific( void (*destructor)(void*) );
00109
00110 virtual ~ThreadSpecific()
00111 {
00112 #ifndef NDEBUG
00113 BOOL res = TlsFree( m_key );
00114 fassert( res );
00115 #else
00116 TlsFree( m_key );
00117 #endif
00118 }
00119
00120 ThreadSpecific* clearAndNext();
00121
00122 void set( void *value );
00123
00124 void* get() const
00125 {
00126 return TlsGetValue( m_key );
00127 }
00128 };
00129
00131 inline int32 atomicInc( volatile int32 &data )
00132 {
00133 volatile LONG* dp = (volatile LONG*) &data;
00134 return InterlockedIncrement( dp );
00135 }
00136
00138 inline int32 atomicDec( volatile int32 &data )
00139 {
00140 volatile LONG* dp = (volatile LONG*) &data;
00141 return InterlockedDecrement( dp );
00142 }
00143
00160 class FALCON_DYN_CLASS Event
00161 {
00162 HANDLE m_hEvent;
00163
00164 public:
00168 inline Event( bool bAutoReset = true, bool initState = false )
00169 {
00170
00171 m_hEvent = CreateEvent( NULL, bAutoReset ? FALSE : TRUE, initState ? TRUE : FALSE, NULL );
00172 }
00173
00179 inline ~Event() {
00180 CloseHandle( m_hEvent );
00181 }
00182
00187 inline void set() { SetEvent( m_hEvent ); }
00188
00189
00194 inline void reset() { ResetEvent( m_hEvent ); }
00195
00211 bool wait( int32 to = -1 ) { return WaitForSingleObject( m_hEvent, to >= 0 ? to : INFINITE ) == WAIT_OBJECT_0; }
00212 };
00213
00214
00215 struct SYSTH_DATA {
00216 HANDLE hThread;
00217 unsigned nThreadID;
00218 HANDLE hEvtDetach;
00219 void *retval;
00220
00222 CRITICAL_SECTION m_csT;
00223
00225 bool m_bDone;
00227 bool m_bDetached;
00228 bool m_bJoining;
00229 };
00230
00231
00232 }
00233
00234 #endif
00235
00236