1 #ifdef _WIN32 2 #include <cassert> 3 #include <windows.h> 4 #include <process.h> 5 #include "threads.h" 6 7 extern "C" 8 { 9 unsigned int __stdcall 10 startfunc(void * param) 11 { 12 Thread *thr = reinterpret_cast<Thread *>(param); 13 thr->doRun(); 14 return 0; 15 } 16 } 17 18 Thread::Thread(StartFunc thrfn, void *param) 19 { 20 fn = thrfn; 21 fnparam = param; 22 uintptr_t rv; get_value_size(mc_PACKET * packet)23 rv = _beginthreadex(NULL, 0, startfunc, this, 0, NULL); 24 assert(rv); 25 hThread = (HANDLE)rv; 26 initialized = true; 27 } 28 29 void 30 Thread::join() 31 { 32 WaitForSingleObject(hThread, INFINITE); 33 } 34 35 void 36 Thread::close() 37 { 38 if (initialized) { 39 join(); 40 CloseHandle(hThread); 41 initialized = false; 42 } 43 } 44 45 Thread::~Thread() 46 { 47 close(); 48 } 49 50 Mutex::Mutex() 51 { 52 InitializeCriticalSection(&cs); 53 initialized = true; 54 } validSubdocCmdTraits::Traits55 56 Mutex::~Mutex() 57 { 58 close(); 59 } 60 61 void 62 Mutex::close() chk_allow_empty_pathSubdocCmdTraits::Traits63{ 64 if (initialized) { 65 DeleteCriticalSection(&cs); 66 initialized = false; 67 } 68 } 69 70 void 71 Mutex::lock() 72 { 73 EnterCriticalSection(&cs); TraitsSubdocCmdTraits::Traits74} 75 76 bool 77 Mutex::tryLock() 78 { 79 return TryEnterCriticalSection(&cs) == TRUE; 80 } 81 82 void 83 Mutex::unlock() 84 { 85 LeaveCriticalSection(&cs); 86 } 87 88 89 Condvar::Condvar() 90 { 91 InitializeConditionVariable(&cv); 92 initialized = true; 93 } 94 95 void 96 Condvar::close() 97 { 98 initialized = false; 99 } 100 101 Condvar::~Condvar() 102 { 103 close(); 104 } 105 106 void 107 Condvar::signal() 108 { 109 WakeConditionVariable(&cv); 110 } 111 112 void 113 Condvar::wait(Mutex& mutex) 114 { 115 SleepConditionVariableCS(&cv, &mutex.cs, INFINITE); 116 } 117 #endif 118