From 2f645941a864e1fe44b63a3e82a518b61305fcfe Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 20 Oct 2016 16:56:16 -0700 Subject: [PATCH] ircd::ctx: Move ctx headers into directory. --- charybdis/charybdis.cc | 2 +- include/ircd/ctx.h | 92 +++++++++---------- include/ircd/{ctx_async.h => ctx/async.h} | 0 include/ircd/ctx/context.h | 68 ++++++++++++++ include/ircd/{ctx_ctx.h => ctx/ctx.h} | 0 include/ircd/{ctx_dock.h => ctx/dock.h} | 0 include/ircd/{ctx_future.h => ctx/future.h} | 0 include/ircd/{ctx_mutex.h => ctx/mutex.h} | 0 include/ircd/{ctx_ole.h => ctx/ole.h} | 0 include/ircd/{ctx_pool.h => ctx/pool.h} | 0 include/ircd/{ctx_prof.h => ctx/prof.h} | 0 include/ircd/{ctx_promise.h => ctx/promise.h} | 0 .../shared_state.h} | 0 include/ircd/sock.h | 2 +- include/ircd/stdinc.h | 9 -- ircd/ctx.cc | 2 +- ircd/ircd.cc | 2 +- modules/conf/listen.cc | 2 +- modules/m_host.cc | 2 +- 19 files changed, 116 insertions(+), 65 deletions(-) rename include/ircd/{ctx_async.h => ctx/async.h} (100%) create mode 100644 include/ircd/ctx/context.h rename include/ircd/{ctx_ctx.h => ctx/ctx.h} (100%) rename include/ircd/{ctx_dock.h => ctx/dock.h} (100%) rename include/ircd/{ctx_future.h => ctx/future.h} (100%) rename include/ircd/{ctx_mutex.h => ctx/mutex.h} (100%) rename include/ircd/{ctx_ole.h => ctx/ole.h} (100%) rename include/ircd/{ctx_pool.h => ctx/pool.h} (100%) rename include/ircd/{ctx_prof.h => ctx/prof.h} (100%) rename include/ircd/{ctx_promise.h => ctx/promise.h} (100%) rename include/ircd/{ctx_shared_state.h => ctx/shared_state.h} (100%) diff --git a/charybdis/charybdis.cc b/charybdis/charybdis.cc index cae596d52..f8b8db2b7 100644 --- a/charybdis/charybdis.cc +++ b/charybdis/charybdis.cc @@ -21,7 +21,7 @@ #include #include -#include +#include #include "lgetopt.h" namespace path = ircd::path; diff --git a/include/ircd/ctx.h b/include/ircd/ctx.h index c646f5e19..46470972e 100644 --- a/include/ircd/ctx.h +++ b/include/ircd/ctx.h @@ -25,7 +25,23 @@ #pragma once #define HAVE_IRCD_CTX_H -#ifdef __cplusplus +// +// This is the public interface to the userspace context system. No 3rd party +// symbols are included from here. This file is included automatically in stdinc.h +// and you do not have to include it manually. +// +// There are two primary objects at work in the context system: +// +// `struct context` +// Public interface emulating std::thread; included automatically from here. +// To spawn and manipulate contexts, deal with this object. +// +// `struct ctx` +// Internal implementation of the context and state holder. This is not included here. +// Several low-level functions are exposed for library creators. This file is usually +// included when boost/asio.hpp is also included and calls are actually made into boost. +// + namespace ircd { namespace ctx { @@ -80,71 +96,55 @@ void sleep_until(const time_point &tp); template void sleep(const duration &); void sleep(const int &secs); -class context -{ - std::unique_ptr c; +} // namespace ctx - public: - bool operator!() const { return !c; } - operator bool() const { return bool(c); } +using ctx::timeout; - operator const ctx &() const { return *c; } - operator ctx &() { return *c; } +} // namespace ircd - bool joined() const { return !c || ircd::ctx::finished(*c); } - void interrupt() { ircd::ctx::interrupt(*c); } - void join(); // Blocks the current context until this one finishes - ctx *detach(); // other calls undefined after this call - - // Note: Constructing with SELF_DESTRUCT flag makes any further use of this object undefined. - context(const size_t &stack_size, std::function, const enum flags &flags = (enum flags)0); - context(std::function, const enum flags &flags = (enum flags)0); - context(context &&) noexcept = default; - context(const context &) = delete; - ~context() noexcept; - - friend void swap(context &a, context &b) noexcept; -}; +#include "ctx/context.h" +#include "ctx/prof.h" +#include "ctx/dock.h" +#include "ctx/mutex.h" +#include "ctx/shared_state.h" +#include "ctx/promise.h" +#include "ctx/future.h" +#include "ctx/async.h" +#include "ctx/pool.h" +#include "ctx/ole.h" inline void -swap(context &a, context &b) -noexcept -{ - std::swap(a.c, b.c); -} - -inline void -sleep(const int &secs) +ircd::ctx::sleep(const int &secs) { sleep(seconds(secs)); } template void -sleep(const duration &d) +ircd::ctx::sleep(const duration &d) { sleep_until(steady_clock::now() + d); } template -throw_overload -wait_until(const time_point &tp) +ircd::throw_overload +ircd::ctx::wait_until(const time_point &tp) { if(wait_until(tp)) throw E(); } template -nothrow_overload -wait_until(const time_point &tp) +ircd::nothrow_overload +ircd::ctx::wait_until(const time_point &tp) { return wait_until(tp, std::nothrow); } template -throw_overload -wait(const duration &d) +ircd::throw_overload +ircd::ctx::wait(const duration &d) { const auto ret(wait(d)); return ret <= duration(0)? throw E() : ret; @@ -152,8 +152,8 @@ wait(const duration &d) template -nothrow_overload -wait(const duration &d) +ircd::nothrow_overload +ircd::ctx::wait(const duration &d) { using std::chrono::duration_cast; @@ -161,17 +161,9 @@ wait(const duration &d) return duration_cast(ret); } -inline ctx & -cur() +inline ircd::ctx::ctx & +ircd::ctx::cur() { assert(current); return *current; } - -} // namespace ctx - -using ctx::context; -using ctx::timeout; - -} // namespace ircd -#endif // __cplusplus diff --git a/include/ircd/ctx_async.h b/include/ircd/ctx/async.h similarity index 100% rename from include/ircd/ctx_async.h rename to include/ircd/ctx/async.h diff --git a/include/ircd/ctx/context.h b/include/ircd/ctx/context.h new file mode 100644 index 000000000..90d5804f0 --- /dev/null +++ b/include/ircd/ctx/context.h @@ -0,0 +1,68 @@ +/* + * 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 + * + * 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_CONTEXT_H + +namespace ircd { +namespace ctx { + +class context +{ + std::unique_ptr c; + + public: + bool operator!() const { return !c; } + operator bool() const { return bool(c); } + + operator const ctx &() const { return *c; } + operator ctx &() { return *c; } + + bool joined() const { return !c || ircd::ctx::finished(*c); } + void interrupt() { ircd::ctx::interrupt(*c); } + void join(); // Blocks the current context until this one finishes + ctx *detach(); // other calls undefined after this call + + // Note: Constructing with SELF_DESTRUCT flag makes any further use of this object undefined. + context(const size_t &stack_size, std::function, const enum flags &flags = (enum flags)0); + context(std::function, const enum flags &flags = (enum flags)0); + context(context &&) noexcept = default; + context(const context &) = delete; + ~context() noexcept; + + friend void swap(context &a, context &b) noexcept; +}; + +inline void +swap(context &a, context &b) +noexcept +{ + std::swap(a.c, b.c); +} + +} // namespace ctx + +using ctx::context; + +} // namespace ircd diff --git a/include/ircd/ctx_ctx.h b/include/ircd/ctx/ctx.h similarity index 100% rename from include/ircd/ctx_ctx.h rename to include/ircd/ctx/ctx.h diff --git a/include/ircd/ctx_dock.h b/include/ircd/ctx/dock.h similarity index 100% rename from include/ircd/ctx_dock.h rename to include/ircd/ctx/dock.h diff --git a/include/ircd/ctx_future.h b/include/ircd/ctx/future.h similarity index 100% rename from include/ircd/ctx_future.h rename to include/ircd/ctx/future.h diff --git a/include/ircd/ctx_mutex.h b/include/ircd/ctx/mutex.h similarity index 100% rename from include/ircd/ctx_mutex.h rename to include/ircd/ctx/mutex.h diff --git a/include/ircd/ctx_ole.h b/include/ircd/ctx/ole.h similarity index 100% rename from include/ircd/ctx_ole.h rename to include/ircd/ctx/ole.h diff --git a/include/ircd/ctx_pool.h b/include/ircd/ctx/pool.h similarity index 100% rename from include/ircd/ctx_pool.h rename to include/ircd/ctx/pool.h diff --git a/include/ircd/ctx_prof.h b/include/ircd/ctx/prof.h similarity index 100% rename from include/ircd/ctx_prof.h rename to include/ircd/ctx/prof.h diff --git a/include/ircd/ctx_promise.h b/include/ircd/ctx/promise.h similarity index 100% rename from include/ircd/ctx_promise.h rename to include/ircd/ctx/promise.h diff --git a/include/ircd/ctx_shared_state.h b/include/ircd/ctx/shared_state.h similarity index 100% rename from include/ircd/ctx_shared_state.h rename to include/ircd/ctx/shared_state.h diff --git a/include/ircd/sock.h b/include/ircd/sock.h index 11ca35593..abee58340 100644 --- a/include/ircd/sock.h +++ b/include/ircd/sock.h @@ -26,7 +26,7 @@ #include #include #include "bufs.h" -#include "ctx_ctx.h" +#include "ctx/ctx.h" namespace ircd { diff --git a/include/ircd/stdinc.h b/include/ircd/stdinc.h index 1ed7c2264..b9197de29 100644 --- a/include/ircd/stdinc.h +++ b/include/ircd/stdinc.h @@ -91,15 +91,6 @@ namespace ircd #include "mode_lease.h" #include "snomask.h" #include "ctx.h" -#include "ctx_prof.h" -#include "ctx_dock.h" -#include "ctx_mutex.h" -#include "ctx_shared_state.h" -#include "ctx_promise.h" -#include "ctx_future.h" -#include "ctx_async.h" -#include "ctx_pool.h" -#include "ctx_ole.h" #include "hook.h" #include "line.h" #include "tape.h" diff --git a/ircd/ctx.cc b/ircd/ctx.cc index 3a51ce377..1fdae3f19 100644 --- a/ircd/ctx.cc +++ b/ircd/ctx.cc @@ -19,7 +19,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include +#include using namespace ircd; diff --git a/ircd/ircd.cc b/ircd/ircd.cc index fe4478d9c..67da87b50 100644 --- a/ircd/ircd.cc +++ b/ircd/ircd.cc @@ -24,7 +24,7 @@ */ #include -#include +#include #include namespace ircd diff --git a/modules/conf/listen.cc b/modules/conf/listen.cc index 97af202fd..267637663 100644 --- a/modules/conf/listen.cc +++ b/modules/conf/listen.cc @@ -20,7 +20,7 @@ */ #include -#include +#include #include #include diff --git a/modules/m_host.cc b/modules/m_host.cc index 791bdc7dc..03d5583a4 100644 --- a/modules/m_host.cc +++ b/modules/m_host.cc @@ -20,7 +20,7 @@ */ #include -#include +#include namespace ip = boost::asio::ip; using namespace ircd;