2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2013-01-04 20:50:26 +01:00
|
|
|
|
2014-07-02 20:07:08 +02:00
|
|
|
#ifndef THREAD_MUTEX_HXX
|
|
|
|
#define THREAD_MUTEX_HXX
|
2013-01-04 20:50:26 +01:00
|
|
|
|
2016-12-29 11:51:25 +01:00
|
|
|
#include <mutex>
|
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2013-01-04 20:50:26 +01:00
|
|
|
|
2013-01-11 10:50:59 +01:00
|
|
|
#include "CriticalSection.hxx"
|
2019-05-07 19:23:01 +02:00
|
|
|
using Mutex = CriticalSection;
|
2013-01-04 20:50:26 +01:00
|
|
|
|
2013-01-07 10:32:01 +01:00
|
|
|
#else
|
|
|
|
|
2019-04-25 18:37:08 +02:00
|
|
|
using Mutex = std::mutex;
|
2013-01-07 10:32:01 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-12-30 06:13:49 +01:00
|
|
|
/**
|
|
|
|
* Within the scope of an instance, this class will keep a #Mutex
|
|
|
|
* unlocked.
|
|
|
|
*/
|
|
|
|
class ScopeUnlock {
|
|
|
|
Mutex &mutex;
|
|
|
|
|
|
|
|
public:
|
2017-11-26 11:58:53 +01:00
|
|
|
explicit ScopeUnlock(Mutex &_mutex) noexcept:mutex(_mutex) {
|
2015-12-30 06:13:49 +01:00
|
|
|
mutex.unlock();
|
2020-03-16 06:39:56 +01:00
|
|
|
}
|
2015-12-30 06:13:49 +01:00
|
|
|
|
2017-11-26 11:58:53 +01:00
|
|
|
~ScopeUnlock() noexcept {
|
2015-12-30 06:13:49 +01:00
|
|
|
mutex.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopeUnlock(const ScopeUnlock &other) = delete;
|
|
|
|
ScopeUnlock &operator=(const ScopeUnlock &other) = delete;
|
|
|
|
};
|
|
|
|
|
2013-01-04 20:50:26 +01:00
|
|
|
#endif
|