2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-09-20 04:01:37 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_CTX_VIEW_H
|
|
|
|
|
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
2018-03-05 14:16:35 +01:00
|
|
|
template<class T, class mutex = mutex> class view;
|
|
|
|
template<class T> using shared_view = view<T, shared_mutex>;
|
2017-09-20 04:01:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Device for a context to share data on its stack with others while yielding
|
|
|
|
///
|
|
|
|
/// The view yields a context while other contexts examine the object pointed
|
|
|
|
/// to in the view. This allows a producing context to construct something
|
|
|
|
/// on its stack and then wait for the consuming contexts to do something with
|
|
|
|
/// that data before the producer resumes and potentially destroys the data.
|
|
|
|
/// This creates a very simple and lightweight single-producer/multi-consumer
|
|
|
|
/// queue mechanism using only context switching.
|
|
|
|
///
|
2017-09-30 08:06:23 +02:00
|
|
|
/// The producer is blocked until all consumers are finished with their view.
|
|
|
|
/// The consumers acquire the unique_lock before passing it to the call to wait().
|
|
|
|
/// wait() returns with a view of the object under unique_lock. Once the
|
|
|
|
/// consumer releases the unique_lock the viewed object is not safe for them.
|
2017-09-20 04:01:37 +02:00
|
|
|
///
|
2018-03-05 14:16:35 +01:00
|
|
|
template<class T,
|
|
|
|
class mutex>
|
2017-09-20 04:01:37 +02:00
|
|
|
class ircd::ctx::view
|
2017-09-30 08:06:23 +02:00
|
|
|
:public mutex
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
|
|
|
T *t {nullptr};
|
2017-09-30 08:06:23 +02:00
|
|
|
dock q;
|
|
|
|
size_t waiting {0};
|
2017-09-20 04:01:37 +02:00
|
|
|
|
|
|
|
bool ready() const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Consumer interface;
|
2018-03-05 14:16:35 +01:00
|
|
|
template<class lock, class time_point> T &wait_until(lock &, time_point&&);
|
|
|
|
template<class lock, class duration> T &wait_for(lock &, const duration &);
|
|
|
|
template<class lock> T &wait(lock &);
|
2017-09-20 04:01:37 +02:00
|
|
|
|
|
|
|
// Producer interface;
|
2018-03-06 12:50:39 +01:00
|
|
|
void expose(T &);
|
2017-09-20 04:01:37 +02:00
|
|
|
|
|
|
|
view() = default;
|
|
|
|
~view() noexcept;
|
|
|
|
};
|
|
|
|
|
2018-03-05 14:16:35 +01:00
|
|
|
template<class T,
|
|
|
|
class mutex>
|
|
|
|
ircd::ctx::view<T, mutex>::~view()
|
2017-09-20 04:01:37 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2017-09-30 08:06:23 +02:00
|
|
|
assert(!waiting);
|
2017-09-20 04:01:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 14:16:35 +01:00
|
|
|
template<class T,
|
|
|
|
class mutex>
|
2017-09-20 04:01:37 +02:00
|
|
|
void
|
2018-03-06 12:50:39 +01:00
|
|
|
ircd::ctx::view<T, mutex>::expose(T &t)
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
2017-09-30 08:06:23 +02:00
|
|
|
if(!waiting)
|
|
|
|
return;
|
2017-09-20 04:01:37 +02:00
|
|
|
|
|
|
|
this->t = &t;
|
2017-09-30 08:06:23 +02:00
|
|
|
q.notify_all();
|
|
|
|
q.wait([this] { return !waiting; });
|
|
|
|
const std::lock_guard<view> lock{*this};
|
|
|
|
this->t = nullptr;
|
|
|
|
assert(!waiting);
|
|
|
|
q.notify_all();
|
2017-09-20 04:01:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 14:16:35 +01:00
|
|
|
template<class T,
|
|
|
|
class mutex>
|
|
|
|
template<class lock>
|
2017-09-20 04:01:37 +02:00
|
|
|
T &
|
2018-03-05 14:16:35 +01:00
|
|
|
ircd::ctx::view<T, mutex>::wait(lock &l)
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
2018-03-05 14:16:35 +01:00
|
|
|
for(assert(l.owns_lock()); ready(); l.lock())
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
2018-03-05 14:16:35 +01:00
|
|
|
l.unlock();
|
2017-09-30 08:06:23 +02:00
|
|
|
q.wait();
|
|
|
|
}
|
2017-09-20 04:01:37 +02:00
|
|
|
|
2017-09-30 08:06:23 +02:00
|
|
|
const unwind ul{[this]
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
2017-09-30 08:06:23 +02:00
|
|
|
--waiting;
|
|
|
|
q.notify_all();
|
|
|
|
}};
|
|
|
|
|
2018-03-05 14:16:35 +01:00
|
|
|
for(++waiting; !ready(); l.lock())
|
2017-09-30 08:06:23 +02:00
|
|
|
{
|
2018-03-05 14:16:35 +01:00
|
|
|
l.unlock();
|
2017-09-30 08:06:23 +02:00
|
|
|
q.wait();
|
|
|
|
}
|
2017-09-20 04:01:37 +02:00
|
|
|
|
|
|
|
assert(t != nullptr);
|
|
|
|
return *t;
|
|
|
|
}
|
|
|
|
|
2018-03-05 14:16:35 +01:00
|
|
|
template<class T,
|
|
|
|
class mutex>
|
|
|
|
template<class lock,
|
|
|
|
class duration>
|
2017-09-20 04:01:37 +02:00
|
|
|
T &
|
2018-03-05 14:16:35 +01:00
|
|
|
ircd::ctx::view<T, mutex>::wait_for(lock &l,
|
|
|
|
const duration &dur)
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
2018-03-05 14:16:35 +01:00
|
|
|
return wait_until(l, now<steady_point>() + dur);
|
2017-09-20 04:01:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 14:16:35 +01:00
|
|
|
template<class T,
|
|
|
|
class mutex>
|
|
|
|
template<class lock,
|
|
|
|
class time_point>
|
2017-09-20 04:01:37 +02:00
|
|
|
T &
|
2018-03-05 14:16:35 +01:00
|
|
|
ircd::ctx::view<T, mutex>::wait_until(lock &l,
|
|
|
|
time_point&& tp)
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
2018-03-05 14:16:35 +01:00
|
|
|
for(assert(l.owns_lock()); ready(); l.lock())
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
2018-03-05 14:16:35 +01:00
|
|
|
l.unlock();
|
2017-09-30 08:06:23 +02:00
|
|
|
q.wait_until(tp);
|
|
|
|
}
|
2017-09-20 04:01:37 +02:00
|
|
|
|
2017-09-30 08:06:23 +02:00
|
|
|
const unwind ul{[this]
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
2017-09-30 08:06:23 +02:00
|
|
|
--waiting;
|
|
|
|
q.notify_all();
|
|
|
|
}};
|
|
|
|
|
2018-03-05 14:16:35 +01:00
|
|
|
for(++waiting; !ready(); l.lock())
|
2017-09-30 08:06:23 +02:00
|
|
|
{
|
2018-03-05 14:16:35 +01:00
|
|
|
l.unlock();
|
2017-09-30 08:06:23 +02:00
|
|
|
q.wait_until(tp);
|
|
|
|
}
|
2017-09-20 04:01:37 +02:00
|
|
|
|
|
|
|
assert(t != nullptr);
|
|
|
|
return *t;
|
|
|
|
}
|
|
|
|
|
2018-03-05 14:16:35 +01:00
|
|
|
template<class T,
|
|
|
|
class mutex>
|
2017-09-20 04:01:37 +02:00
|
|
|
bool
|
2018-03-05 14:16:35 +01:00
|
|
|
ircd::ctx::view<T, mutex>::ready()
|
2017-09-20 04:01:37 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return t != nullptr;
|
|
|
|
}
|