2013-01-04 20:50:26 +01:00
|
|
|
/*
|
2017-01-17 11:54:55 +01:00
|
|
|
* Copyright (C) 2009-2016 Max Kellermann <max.kellermann@gmail.com>
|
2013-01-04 20:50:26 +01:00
|
|
|
*
|
2014-07-02 20:07:08 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
2013-01-04 20:50:26 +01:00
|
|
|
*
|
2014-07-02 20:07:08 +02:00
|
|
|
* - Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2013-01-04 20:50:26 +01:00
|
|
|
*
|
2014-07-02 20:07:08 +02:00
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
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
|