mpd/src/thread/CriticalSection.hxx

43 lines
901 B
C++
Raw Normal View History

// SPDX-License-Identifier: BSD-2-Clause
// author: Max Kellermann <max.kellermann@gmail.com>
#ifndef THREAD_CRITICAL_SECTION_HXX
#define THREAD_CRITICAL_SECTION_HXX
#include <synchapi.h>
/**
* Wrapper for a CRITICAL_SECTION, backend for the Mutex class.
*/
class CriticalSection {
friend class WindowsCond;
CRITICAL_SECTION critical_section;
public:
2017-11-26 11:58:53 +01:00
CriticalSection() noexcept {
::InitializeCriticalSection(&critical_section);
}
2017-11-26 11:58:53 +01:00
~CriticalSection() noexcept {
::DeleteCriticalSection(&critical_section);
}
CriticalSection(const CriticalSection &other) = delete;
CriticalSection &operator=(const CriticalSection &other) = delete;
2017-11-26 11:58:53 +01:00
void lock() noexcept {
::EnterCriticalSection(&critical_section);
}
2017-11-26 11:58:53 +01:00
bool try_lock() noexcept {
return ::TryEnterCriticalSection(&critical_section) != 0;
}
2017-11-26 11:58:53 +01:00
void unlock() noexcept {
::LeaveCriticalSection(&critical_section);
}
};
#endif