1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
/*
* Copyright © 2017 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 2 or 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Christopher James Halse Rogers <christopher.halse.rogers@canonical.com>
*/
#ifndef WLCS_MUTEX_H_
#define WLCS_MUTEX_H_
#include <mutex>
#include <condition_variable>
#include <boost/throw_exception.hpp>
namespace wlcs
{
/**
* Smart-pointer-esque accessor for Mutex<> protected data.
*
* Ensures exclusive access to the referenced data.
*
* \tparam Guarded Type of data guarded by the mutex.
*/
template<typename Guarded>
class MutexGuard
{
public:
MutexGuard(std::unique_lock<std::mutex>&& lock, Guarded& value)
: value{value},
lock{std::move(lock)}
{
}
MutexGuard(MutexGuard&& from) = default;
~MutexGuard() noexcept(false)
{
if (lock.owns_lock())
{
lock.unlock();
}
}
Guarded& operator*()
{
return value;
}
Guarded* operator->()
{
return &value;
}
private:
Guarded& value;
std::unique_lock<std::mutex> lock;
};
/**
* A data-locking mutex
*
* This is a mutex which owns the data it guards, and can give out a
* smart-pointer-esque lock to lock and access it.
*
* \tparam Guarded The type of data guarded by the mutex
*/
template<typename Guarded>
class Mutex
{
public:
Mutex() = default;
Mutex(Guarded&& initial_value)
: value{std::move(initial_value)}
{
}
Mutex(Mutex const&) = delete;
Mutex& operator=(Mutex const&) = delete;
/**
* Lock the mutex and return an accessor for the protected data.
*
* \return A smart-pointer-esque accessor for the contained data.
* While code has access to the MutexGuard it is guaranteed to have exclusive
* access to the contained data.
*/
MutexGuard<Guarded> lock()
{
return MutexGuard<Guarded>{std::unique_lock<std::mutex>{mutex}, value};
}
protected:
std::mutex mutex;
Guarded value;
};
template<typename Guarded>
class WaitableMutex : public Mutex<Guarded>
{
public:
using Mutex<Guarded>::Mutex;
template<typename Predicate, typename Rep, typename Period>
MutexGuard<Guarded> wait_for(Predicate predicate, std::chrono::duration<Rep, Period> timeout)
{
std::unique_lock<std::mutex> lock{this->mutex};
if (!notifier.wait_for(lock, timeout, [this, &predicate]() { return predicate(this->value); }))
{
BOOST_THROW_EXCEPTION((std::runtime_error{"Notification timeout"}));
}
return MutexGuard<Guarded>{std::move(lock), this->value};
}
void notify_all()
{
notifier.notify_all();
}
private:
std::condition_variable notifier;
};
}
#endif //WLCS_MUTEX_H_
|