0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-02 18:18:56 +02:00

ircd::ctx: Add context pool.

This commit is contained in:
Jason Volk 2016-09-21 14:15:49 -07:00
parent 5e8c4bb2a1
commit f7f9d87df0
3 changed files with 152 additions and 0 deletions

57
include/ircd/ctx_pool.h Normal file
View file

@ -0,0 +1,57 @@
/*
* Copyright (C) 2016 Charybdis Development Team
* Copyright (C) 2016 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#define HAVE_IRCD_CTX_POOL_H
namespace ircd {
namespace ctx {
struct pool
{
using closure = std::function<void ()>;
private:
size_t stack_size;
size_t available;
struct dock dock;
std::deque<closure> queue;
std::vector<context> ctxs;
void next();
void main();
public:
auto size() const { return ctxs.size(); }
auto avail() const { return available; }
auto queued() const { return queue.size(); }
void add(const size_t & = 1);
void del(const size_t & = 1);
void operator()(closure);
pool(const size_t & = 0, const size_t &stack_size = DEFAULT_STACK_SIZE);
~pool() noexcept;
};
} // namespace ctx
} // namespace ircd

View file

@ -96,6 +96,7 @@ namespace ircd
#include "ctx_promise.h"
#include "ctx_future.h"
#include "ctx_async.h"
#include "ctx_pool.h"
#include "line.h"
#include "tape.h"

View file

@ -232,3 +232,97 @@ ctx::stack_usage_here(const ctx &ctx)
{
return ctx.stack_base - uintptr_t(__builtin_frame_address(0));
}
///////////////////////////////////////////////////////////////////////////////
//
// ctx_pool.h
//
ctx::pool::pool(const size_t &size,
const size_t &stack_size)
:stack_size{stack_size}
,available{0}
{
add(size);
}
ctx::pool::~pool()
noexcept
{
del(size());
}
void
ctx::pool::operator()(closure closure)
{
queue.emplace_back(std::move(closure));
dock.notify_one();
}
void
ctx::pool::del(const size_t &num)
{
const ssize_t requested(size() - num);
const size_t target(std::max(requested, ssize_t(0)));
while(ctxs.size() > target)
ctxs.pop_back();
}
void
ctx::pool::add(const size_t &num)
{
for(size_t i(0); i < num; ++i)
ctxs.emplace_back(stack_size, std::bind(&pool::main, this), DEFER_POST);
}
void
ctx::pool::main()
try
{
++available;
const scope avail([this]
{
--available;
});
while(1)
next();
}
catch(const interrupted &e)
{
log::debug("pool(%p) ctx(%p): %s",
this,
&cur(),
e.what());
}
void
ctx::pool::next()
try
{
dock.wait([this]
{
return !queue.empty();
});
--available;
const scope avail([this]
{
++available;
});
const auto func(std::move(queue.front()));
queue.pop_front();
func();
}
catch(const interrupted &e)
{
throw;
}
catch(const std::exception &e)
{
log::critical("pool(%p) ctx(%p): unhandled: %s",
this,
&cur(),
e.what());
}