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.
|
2018-03-23 05:58:06 +01:00
|
|
|
/// The consumers lock the mutex before passing it to the call to wait().
|
|
|
|
/// wait() returns with a view of the object under lock. Once the consumer
|
|
|
|
/// releases their 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
|
|
|
{
|
2017-09-30 08:06:23 +02:00
|
|
|
dock q;
|
2018-03-27 10:34:35 +02:00
|
|
|
T *t {nullptr};
|
|
|
|
size_t wanting {0};
|
2017-09-30 08:06:23 +02:00
|
|
|
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-27 09:52:02 +02:00
|
|
|
void operator()(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);
|
2018-03-27 10:34:35 +02:00
|
|
|
assert(!wanting);
|
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-27 09:52:02 +02:00
|
|
|
ircd::ctx::view<T, mutex>::operator()(T &t)
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
2018-03-27 10:34:35 +02:00
|
|
|
const auto produce{[this]
|
|
|
|
(T *const &t, size_t &semaphore)
|
|
|
|
{
|
|
|
|
q.wait([this, &semaphore]
|
|
|
|
{
|
|
|
|
return semaphore == 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
const std::lock_guard<view> lock{*this};
|
|
|
|
this->t = t;
|
|
|
|
q.notify_all();
|
|
|
|
}};
|
|
|
|
|
2018-08-29 03:57:20 +02:00
|
|
|
const uninterruptible ui;
|
2018-03-27 10:34:35 +02:00
|
|
|
produce(&t, wanting);
|
|
|
|
produce(nullptr, waiting);
|
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-27 10:34:35 +02:00
|
|
|
const auto consume{[this, &l]
|
|
|
|
(const bool &r, size_t &semaphore)
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
2018-03-27 10:34:35 +02:00
|
|
|
const unwind uw{[this, &l, &semaphore]
|
|
|
|
{
|
|
|
|
--semaphore;
|
|
|
|
q.notify_all();
|
|
|
|
}};
|
|
|
|
|
|
|
|
++semaphore;
|
|
|
|
assert(l.owns_lock());
|
|
|
|
const unlock_guard<lock> ul{l};
|
|
|
|
q.wait([this, &r]
|
|
|
|
{
|
|
|
|
return ready() == r;
|
|
|
|
});
|
2017-09-30 08:06:23 +02:00
|
|
|
}};
|
|
|
|
|
2018-03-27 10:34:35 +02:00
|
|
|
consume(false, wanting);
|
|
|
|
consume(true, waiting);
|
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
|
|
|
{
|
2019-09-23 05:02:23 +02:00
|
|
|
return wait_until(l, now<system_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-27 10:34:35 +02:00
|
|
|
const auto consume{[this, &l, &tp]
|
|
|
|
(const bool &r, size_t &semaphore)
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
2018-03-27 10:34:35 +02:00
|
|
|
const unwind uw{[this, &l, &semaphore]
|
|
|
|
{
|
|
|
|
--semaphore;
|
|
|
|
q.notify_all();
|
|
|
|
}};
|
|
|
|
|
|
|
|
++semaphore;
|
|
|
|
assert(l.owns_lock());
|
|
|
|
const unlock_guard<lock> ul{l};
|
|
|
|
if(!q.wait_until(tp, [this, &r]
|
|
|
|
{
|
|
|
|
return ready() == r;
|
|
|
|
}))
|
2018-03-27 01:53:24 +02:00
|
|
|
throw timeout{};
|
2017-09-30 08:06:23 +02:00
|
|
|
}};
|
|
|
|
|
2018-03-27 10:34:35 +02:00
|
|
|
consume(false, wanting);
|
|
|
|
consume(true, waiting);
|
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;
|
|
|
|
}
|