libcruft-util/thread/monitor.hpp
Danny Robson 01094611eb thread: add minimal thread primitives for win32
This allows us to get around the lack of these types under MinGW
2019-06-22 15:46:34 +10:00

53 lines
1.2 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_THREAD_MONITOR_HPP
#define CRUFT_UTIL_THREAD_MONITOR_HPP
#include "mutex.hpp"
#include <utility>
#include <functional>
namespace cruft::thread {
template <typename ValueT, typename MutexT = cruft::thread::mutex>
class monitor {
public:
template <typename ...Args>
monitor (Args &&...args):
m_value (::std::forward<Args> (args)...)
{ ; }
class proxy {
public:
proxy (MutexT &_mutex, ValueT &_value):
m_guard (_mutex),
m_value (_value)
{ ; }
ValueT* operator-> ()
{
return &m_value;
}
private:
::std::lock_guard<MutexT> m_guard;
ValueT &m_value;
};
auto acquire (void) { return proxy (m_mutex, m_value); }
auto operator-> () { return acquire (); }
private:
MutexT m_mutex;
ValueT m_value;
};
};
#endif