0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-12 13:01:07 +01:00

Introduce userspace contexts.

This commit is contained in:
Jason Volk 2016-09-05 08:53:36 -07:00
parent 6674648590
commit 249ba02faa
7 changed files with 271 additions and 10 deletions

View file

@ -774,7 +774,7 @@ dnl boost support
dnl
BOOST_VERSION_MIN="1.56"
BOOST_BUILT_LIBS="system,filesystem"
BOOST_BUILT_LIBS="system,filesystem,context,coroutine"
AC_MSG_CHECKING([whether you asked to use boost sources included here])
AC_ARG_WITH(included-boost,
@ -801,7 +801,7 @@ AC_HELP_STRING([--with-included-boost[[[=shared]]]], [Use the boost sources from
fi
boost_linkage="$withval"
bash tools/boostrap.sh $BTOOLSET $BOOST_BUILT_LIBS release shared single
bash tools/boostrap.sh $BTOOLSET $BOOST_BUILT_LIBS release shared multi
if [[ $? != 0 ]]; then
AC_MSG_ERROR([Failed to build included boost.])
fi
@ -837,16 +837,16 @@ AC_HELP_STRING([--with-included-boost[[[=shared]]]], [Use the boost sources from
if [[ "$boost_linkage" = "shared" ]]; then
AM_COND_IF([MINGW],
[
AC_SUBST(BOOST_LIBS, ["-lboost_filesystem-mgw53-1_61.dll -lboost_system-mgw53-1_61.dll"])
AC_SUBST(BOOST_LIBS, ["-lboost_coroutine-mgw53-1_61.dll -lboost_context-mgw53-1_61.dll -lboost_thread-mgw53-1_61.dll -lboost_filesystem-mgw53-1_61.dll -lboost_system-mgw53-1_61.dll"])
], [
AC_SUBST(BOOST_LIBS, ["-lboost_filesystem -lboost_system -lpthread"])
AC_SUBST(BOOST_LIBS, ["-lboost_coroutine -lboost_context -lboost_thread -lboost_filesystem -lboost_system -lpthread"])
])
else
AM_COND_IF([MINGW],
[
AC_SUBST(BOOST_LIBS, ["$boost_libdir/libboost_filesystem-mgw53-1_61.dll.a $boost_libdir/libboost_system-mgw53-1_61.dll.a"])
AC_SUBST(BOOST_LIBS, ["$boost_libdir/libboost_coroutine-mgw53-1_61.dll.a $boost_libdir/libboost_context-mgw53-1_61.dll.a $boost_libdir/libboost_thread-mgw53-1_61.dll.a $boost_libdir/libboost_filesystem-mgw53-1_61.dll.a $boost_libdir/libboost_system-mgw53-1_61.dll.a"])
], [
AC_SUBST(BOOST_LIBS, ["-lboost_filesystem -lboost_system -lpthread"])
AC_SUBST(BOOST_LIBS, ["-lboost_coroutine -lboost_context -lboost_thread -lboost_filesystem -lboost_system -lpthread"])
dnl AC_SUBST(BOOST_LIBS, ["$boost_libdir/libboost_filesystem.a"])
dnl AC_SUBST(BOOST_LIBS, ["$boost_libdir/libboost_system.a"])
])

74
include/ircd/ctx.h Normal file
View file

@ -0,0 +1,74 @@
/*
* charybdis: oh just a little chat server
* ctx.h: userland context switching (stackful coroutines)
*
* 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_H
#ifdef __cplusplus
namespace ircd {
namespace ctx {
IRCD_EXCEPTION(ircd::error, error)
IRCD_EXCEPTION(error, interrupted)
const auto DEFAULT_STACK_SIZE = 64_KiB;
struct ctx;
class context
{
using ptr = std::unique_ptr<ctx>;
using function = std::function<void ()>;
ptr c;
public:
void join();
ctx *detach();
context &swap(context &) noexcept;
context(const size_t &stack_size, const function &);
context(const function &);
context(context &&) noexcept = default;
context(const context &) = delete;
~context() noexcept;
};
extern __thread struct ctx *current;
void swap(context &a, context &b) noexcept;
inline
ctx &cur()
{
assert(current);
return *current;
}
} // namespace ctx
using ctx::cur;
using ctx::context;
} // namespace ircd
#endif // __cplusplus

55
include/ircd/ctx_ctx.h Normal file
View file

@ -0,0 +1,55 @@
/*
* 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.
*/
#include <boost/asio/io_service.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/spawn.hpp>
namespace ircd {
namespace ctx {
struct ctx
{
boost::asio::io_service::strand strand;
boost::asio::steady_timer alarm;
size_t stack_size;
const uint8_t *stack_base;
boost::asio::yield_context *yc;
operator boost::asio::yield_context();
void operator()(boost::asio::yield_context, const std::function<void ()>);
ctx(const size_t &stack_size = DEFAULT_STACK_SIZE,
boost::asio::io_service *const &ios = ircd::ios);
ctx(const ctx &) = delete;
~ctx() noexcept;
};
inline
ctx::operator boost::asio::yield_context()
{
return *yc;
}
} // namespace ctx
} // namespace ircd

View file

@ -33,6 +33,7 @@ namespace ircd
using std::end;
using std::get;
using ostream = std::ostream;
namespace ph = std::placeholders;
using namespace std::string_literals;
}
@ -77,6 +78,7 @@ namespace ircd
#include "mode_table.h"
#include "mode_lease.h"
#include "snomask.h"
#include "ctx.h"
#include "cache.h"
#include "whowas.h"

View file

@ -41,7 +41,8 @@ libircd_la_SOURCES = \
modules.cc \
snomask.cc \
info.cc \
cmd.cc
cmd.cc \
ctx.cc
#authproc.cc \
#bandbi.cc \

115
ircd/ctx.cc Normal file
View file

@ -0,0 +1,115 @@
/*
* 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.
*/
#include <ircd/ctx_ctx.h>
using namespace ircd;
/******************************************************************************
* ctx.h
*/
__thread ctx::ctx *ctx::current;
ircd::ctx::context::context(const size_t &stack_size,
const function &func)
:c{std::make_unique<ctx>(stack_size)}
{
static const boost::coroutines::attributes attrs
{
stack_size,
boost::coroutines::stack_unwind
};
const auto bound(std::bind(&ctx::operator(), c.get(), ph::_1, func));
boost::asio::spawn(c->strand, bound, attrs);
}
ircd::ctx::context::context(const function &func)
:context
{
DEFAULT_STACK_SIZE,
func
}
{
}
ircd::ctx::context::~context()
noexcept
{
join();
}
void
ircd::ctx::context::join()
{
}
ircd::ctx::context &
ircd::ctx::context::swap(context &other)
noexcept
{
std::swap(c, other.c);
return *this;
}
ircd::ctx::ctx *
ircd::ctx::context::detach()
{
return c.release();
}
void
ircd::ctx::swap(context &a, context &b)
noexcept
{
a.swap(b);
}
/******************************************************************************
* ctx_ctx.h
*/
ctx::ctx::ctx(const size_t &stack_size,
boost::asio::io_service *const &ios)
:strand{*ios}
,alarm{*ios}
,stack_size{stack_size}
,stack_base{nullptr}
,yc{nullptr}
{
}
ctx::ctx::~ctx()
noexcept
{
}
void
ctx::ctx::operator()(boost::asio::yield_context yc,
const std::function<void ()> func)
{
this->stack_base = reinterpret_cast<const uint8_t *>(__builtin_frame_address(0));
this->yc = &yc;
ircd::ctx::current = this;
if(likely(bool(func)))
func();
}

View file

@ -147,11 +147,25 @@ run git submodule update --init --recursive libs/function
run git submodule update --init --recursive libs/function_types
run git submodule update --init --recursive libs/iostreams
run ./bootstrap.sh --prefix=$PWD --libdir=$PWD/lib --with-libraries=$BLIBS $BSFLAGS
run ./b2 threading=$BTHREADING variant=$BVARIANT link=$BLINK address-model=64
run git submodule update --init --recursive libs/coroutine
#run git submodule update --init --recursive libs/coroutine2
## ASIO does not need coroutine2 at this time, but there is
## some issue with segmented stack support requiring inclusion
## of libs/context...
run git submodule update --init --recursive libs/context
run git submodule update --init --recursive libs/thread
run git submodule update --init --recursive libs/chrono
run git submodule update --init --recursive libs/atomic
run git submodule update --init --recursive libs/ratio
run git submodule update --init --recursive libs/intrusive
run git submodule update --init --recursive libs/tuple
run git submodule update --init --recursive libs/exception
run git submodule update --init --recursive libs/algorithm
### Install should go right into this local submodule repository
run ./b2 install
run ./bootstrap.sh --prefix=$PWD --libdir=$PWD/lib --with-libraries=$BLIBS $BSFLAGS
run ./b2 headers
run ./b2 install threading=$BTHREADING variant=$BVARIANT link=$BLINK address-model=64 warnings=all
### TODO: this shouldn't be necessary.
### XXX: required when boost submodules are fetched and built la carte, but not required