2018-12-25 02:58:57 +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.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_CTX_PARALLEL_H
|
|
|
|
|
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
|
|
|
template<class arg> class parallel;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class arg>
|
|
|
|
struct ircd::ctx::parallel
|
|
|
|
{
|
|
|
|
using closure = std::function<void (arg &)>;
|
|
|
|
|
|
|
|
pool *p {nullptr};
|
|
|
|
vector_view<arg> a;
|
|
|
|
closure c;
|
|
|
|
dock d;
|
|
|
|
std::exception_ptr eptr;
|
2018-12-27 02:12:28 +01:00
|
|
|
ushort snd {0};
|
|
|
|
ushort rcv {0};
|
|
|
|
ushort out {0};
|
|
|
|
|
2019-01-02 02:05:15 +01:00
|
|
|
void rethrow_any_exception();
|
2018-12-27 02:12:28 +01:00
|
|
|
void receiver() noexcept;
|
2019-01-02 05:39:00 +01:00
|
|
|
void sender() noexcept;
|
|
|
|
void sender(const arg &a) noexcept;
|
2018-12-25 02:58:57 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
void wait_avail();
|
|
|
|
void wait_done();
|
|
|
|
|
2018-12-27 02:12:28 +01:00
|
|
|
void operator()();
|
2018-12-25 02:58:57 +01:00
|
|
|
void operator()(const arg &a);
|
|
|
|
|
2018-12-27 02:12:28 +01:00
|
|
|
parallel(pool &, const vector_view<arg> &, closure);
|
2018-12-25 02:58:57 +01:00
|
|
|
~parallel() noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class arg>
|
|
|
|
ircd::ctx::parallel<arg>::parallel(pool &p,
|
2018-12-27 02:12:28 +01:00
|
|
|
const vector_view<arg> &a,
|
2018-12-25 02:58:57 +01:00
|
|
|
closure c)
|
|
|
|
:p{&p}
|
2018-12-27 02:12:28 +01:00
|
|
|
,a{a}
|
2018-12-25 02:58:57 +01:00
|
|
|
,c{std::move(c)}
|
|
|
|
{
|
|
|
|
p.min(this->a.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class arg>
|
|
|
|
ircd::ctx::parallel<arg>::~parallel()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
const uninterruptible::nothrow ui;
|
|
|
|
wait_done();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class arg>
|
|
|
|
void
|
|
|
|
ircd::ctx::parallel<arg>::operator()(const arg &a)
|
|
|
|
{
|
2019-01-02 02:05:15 +01:00
|
|
|
rethrow_any_exception();
|
2019-01-02 05:39:00 +01:00
|
|
|
sender(a);
|
|
|
|
wait_avail();
|
|
|
|
}
|
2019-01-02 05:29:59 +01:00
|
|
|
|
2019-01-02 05:39:00 +01:00
|
|
|
template<class arg>
|
|
|
|
void
|
|
|
|
ircd::ctx::parallel<arg>::operator()()
|
|
|
|
{
|
|
|
|
rethrow_any_exception();
|
|
|
|
sender();
|
|
|
|
wait_avail();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class arg>
|
|
|
|
void
|
|
|
|
ircd::ctx::parallel<arg>::sender(const arg &a)
|
|
|
|
noexcept
|
|
|
|
{
|
2018-12-28 00:43:34 +01:00
|
|
|
this->a.at(snd++) = a;
|
|
|
|
snd %= this->a.size();
|
2018-12-27 02:35:49 +01:00
|
|
|
out++;
|
2019-01-02 05:29:59 +01:00
|
|
|
|
|
|
|
auto &p(*this->p);
|
|
|
|
auto func(std::bind(¶llel::receiver, this));
|
|
|
|
if(likely(p.size()))
|
|
|
|
p(std::move(func));
|
|
|
|
else
|
|
|
|
func();
|
2018-12-27 02:12:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class arg>
|
|
|
|
void
|
2019-01-02 05:39:00 +01:00
|
|
|
ircd::ctx::parallel<arg>::sender()
|
|
|
|
noexcept
|
2018-12-27 02:12:28 +01:00
|
|
|
{
|
2018-12-28 00:43:34 +01:00
|
|
|
snd++;
|
|
|
|
snd %= this->a.size();
|
2018-12-27 02:35:49 +01:00
|
|
|
out++;
|
2019-01-02 05:29:59 +01:00
|
|
|
|
|
|
|
auto &p(*this->p);
|
|
|
|
auto func(std::bind(¶llel::receiver, this));
|
|
|
|
if(likely(p.size()))
|
|
|
|
p(std::move(func));
|
|
|
|
else
|
|
|
|
func();
|
2018-12-27 02:12:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class arg>
|
|
|
|
void
|
|
|
|
ircd::ctx::parallel<arg>::receiver()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
auto &a
|
2018-12-25 02:58:57 +01:00
|
|
|
{
|
2018-12-27 02:12:28 +01:00
|
|
|
this->a.at(rcv++ % this->a.size())
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!this->eptr) try
|
|
|
|
{
|
|
|
|
c(a);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
this->eptr = std::current_exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
out--;
|
|
|
|
d.notify_one();
|
2018-12-25 02:58:57 +01:00
|
|
|
}
|
|
|
|
|
2019-01-02 02:05:15 +01:00
|
|
|
template<class arg>
|
|
|
|
void
|
|
|
|
ircd::ctx::parallel<arg>::rethrow_any_exception()
|
|
|
|
{
|
|
|
|
if(likely(!this->eptr))
|
|
|
|
return;
|
|
|
|
|
|
|
|
wait_done();
|
|
|
|
const auto eptr(this->eptr);
|
|
|
|
this->eptr = {};
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
}
|
|
|
|
|
2018-12-25 02:58:57 +01:00
|
|
|
template<class arg>
|
|
|
|
void
|
|
|
|
ircd::ctx::parallel<arg>::wait_avail()
|
|
|
|
{
|
|
|
|
d.wait([this]
|
|
|
|
{
|
|
|
|
return out < a.size();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class arg>
|
|
|
|
void
|
|
|
|
ircd::ctx::parallel<arg>::wait_done()
|
|
|
|
{
|
|
|
|
d.wait([this]
|
|
|
|
{
|
|
|
|
return !out;
|
|
|
|
});
|
|
|
|
}
|