2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2013-01-11 10:50:59 +01:00
|
|
|
|
2014-07-02 20:07:08 +02:00
|
|
|
#ifndef THREAD_CRITICAL_SECTION_HXX
|
|
|
|
#define THREAD_CRITICAL_SECTION_HXX
|
2013-01-11 10:50:59 +01:00
|
|
|
|
2021-05-19 17:24:02 +02:00
|
|
|
#include <synchapi.h>
|
2013-01-11 10:50:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2013-01-11 10:50:59 +01:00
|
|
|
::InitializeCriticalSection(&critical_section);
|
|
|
|
}
|
|
|
|
|
2017-11-26 11:58:53 +01:00
|
|
|
~CriticalSection() noexcept {
|
2013-01-11 10:50:59 +01:00
|
|
|
::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 {
|
2013-01-11 10:50:59 +01:00
|
|
|
::EnterCriticalSection(&critical_section);
|
2020-03-17 16:57:57 +01:00
|
|
|
}
|
2013-01-11 10:50:59 +01:00
|
|
|
|
2017-11-26 11:58:53 +01:00
|
|
|
bool try_lock() noexcept {
|
2013-01-11 10:50:59 +01:00
|
|
|
return ::TryEnterCriticalSection(&critical_section) != 0;
|
2020-03-17 16:57:57 +01:00
|
|
|
}
|
2013-01-11 10:50:59 +01:00
|
|
|
|
2017-11-26 11:58:53 +01:00
|
|
|
void unlock() noexcept {
|
2013-01-11 10:50:59 +01:00
|
|
|
::LeaveCriticalSection(&critical_section);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|