Textractor_test/host/winmutex.h

36 lines
711 B
C
Raw Normal View History

2015-04-02 22:29:31 +08:00
#pragma once
// winmutex.h
// 12/11/2011 jichi
#include <windows.h>
#ifdef _MSC_VER
# pragma warning(disable:4800) // C4800: forcing value to bool
#endif // _MSC_VER
2018-07-21 05:36:58 +08:00
// Artikash 7/20/2018: these are similar to std::lock guard but use Winapi objects
2015-04-02 22:29:31 +08:00
2018-07-21 05:36:58 +08:00
class MutexLocker
{
2018-08-09 11:56:36 +08:00
HANDLE mutex;
2018-07-21 05:36:58 +08:00
public:
2018-08-09 11:56:36 +08:00
explicit MutexLocker(HANDLE mutex) : mutex(mutex)
2018-07-21 05:36:58 +08:00
{
2018-08-09 11:56:36 +08:00
WaitForSingleObject(mutex, 0);
2018-07-21 05:36:58 +08:00
}
2018-08-09 11:56:36 +08:00
~MutexLocker() { if (mutex != INVALID_HANDLE_VALUE && mutex != nullptr) ReleaseMutex(mutex); }
2018-07-21 05:36:58 +08:00
};
class CriticalSectionLocker
{
2018-08-09 11:56:36 +08:00
CRITICAL_SECTION* cs;
2018-07-21 05:36:58 +08:00
public:
2018-08-09 11:56:36 +08:00
explicit CriticalSectionLocker(CRITICAL_SECTION* cs) : cs(cs)
2018-07-21 05:36:58 +08:00
{
2018-08-09 11:56:36 +08:00
EnterCriticalSection(cs);
2018-07-21 05:36:58 +08:00
}
2018-08-09 11:56:36 +08:00
~CriticalSectionLocker() { LeaveCriticalSection(cs); }
2018-07-21 05:36:58 +08:00
};
2018-06-13 05:31:24 +08:00
2015-04-02 22:29:31 +08:00
// EOF