mpd/src/thread/Mutex.hxx

41 lines
666 B
C++
Raw Normal View History

// SPDX-License-Identifier: BSD-2-Clause
// author: Max Kellermann <max.kellermann@gmail.com>
#ifndef THREAD_MUTEX_HXX
#define THREAD_MUTEX_HXX
#include <mutex>
#ifdef _WIN32
#include "CriticalSection.hxx"
using Mutex = CriticalSection;
#else
using Mutex = std::mutex;
#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();
}
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;
};
#endif