include/am-thread-windows.h
changeset 0 ba3d4abaf3f1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/include/am-thread-windows.h	Fri Aug 23 20:34:17 2013 -0700
     1.3 @@ -0,0 +1,161 @@
     1.4 +// vim: set sts=8 ts=2 sw=2 tw=99 et:
     1.5 +//
     1.6 +// Copyright (C) 2013, David Anderson and AlliedModders LLC
     1.7 +// All rights reserved.
     1.8 +// 
     1.9 +// Redistribution and use in source and binary forms, with or without
    1.10 +// modification, are permitted provided that the following conditions are met:
    1.11 +// 
    1.12 +//  * Redistributions of source code must retain the above copyright notice, this
    1.13 +//    list of conditions and the following disclaimer.
    1.14 +//  * Redistributions in binary form must reproduce the above copyright notice,
    1.15 +//    this list of conditions and the following disclaimer in the documentation
    1.16 +//    and/or other materials provided with the distribution.
    1.17 +//  * Neither the name of AlliedModders LLC nor the names of its contributors
    1.18 +//    may be used to endorse or promote products derived from this software
    1.19 +//    without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.22 +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.23 +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.24 +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.25 +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.26 +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.27 +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.28 +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.29 +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.30 +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.31 +// POSSIBILITY OF SUCH DAMAGE.
    1.32 +
    1.33 +#ifndef _include_amtl_thread_windows_h_
    1.34 +#define _include_amtl_thread_windows_h_
    1.35 +
    1.36 +#include <windows.h>
    1.37 +
    1.38 +namespace ke {
    1.39 +
    1.40 +class CriticalSection : public Lockable
    1.41 +{
    1.42 + public:
    1.43 +  CriticalSection() {
    1.44 +    InitializeCriticalSection(&cs_);
    1.45 +  }
    1.46 +  ~CriticalSection() {
    1.47 +    DeleteCriticalSection(&cs_);
    1.48 +  }
    1.49 +
    1.50 +  bool DoTryLock() KE_OVERRIDE {
    1.51 +    return !!TryEnterCriticalSection(&cs_);
    1.52 +  }
    1.53 +  void DoLock() KE_OVERRIDE {
    1.54 +    EnterCriticalSection(&cs_);
    1.55 +  }
    1.56 +
    1.57 +  void DoUnlock() KE_OVERRIDE {
    1.58 +    LeaveCriticalSection(&cs_);
    1.59 +  }
    1.60 +
    1.61 + private:
    1.62 +  CRITICAL_SECTION cs_;
    1.63 +};
    1.64 +
    1.65 +typedef CriticalSection Mutex;
    1.66 +
    1.67 +// Currently, this class only supports single-listener CVs.
    1.68 +class ConditionVariable : public Lockable
    1.69 +{
    1.70 + public:
    1.71 +  ConditionVariable() {
    1.72 +    event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
    1.73 +  }
    1.74 +  ~ConditionVariable() {
    1.75 +    CloseHandle(event_);
    1.76 +  }
    1.77 +
    1.78 +  bool DoTryLock() KE_OVERRIDE {
    1.79 +    return cs_.DoTryLock();
    1.80 +  }
    1.81 +  void DoLock() KE_OVERRIDE {
    1.82 +    cs_.DoLock();
    1.83 +  }
    1.84 +  void DoUnlock() KE_OVERRIDE {
    1.85 +    cs_.DoUnlock();
    1.86 +  }
    1.87 +
    1.88 +  void Notify() {
    1.89 +    AssertCurrentThreadOwns();
    1.90 +    SetEvent(event_);
    1.91 +  }
    1.92 +
    1.93 +  WaitResult Wait(size_t timeout_ms) {
    1.94 +    // This will assert if the lock has not been acquired. We don't need to be
    1.95 +    // atomic here, like pthread_cond_wait, because the event bit will stick
    1.96 +    // until reset by a wait function.
    1.97 +    Unlock();
    1.98 +    DWORD rv = WaitForSingleObject(event_, timeout_ms);
    1.99 +    Lock();
   1.100 +
   1.101 +    if (rv == WAIT_TIMEOUT)
   1.102 +      return Wait_Timeout;
   1.103 +    if (rv == WAIT_FAILED)
   1.104 +      return Wait_Error;
   1.105 +    return Wait_Signaled;
   1.106 +  }
   1.107 +
   1.108 +  WaitResult Wait() {
   1.109 +    return Wait(INFINITE);
   1.110 +  }
   1.111 +
   1.112 + private:
   1.113 +  CriticalSection cs_;
   1.114 +  HANDLE event_;
   1.115 +};
   1.116 +
   1.117 +class Thread
   1.118 +{
   1.119 + public:
   1.120 +  Thread(IRunnable *run, const char *name = NULL) {
   1.121 +    thread_ = CreateThread(NULL, 0, Main, run, 0, NULL);
   1.122 +  }
   1.123 +  ~Thread() {
   1.124 +    if (!thread_)
   1.125 +      return;
   1.126 +    CloseHandle(thread_);
   1.127 +  }
   1.128 +
   1.129 +  bool Succeeded() const {
   1.130 +    return !!thread_;
   1.131 +  }
   1.132 +
   1.133 +  void Join() {
   1.134 +    if (!Succeeded())
   1.135 +      return;
   1.136 +    WaitForSingleObject(thread_, INFINITE);
   1.137 +  }
   1.138 +
   1.139 +  HANDLE handle() const {
   1.140 +	  return thread_;
   1.141 +  }
   1.142 +
   1.143 + private:
   1.144 +  static DWORD WINAPI Main(LPVOID arg) {
   1.145 +    ((IRunnable *)arg)->Run();
   1.146 +    return 0;
   1.147 +  }
   1.148 +
   1.149 +#pragma pack(push, 8)
   1.150 +  struct ThreadNameInfo {
   1.151 +    DWORD dwType;
   1.152 +    LPCSTR szName;
   1.153 +    DWORD dwThreadID;
   1.154 +    DWORD dwFlags;
   1.155 +  };
   1.156 +#pragma pack(pop)
   1.157 +
   1.158 + private:
   1.159 +  HANDLE thread_;
   1.160 +};
   1.161 +
   1.162 +} // namespace ke
   1.163 +
   1.164 +#endif // _include_amtl_thread_windows_h_