public/sourcepawn/ke_thread_windows.h
changeset 4030 eb4df1f7ff3c
parent 4029 cf8bdfcf6503
child 4031 852cf4415d12
     1.1 --- a/public/sourcepawn/ke_thread_windows.h	Thu Aug 22 14:05:44 2013 -0700
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,148 +0,0 @@
     1.4 -// vim: set ts=8 sts=2 sw=2 tw=99 et:
     1.5 -//
     1.6 -// This file is part of SourcePawn.
     1.7 -// 
     1.8 -// SourcePawn is free software: you can redistribute it and/or modify
     1.9 -// it under the terms of the GNU General Public License as published by
    1.10 -// the Free Software Foundation, either version 3 of the License, or
    1.11 -// (at your option) any later version.
    1.12 -// 
    1.13 -// SourcePawn is distributed in the hope that it will be useful,
    1.14 -// but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 -// GNU General Public License for more details.
    1.17 -// 
    1.18 -// You should have received a copy of the GNU General Public License
    1.19 -// along with SourcePawn.  If not, see <http://www.gnu.org/licenses/>.
    1.20 -#ifndef _include_sourcepawn_thread_windows_h_
    1.21 -#define _include_sourcepawn_thread_windows_h_
    1.22 -
    1.23 -#include <windows.h>
    1.24 -
    1.25 -namespace ke {
    1.26 -
    1.27 -class CriticalSection : public Lockable
    1.28 -{
    1.29 - public:
    1.30 -  CriticalSection() {
    1.31 -    InitializeCriticalSection(&cs_);
    1.32 -  }
    1.33 -  ~CriticalSection() {
    1.34 -    DeleteCriticalSection(&cs_);
    1.35 -  }
    1.36 -
    1.37 -  bool DoTryLock() {
    1.38 -    return !!TryEnterCriticalSection(&cs_);
    1.39 -  }
    1.40 -  void DoLock() {
    1.41 -    EnterCriticalSection(&cs_);
    1.42 -  }
    1.43 -
    1.44 -  void DoUnlock() {
    1.45 -    LeaveCriticalSection(&cs_);
    1.46 -  }
    1.47 -
    1.48 - private:
    1.49 -  CRITICAL_SECTION cs_;
    1.50 -};
    1.51 -
    1.52 -typedef CriticalSection Mutex;
    1.53 -
    1.54 -// Currently, this class only supports single-listener CVs.
    1.55 -class ConditionVariable : public Lockable
    1.56 -{
    1.57 - public:
    1.58 -  ConditionVariable() {
    1.59 -    event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
    1.60 -  }
    1.61 -  ~ConditionVariable() {
    1.62 -    CloseHandle(event_);
    1.63 -  }
    1.64 -
    1.65 -  bool DoTryLock() {
    1.66 -    return cs_.DoTryLock();
    1.67 -  }
    1.68 -  void DoLock() {
    1.69 -    cs_.DoLock();
    1.70 -  }
    1.71 -  void DoUnlock() {
    1.72 -    cs_.DoUnlock();
    1.73 -  }
    1.74 -
    1.75 -  void Notify() {
    1.76 -    AssertCurrentThreadOwns();
    1.77 -    SetEvent(event_);
    1.78 -  }
    1.79 -
    1.80 -  WaitResult Wait(size_t timeout_ms) {
    1.81 -    // This will assert if the lock has not been acquired. We don't need to be
    1.82 -    // atomic here, like pthread_cond_wait, because the event bit will stick
    1.83 -    // until reset by a wait function.
    1.84 -    Unlock();
    1.85 -    DWORD rv = WaitForSingleObject(event_, timeout_ms);
    1.86 -    Lock();
    1.87 -
    1.88 -    if (rv == WAIT_TIMEOUT)
    1.89 -      return Wait_Timeout;
    1.90 -    if (rv == WAIT_FAILED)
    1.91 -      return Wait_Error;
    1.92 -    return Wait_Signaled;
    1.93 -  }
    1.94 -
    1.95 -  WaitResult Wait() {
    1.96 -    return Wait(INFINITE);
    1.97 -  }
    1.98 -
    1.99 - private:
   1.100 -  CriticalSection cs_;
   1.101 -  HANDLE event_;
   1.102 -};
   1.103 -
   1.104 -class Thread
   1.105 -{
   1.106 - public:
   1.107 -  Thread(IRunnable *run, const char *name = NULL) {
   1.108 -    thread_ = CreateThread(NULL, 0, Main, run, 0, NULL);
   1.109 -  }
   1.110 -  ~Thread() {
   1.111 -    if (!thread_)
   1.112 -      return;
   1.113 -    CloseHandle(thread_);
   1.114 -  }
   1.115 -
   1.116 -  bool Succeeded() const {
   1.117 -    return !!thread_;
   1.118 -  }
   1.119 -
   1.120 -  void Join() {
   1.121 -    if (!Succeeded())
   1.122 -      return;
   1.123 -    WaitForSingleObject(thread_, INFINITE);
   1.124 -  }
   1.125 -
   1.126 -  HANDLE handle() const {
   1.127 -	  return thread_;
   1.128 -  }
   1.129 -
   1.130 - private:
   1.131 -  static DWORD WINAPI Main(LPVOID arg) {
   1.132 -    ((IRunnable *)arg)->Run();
   1.133 -    return 0;
   1.134 -  }
   1.135 -
   1.136 -#pragma pack(push, 8)
   1.137 -  struct ThreadNameInfo {
   1.138 -    DWORD dwType;
   1.139 -    LPCSTR szName;
   1.140 -    DWORD dwThreadID;
   1.141 -    DWORD dwFlags;
   1.142 -  };
   1.143 -#pragma pack(pop)
   1.144 -
   1.145 - private:
   1.146 -  HANDLE thread_;
   1.147 -};
   1.148 -
   1.149 -} // namespace ke
   1.150 -
   1.151 -#endif // _include_sourcepawn_thread_windows_h_