| 1 |
// Copyright (c) 2015 Ryan Prichard |
| 2 |
// |
| 3 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
// of this software and associated documentation files (the "Software"), to |
| 5 |
// deal in the Software without restriction, including without limitation the |
| 6 |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 |
// sell copies of the Software, and to permit persons to whom the Software is |
| 8 |
// furnished to do so, subject to the following conditions: |
| 9 |
// |
| 10 |
// The above copyright notice and this permission notice shall be included in |
| 11 |
// all copies or substantial portions of the Software. |
| 12 |
// |
| 13 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 |
// IN THE SOFTWARE. |
| 20 |
|
| 21 |
// Recent 4.x MinGW and MinGW-w64 gcc compilers lack std::mutex and |
| 22 |
// std::lock_guard. I have a 5.2.0 MinGW-w64 compiler packaged through MSYS2 |
| 23 |
// that *is* new enough, but that's one compiler against several deficient |
| 24 |
// ones. Wrap CRITICAL_SECTION instead. |
| 25 |
|
| 26 |
#ifndef WINPTY_SHARED_MUTEX_H |
| 27 |
#define WINPTY_SHARED_MUTEX_H |
| 28 |
|
| 29 |
#include <windows.h> |
| 30 |
|
| 31 |
class Mutex { |
| 32 |
CRITICAL_SECTION m_mutex; |
| 33 |
public: |
| 34 |
Mutex() { InitializeCriticalSection(&m_mutex); } |
| 35 |
~Mutex() { DeleteCriticalSection(&m_mutex); } |
| 36 |
void lock() { EnterCriticalSection(&m_mutex); } |
| 37 |
void unlock() { LeaveCriticalSection(&m_mutex); } |
| 38 |
|
| 39 |
Mutex(const Mutex &other) = delete; |
| 40 |
Mutex &operator=(const Mutex &other) = delete; |
| 41 |
}; |
| 42 |
|
| 43 |
template <typename T> |
| 44 |
class LockGuard { |
| 45 |
T &m_lock; |
| 46 |
public: |
| 47 |
LockGuard(T &lock) : m_lock(lock) { m_lock.lock(); } |
| 48 |
~LockGuard() { m_lock.unlock(); } |
| 49 |
|
| 50 |
LockGuard(const LockGuard &other) = delete; |
| 51 |
LockGuard &operator=(const LockGuard &other) = delete; |
| 52 |
}; |
| 53 |
|
| 54 |
#endif // WINPTY_SHARED_MUTEX_H |