00001 /* 00002 wefts_os_widnows.h 00003 MS-Windows specific header file 00004 00005 $Id: wefts_os_windows.h,v 1.7 2003/12/20 23:34:09 jonnymind Exp $ 00006 --------------------------------------- 00007 Begin : 2003-10-15 16.50 00008 Author : Giancarlo Niccolai 00009 00010 Last modified because: 00011 00012 */ 00013 00014 /************************************************************************** 00015 * This program is free software; you can redistribute it and/or modify * 00016 * it under the terms of the GNU Library General Public License as * 00017 * published by the Free Software Foundation; either version 2.1 of the * 00018 * License, or (at your option) any later version. * 00019 ***************************************************************************/ 00020 00021 #ifndef WT_OS_WINDOWS_H 00022 #define WT_OS_WINDOWS_H 00023 00024 #include <wefts_cleanup.h> 00025 #include <wefts_os_base.h> 00026 #include <wefts_error.h> 00027 00028 00029 #ifdef __extern_c 00030 extern "C" { 00031 #endif 00032 00033 #include <windows.h> 00034 00035 #ifdef __extern_c 00036 } 00037 #endif 00038 00044 namespace Wefts { 00045 00046 extern DWORD s_tlsThreadObj; 00047 00048 void (* s_cleanerPointer)(void *); 00049 00050 class OSMutexWindows: public OSMutexBase { 00051 protected: 00052 CRITICAL_SECTION m_sect; 00053 00054 friend class OSConditionWindows; 00055 public: 00056 OSMutexWindows() throw( InitError ); 00057 00058 ~OSMutexWindows() 00059 { 00060 DeleteCriticalSection( &m_sect ); 00061 } 00062 00063 virtual inline void lock() { EnterCriticalSection( &m_sect ); } 00064 00065 virtual inline bool trylock() { 00066 BOOL res = TryEnterCriticalSection( &m_sect ); 00067 return ( res == TRUE ); 00068 } 00069 00071 virtual inline void unlock() { LeaveCriticalSection( &m_sect ); } 00072 }; 00073 00074 #if 0 00075 class OSMutexWindows: public OSMutexBase { 00076 protected: 00077 CRITICAL_SECTION m_sema; 00078 00079 friend class OSConditionWindows; 00080 public: 00081 OSMutexWindows() throw( InitError ) 00082 : OSMutexBase() 00083 { 00084 InitializeCriticalSection(&m_sema); 00085 //if ( m_sema == NULL ) throw InitError( GetLastError() ); 00086 } 00087 00088 ~OSMutexWindows() 00089 { 00090 DeleteCriticalSection( &m_sema ); 00091 } 00092 00093 virtual inline void lock() { EnterCriticalSection( &m_sema); } 00094 00095 virtual inline bool trylock() { 00096 return TryEnterCriticalSection( &m_sema); 00097 } 00098 00100 virtual inline void unlock() { LeaveCriticalSection(&m_sema); } 00101 }; 00102 #endif 00103 00108 class OSConditionWindows: public OSConditionBase { 00109 private: 00110 HANDLE semBlockLock; 00111 HANDLE semBlockQueue; 00112 CRITICAL_SECTION mtxUnblockLock; 00113 int nWaitersGone; 00114 int nWaitersBlocked; 00115 int nWaitersToUnblock; 00116 00117 public: 00118 OSConditionWindows() throw( InitError ); 00119 ~OSConditionWindows(); 00120 00121 void signal(); 00123 bool wait( OSMutexBase &mtx, CleanupItem guard ); 00124 00126 bool timedWait( OSMutexBase &mtx, long seconds, long nanoseconds, 00127 CleanupItem guard ); 00128 }; 00129 00133 class OSThreadWindows: public OSThreadBase { 00134 private: 00136 DWORD m_thid; 00137 00139 HANDLE m_hth; 00140 00142 bool m_valid; 00143 00145 HANDLE m_evtCancel; 00146 00151 bool m_wasCanceled; 00152 00154 bool m_canCancel; 00155 00160 void (*m_rawCleanup)(void *); 00161 00166 void *m_rawCleanData; 00167 00168 friend class OSConditionWindows; 00169 friend class OSFileFuncWin; 00170 friend bool OSSleep( long nSeconds, long nNanoseconds ); 00171 public: 00172 OSThreadWindows(); 00173 OSThreadWindows( bool val ): OSThreadBase( val ) { setCurrent(); } 00174 ~OSThreadWindows(); 00175 00176 bool start( void *(*func)(void *), void *data ); 00177 bool stop(); 00178 void *join() throw( NotJoinableError ); 00179 00181 int enableCancel(); 00182 00184 int disableCancel(); 00185 00186 inline bool setCancelState( int state ) 00187 { 00188 m_canCancel = (bool) state; 00189 return true; 00190 } 00191 00192 virtual void osRun( void (*func)(void *), void *data, void (*cleanup)(void *) ); 00193 00194 void testCancel(); 00195 00197 inline void setCancelDeferred() {}; 00198 00199 inline void detach() {} 00200 00201 void exit( void *data ); 00202 00203 inline bool same() const { 00204 return (m_valid && m_thid == GetCurrentThreadId()); 00205 } 00206 00207 bool equal( const OSThreadBase &th ) const { 00208 const OSThreadWindows *other =static_cast<const OSThreadWindows *>( &th ); 00209 return ( m_valid && other->m_valid && other->m_thid == m_thid ); 00210 } 00211 00212 virtual bool setCurrent(); 00213 virtual void invalidate(); 00214 }; 00215 00216 #include <stdio.h> 00217 00219 class OSSpecificDataWindows: public OSSpecificDataBase 00220 { 00221 private: 00222 DWORD m_data; 00223 00224 public: 00228 OSSpecificDataWindows() throw( InitError ); 00229 00234 ~OSSpecificDataWindows() 00235 { 00236 if ( m_data != TLS_OUT_OF_INDEXES ) 00237 TlsFree( m_data ); 00238 } 00239 00240 void *data() const { return TlsGetValue( m_data ); } 00241 void data(const void *dt) { 00242 void *dt1 = const_cast<void *>(dt); 00243 TlsSetValue( m_data, dt1); 00244 } 00245 }; 00246 00247 typedef OSSpecificDataWindows OSSpecificData; 00248 typedef OSThreadWindows OSThread; 00249 typedef OSConditionWindows OSCondition; 00250 typedef OSMutexWindows OSMutex; 00251 00253 } // namespace 00254 00255 #endif