#include <util/signalinterrupt.h>
#ifdef WIN32
#include <mutex>
#else
#include <util/tokenpipe.h>
#endif
#include <ios>
#include <optional>
namespace util {
SignalInterrupt::SignalInterrupt() : m_flag{false}
{
#ifndef WIN32
std::optional<TokenPipe> pipe = TokenPipe::Make();
if (!pipe) throw std::ios_base::failure("Could not create TokenPipe");
m_pipe_r = pipe->TakeReadEnd();
m_pipe_w = pipe->TakeWriteEnd();
#endif
}
SignalInterrupt::operator bool() const
{
return m_flag;
}
bool SignalInterrupt::reset()
{
if (*this && !wait()) return false;
m_flag = false;
return true;
}
bool SignalInterrupt::operator()()
{
#ifdef WIN32
std::unique_lock<std::mutex> lk(m_mutex);
m_flag = true;
m_cv.notify_one();
#else
if (!m_flag.exchange(true)) {
int res = m_pipe_w.TokenWrite('x');
if (res != 0) {
return false;
}
}
#endif
return true;
}
bool SignalInterrupt::wait()
{
#ifdef WIN32
std::unique_lock<std::mutex> lk(m_mutex);
m_cv.wait(lk, [this] { return m_flag.load(); });
#else
int res = m_pipe_r.TokenRead();
if (res != 'x') {
return false;
}
#endif
return true;
}
}