Commit d7a17b1d authored by Alejandro Homs Puron's avatar Alejandro Homs Puron Committed by operator for beamline
Browse files

[WIP][BUFFER] sync_policy

parent 4aeefcb1
Pipeline #25271 failed with stages
in 105 minutes and 15 seconds
// Copyright (C) 2019 Alejandro Homs, ESRF.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
namespace lima
{
namespace buffer
{
class overrun_error : public std::exception
{
public:
virtual const char *what() const { return "Buffer overrun error"; }
};
template <class SyncContainer>
class NonBlockingSync
{
public:
struct buffer_type : public typename SyncContainer::element_type {};
void pre_get()
{ if (!SyncContainer::is_get_ok()) throw overrun_error(); }
void post_get(const buffer_type& e) {}
void pre_put(const buffer_type& e)
{ m_overwritten = !SyncContainer::is_put_ok(e); }
void post_put()
{ if (m_overwritten) throw overrun_error(); }
private:
bool m_overwritten;
};
template <class SyncContainer>
class BlockingSync
{
public:
struct buffer_type : public typename SyncContainer::element_type {};
void pre_get()
{
std::unique_lock<std::mutex> l(m_mutex);
if (!SyncContainer::is_get_ok())
m_cond.wait(m_mutex, [&] () {
return SyncContainer::is_get_ok();
});
}
void post_get(const buffer_type& e)
{}
void pre_put(const buffer_type& e)
{
std::lock_guard<std::mutex> l(m_mutex);
m_available = (!SyncContainer::is_get_ok() &&
SyncContainer::is_put_making_get_ok(e));
}
void post_put()
{
std::lock_guard<std::mutex> l(m_mutex);
if (m_available)
m_cond.notify_one();
}
private:
std::mutex m_mutex;
std::condition_variable m_cond;
bool m_available;
};
} // namespace buffer
} // namespace lima
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment