0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-04 11:08:55 +02:00

ircd: Move runlevel related into ircd::run:: namespace.

This commit is contained in:
Jason Volk 2019-01-18 08:55:06 -08:00
parent 8f4fe97286
commit d639eceba2
13 changed files with 194 additions and 193 deletions

View file

@ -431,13 +431,12 @@ construct::console::next_command()
}
void
construct::console::on_runlevel(const enum ircd::runlevel &runlevel)
construct::console::on_runlevel(const enum ircd::run::level &runlevel)
{
switch(runlevel)
{
case ircd::runlevel::QUIT:
case ircd::runlevel::HALT:
case ircd::runlevel::FAULT:
case ircd::run::level::QUIT:
case ircd::run::level::HALT:
console::terminate();
break;
@ -450,14 +449,14 @@ bool
construct::console::wait_running()
const
{
ircd::runlevel_changed::dock.wait([]
ircd::run::changed::dock.wait([]
{
return ircd::runlevel == ircd::runlevel::RUN ||
ircd::runlevel == ircd::runlevel::QUIT ||
ircd::runlevel == ircd::runlevel::HALT;
return ircd::run::level == ircd::run::level::RUN ||
ircd::run::level == ircd::run::level::QUIT ||
ircd::run::level == ircd::run::level::HALT;
});
return ircd::runlevel == ircd::runlevel::RUN;
return ircd::run::level == ircd::run::level::RUN;
}
void

View file

@ -19,11 +19,11 @@ namespace construct
struct construct::signals
{
std::unique_ptr<boost::asio::signal_set> signal_set;
ircd::runlevel_changed runlevel_changed;
ircd::run::changed runlevel_changed;
void set_handle();
void on_signal(const boost::system::error_code &, int) noexcept;
void on_runlevel(const enum ircd::runlevel &);
void on_runlevel(const enum ircd::run::level &);
public:
signals(boost::asio::io_context &ios);
@ -45,10 +45,10 @@ struct construct::console
std::string record_path;
ircd::module *module {nullptr};
ircd::context context;
ircd::runlevel_changed runlevel_changed;
ircd::run::changed runlevel_changed;
void show_message() const;
void on_runlevel(const enum ircd::runlevel &);
void on_runlevel(const enum ircd::run::level &);
bool wait_running() const;
bool next_command();
void wait_input();

View file

@ -46,12 +46,11 @@ construct::signals::signals(boost::asio::io_context &ios)
// won't return even if we call ircd::quit(). We use this callback to
// cancel the signal handlers so run() can return and the program can exit.
void
construct::signals::on_runlevel(const enum ircd::runlevel &runlevel)
construct::signals::on_runlevel(const enum ircd::run::level &level)
{
switch(runlevel)
switch(level)
{
case ircd::runlevel::HALT:
case ircd::runlevel::FAULT:
case ircd::run::level::HALT:
signal_set->cancel();
break;
@ -85,7 +84,7 @@ noexcept
handle_signal(signum);
switch(ircd::runlevel)
switch(ircd::run::level)
{
// Reinstall handler for next signal
default:
@ -93,8 +92,7 @@ noexcept
break;
// No reinstall of handler.
case ircd::runlevel::QUIT:
case ircd::runlevel::FAULT:
case ircd::run::level::HALT:
break;
}
}
@ -186,14 +184,14 @@ construct::handle_usr1()
try
{
// Spawning the context that follows this branch and doing a rehash
// when not in a stable state like runlevel::RUN will just make a mess
// when not in a stable state like run::level::RUN will just make a mess
// so any signal received is just dropped and the user can try again.
if(ircd::runlevel != ircd::runlevel::RUN)
if(ircd::run::level != ircd::run::level::RUN)
{
ircd::log::warning
{
"Not rehashing conf from SIGUSR1 in runlevel %s",
reflect(ircd::runlevel)
reflect(ircd::run::level)
};
return;

View file

@ -20,7 +20,7 @@
#include "allocator.h"
#include "util/util.h"
#include "exception.h"
#include "runlevel.h"
#include "run.h"
#include "fpe.h"
#include "demangle.h"
#include "localee.h"

90
include/ircd/run.h Normal file
View file

@ -0,0 +1,90 @@
// 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.
#define HAVE_IRCD_RUN_H
// Forward declarations for because ctx.h is included after runlevel.h.
namespace ircd::ctx
{
struct dock;
}
namespace ircd::run
{
enum class level :int;
struct changed;
extern const enum level &level;
string_view reflect(const enum level &);
bool set(const enum level &);
}
/// An instance of this class registers itself to be called back when
/// the ircd::run::level has changed.
///
/// Note: Its destructor will access a list in libircd; after a callback
/// for a HALT do not unload libircd.so until destructing this object.
///
/// A static ctx::dock is also available for contexts to wait for a run::level
/// change notification.
///
struct ircd::run::changed
:instance_list<ircd::run::changed>
,std::function<void (const enum level &)>
{
using handler = std::function<void (const enum level &)>;
// Users on an ircd::ctx who wish to use the dock interface to wait for
// a run::level change can directly access this static instance.
static ctx::dock dock;
/// The handler function will be called back for any run::level change while
/// this instance remains in scope.
changed(handler function);
~changed() noexcept;
};
/// The run::level allows all observers to know the coarse state of IRCd and to
/// react accordingly. This can be used by the embedder of libircd to know
/// when it's safe to use or delete libircd resources. It is also used
/// similarly by the library and its modules. Only one runlevel is active at
/// any time.
///
/// * HALT is the off mode. Nothing is/will be running in libircd until
/// an invocation of ircd::init();
///
/// * READY is the state after calling ircd::init(). Leaving READY is done with
/// the user either calling their ios.run() to start executing tasks or calling
/// ircd::quit() to HALT again.
///
/// * START indicates the daemon is executing its startup procedures. Leaving
/// the START state occurs internally when there is success or a fatal error.
///
/// * RUN is the service mode. Full client and application functionality exists
/// in this mode. Leaving the RUN mode is done with ircd::quit();
///
/// * QUIT indicates the daemon is executing the shutdown procedures. This
/// will eventually return back to the HALT state.
///
/// * FAULT is a special mode indicating something really bad. The exact
/// details of this mode are ambiguous. Users do not have to handle this.
///
enum class ircd::run::level
:int
{
HALT = 0, ///< x <-- IRCd Powered off.
READY = 1, ///< | | Ready for user to run ios event loop.
START = 2, ///< | | Starting up subsystems for service.
RUN = 3, ///< O | IRCd in service.
QUIT = 4, ///< --> ^ Clean shutdown in progress.
FAULT = -1, ///< Unrecoverable fault.
};

View file

@ -1,81 +0,0 @@
// 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.
#define HAVE_IRCD_IRCD_RUNLEVEL_H
namespace ircd
{
enum class runlevel :int;
struct runlevel_changed;
extern const enum runlevel &runlevel;
string_view reflect(const enum runlevel &);
bool runlevel_set(const enum runlevel &);
}
// Forward declarations for because ctx.h is included after runlevel.h.
namespace ircd::ctx
{
struct dock;
}
/// An instance of this class registers itself to be called back when
/// the ircd::runlevel has changed.
///
/// Note: Its destructor will access a list in libircd; after a callback
/// for a HALT do not unload libircd.so until destructing this object.
///
/// A static ctx::dock is also available for contexts to wait for a runlevel
/// change notification.
///
struct ircd::runlevel_changed
:instance_list<ircd::runlevel_changed>
,std::function<void (const enum runlevel &)>
{
using handler = std::function<void (const enum runlevel &)>;
// Users on an ircd::ctx who wish to use the dock interface to wait for
// a runlevel change can directly access this static instance.
static ctx::dock dock;
/// The handler function will be called back for any runlevel change while
/// this instance remains in scope.
runlevel_changed(handler function);
~runlevel_changed() noexcept;
};
/// The runlevel allows all observers to know the coarse state of IRCd and to
/// react accordingly. This can be used by the embedder of libircd to know
/// when it's safe to use or delete libircd resources. It is also used
/// similarly by the library and its modules.
///
/// Primary modes:
///
/// * HALT is the off mode. Nothing is/will be running in libircd until
/// an invocation of ircd::init();
///
/// * RUN is the service mode. Full client and application functionality
/// exists in this mode. Leaving the RUN mode is done with ircd::quit();
///
/// - Transitional modes: Modes which are working towards the next mode.
/// - Interventional modes: Modes which are *not* working towards the next
/// mode and may require some user action to continue.
///
enum class ircd::runlevel
:int
{
HALT = 0, ///< [inter] IRCd Powered off.
READY = 1, ///< [inter] Ready for user to run ios event loop.
START = 2, ///< [trans] Starting up subsystems for service.
RUN = 3, ///< [inter] IRCd in service.
QUIT = 4, ///< [trans] Clean shutdown in progress
FAULT = -1, ///< [trans] QUIT with exception (dirty shutdown)
};

View file

@ -449,13 +449,13 @@ ircd::handle_ec(client &client,
using boost::asio::error::get_ssl_category;
using boost::asio::error::get_misc_category;
if(unlikely(runlevel != runlevel::RUN && !ec))
if(unlikely(run::level != run::level::RUN && !ec))
{
log::dwarning
{
client::log, "%s refusing client request in runlevel %s",
client.loghead(),
reflect(runlevel)
reflect(run::level)
};
client.close(net::dc::RST, net::close_ignore);

View file

@ -408,10 +408,12 @@ ircd::ctx::terminate(ctx &ctx)
/// Marks `ctx` for interruption and enqueues it for resumption to receive the
/// interrupt which will be an exception coming out of the point where the
/// `ctx` was yielding.
///
/// NOTE: If the IRCd run::level is QUIT, an interrupt() becomes a terminate().
void
ircd::ctx::interrupt(ctx &ctx)
{
if(unlikely(ircd::runlevel == runlevel::QUIT))
if(unlikely(run::level == run::level::QUIT))
return terminate(ctx);
if(finished(ctx))

View file

@ -7679,9 +7679,9 @@ ircd::db::database::env::state::pool::operator()(task &&task)
return;
// Don't start a background task before RUN.
runlevel_changed::dock.wait([]
run::changed::dock.wait([]
{
return runlevel == runlevel::RUN;
return run::level == run::level::RUN;
});
const ctx::uninterruptible::nothrow ui;

View file

@ -33,17 +33,17 @@ ircd::debugmode
/// This function will setup the main program loop of libircd. The execution will
/// occur when your io_context.run() or poll() is further invoked.
///
/// init() can only be called from a runlevel::HALT state
/// init() can only be called from a run::level::HALT state
void
ircd::init(boost::asio::io_context &user_ios,
const string_view &origin,
const string_view &hostname)
try
{
if(runlevel != runlevel::HALT)
if(run::level != run::level::HALT)
throw error
{
"Cannot init() IRCd from runlevel %s", reflect(runlevel)
"Cannot init() IRCd from runlevel %s", reflect(run::level)
};
ios::init(user_ios);
@ -75,7 +75,7 @@ try
// from assertions that it's not blocking the process with excessive CPU
// usage or long syscall. Main context can't meet this requirement.
//
ircd::context main_context
context main_context
{
"main", 256_KiB, &ircd::main, context::POST | context::SLICE_EXEMPT
};
@ -87,10 +87,10 @@ try
// this must be manually deleted with assurance that mc will never enter.
ircd::main_context = main_context.detach();
// Finally, without prior exception, the commitment to runlevel::READY
// Finally, without prior exception, the commitment to run::level::READY
// is made here. The user can now invoke their ios.run(), or, if they
// have already, IRCd will begin main execution shortly...
ircd::runlevel_set(runlevel::READY);
run::set(run::level::READY);
}
catch(const std::exception &e)
{
@ -115,39 +115,39 @@ noexcept
log::debug
{
"IRCd quit requested from runlevel:%s ctx:%p main_context:%p",
reflect(runlevel),
reflect(run::level),
(const void *)ctx::current,
(const void *)main_context
};
if(main_context) switch(runlevel)
if(main_context) switch(run::level)
{
case runlevel::READY:
case run::level::READY:
{
ctx::terminate(*main_context);
main_context = nullptr;
ircd::runlevel_set(runlevel::HALT);
ircd::run::set(run::level::HALT);
return true;
}
case runlevel::START:
case run::level::START:
{
ctx::terminate(*main_context);
main_context = nullptr;
ircd::runlevel_set(runlevel::QUIT);
ircd::run::set(run::level::QUIT);
return true;
}
case runlevel::RUN:
case run::level::RUN:
{
ctx::notify(*main_context);
main_context = nullptr;
return true;
}
case runlevel::HALT:
case runlevel::QUIT:
case runlevel::FAULT:
case run::level::HALT:
case run::level::QUIT:
case run::level::FAULT:
return false;
}
@ -163,7 +163,7 @@ noexcept
/// of the subsystem, so destruction will shut down that subsystem.
///
/// The status of this function and IRCd overall can be observed through
/// the ircd::runlevel. The ircd::runlevel_changed callback can be set
/// the ircd::runlevel. The ircd::run::changed callback can be set
/// to be notified on a runlevel change. The user should wait for a runlevel
/// of HALT before destroying IRCd related resources and stopping their
/// io_context from running more jobs.
@ -177,16 +177,16 @@ noexcept try
// threads, but we consider this one thread a main thread for now...
ircd::ios::main_thread_id = std::this_thread::get_id();
// When this function completes, subsystems are done shutting down and IRCd
// When this function completes without exception, subsystems are done shutting down and IRCd
// transitions to HALT.
const unwind halted{[]
{
runlevel_set(runlevel::HALT);
run::set(run::level::HALT);
}};
// When this function is entered IRCd will transition to START indicating
// that subsystems are initializing.
ircd::runlevel_set(runlevel::START);
run::set(run::level::START);
// These objects are the init()'s and fini()'s for each subsystem.
// Appearing here ties their life to the main context. Initialization can
@ -221,20 +221,17 @@ noexcept try
client::wait_all();
}};
// When the call to wait() below completes, IRCd exits from the RUN state.
const unwind nominal
{
std::bind(&ircd::runlevel_set, runlevel::QUIT)
};
// IRCd will now transition to the RUN state indicating full functionality.
ircd::runlevel_set(runlevel::RUN);
run::set(run::level::RUN);
// This call blocks until the main context is notified or interrupted etc.
// Waiting here will hold open this stack with all of the above objects
// living on it. Once this call completes this function effectively
// executes backwards from this point and shuts down IRCd.
// living on it.
ctx::wait();
// Once this call completes this main stack unwinds from this point and
// shuts down IRCd.
run::set(run::level::QUIT);
}
catch(const http::error &e) // <-- m::error
{
@ -271,37 +268,37 @@ ircd::uptime()
///////////////////////////////////////////////////////////////////////////////
//
// ircd/runlevel.h
// ircd/run.h
//
namespace ircd
namespace ircd::run
{
static enum runlevel _runlevel;
static enum level _level;
}
decltype(ircd::runlevel)
ircd::runlevel
decltype(ircd::run::level)
ircd::run::level
{
_runlevel
_level
};
//
// runlevel_changed
// run::changed
//
template<>
decltype(ircd::runlevel_changed::list)
ircd::util::instance_list<ircd::runlevel_changed>::list
decltype(ircd::run::changed::list)
ircd::util::instance_list<ircd::run::changed>::list
{};
decltype(ircd::runlevel_changed::dock)
ircd::runlevel_changed::dock;
decltype(ircd::run::changed::dock)
ircd::run::changed::dock;
//
// runlevel_changed::runlevel_changed
// run::changed::changed
//
ircd::runlevel_changed::runlevel_changed(handler function)
ircd::run::changed::changed(handler function)
:handler
{
std::move(function)
@ -309,39 +306,35 @@ ircd::runlevel_changed::runlevel_changed(handler function)
{
}
ircd::runlevel_changed::~runlevel_changed()
ircd::run::changed::~changed()
noexcept
{
}
/// Sets the runlevel of IRCd and notifies users. This should never be called
/// manually/directly, as it doesn't trigger a runlevel change itself, it just
/// notifies of one.
///
/// The notification will be posted to the io_context. This is important to
/// prevent the callback from continuing execution on some ircd::ctx stack and
/// instead invoke their function on the main stack in their own io_context
/// event slice.
bool
ircd::runlevel_set(const enum runlevel &new_runlevel)
ircd::run::set(const enum level &new_level)
try
{
if(runlevel == new_runlevel)
if(level == new_level)
return false;
log::debug
{
"IRCd runlevel transition from '%s' to '%s' (notifying %zu)",
reflect(runlevel),
reflect(new_runlevel),
runlevel_changed::list.size()
"IRCd level transition from '%s' to '%s' (notifying %zu)",
reflect(level),
reflect(new_level),
changed::list.size()
};
_runlevel = new_runlevel;
runlevel_changed::dock.notify_all();
_level = new_level;
changed::dock.notify_all();
// This latch is used to block this call when setting the runlevel
// from an ircd::ctx. If the runlevel is set from the main stack then
// This latch is used to block this call when setting the level
// from an ircd::ctx. If the level is set from the main stack then
// the caller will have to do synchronization themselves.
ctx::latch latch
{
@ -351,28 +344,28 @@ try
// This function will notify the user of the change to IRCd. When there
// are listeners, function is posted to the io_context ensuring THERE IS
// NO CONTINUATION ON THIS STACK by the user.
const auto call_users{[new_runlevel, &latch, latching(!latch.is_ready())]
const auto call_users{[new_level, &latch, latching(!latch.is_ready())]
{
assert(new_runlevel == ircd::runlevel);
assert(new_level == run::level);
log::notice
{
"IRCd %s", reflect(new_runlevel)
"IRCd %s", reflect(new_level)
};
if(new_runlevel == runlevel::HALT)
if(new_level == level::HALT)
log::fini();
else
log::flush();
for(const auto &handler : runlevel_changed::list)
(*handler)(new_runlevel);
for(const auto &handler : changed::list)
(*handler)(new_level);
if(latching)
latch.count_down();
}};
if(runlevel_changed::list.size())
if(changed::list.size())
ircd::post(call_users);
else
call_users();
@ -386,8 +379,8 @@ catch(const std::exception &e)
{
log::critical
{
"IRCd runlevel change to '%s': %s",
reflect(new_runlevel),
"IRCd level change to '%s': %s",
reflect(new_level),
e.what()
};
@ -396,16 +389,16 @@ catch(const std::exception &e)
}
ircd::string_view
ircd::reflect(const enum runlevel &level)
ircd::run::reflect(const enum run::level &level)
{
switch(level)
{
case runlevel::HALT: return "HALT";
case runlevel::READY: return "READY";
case runlevel::START: return "START";
case runlevel::RUN: return "RUN";
case runlevel::QUIT: return "QUIT";
case runlevel::FAULT: return "FAULT";
case level::HALT: return "HALT";
case level::READY: return "READY";
case level::START: return "START";
case level::RUN: return "RUN";
case level::QUIT: return "QUIT";
case level::FAULT: return "FAULT";
}
return "??????";

View file

@ -331,7 +331,7 @@ void
ircd::server::submit(const hostport &hostport,
request &request)
{
if(unlikely(ircd::runlevel != ircd::runlevel::RUN))
if(unlikely(ircd::run::level != ircd::run::level::RUN))
throw unavailable
{
"Unable to fulfill requests at this time."
@ -480,7 +480,7 @@ void
ircd::server::peer::submit(request &request)
try
{
if(!err_check() || unlikely(ircd::runlevel != ircd::runlevel::RUN))
if(!err_check() || unlikely(ircd::run::level != ircd::run::level::RUN))
throw unavailable
{
"Peer is unable to take any requests: %s", err_msg()
@ -962,7 +962,7 @@ try
// The hostname in open_opts should still reference this object's string.
assert(host(open_opts.hostport).data() == this->hostcanon.data());
if(unlikely(ircd::runlevel != ircd::runlevel::RUN))
if(unlikely(ircd::run::level != ircd::run::level::RUN))
op_fini = true;
if(unlikely(finished()))
@ -1298,7 +1298,7 @@ ircd::server::link::cancel_uncommitted(std::exception_ptr eptr)
bool
ircd::server::link::open(const net::open_opts &open_opts)
{
assert(ircd::runlevel == ircd::runlevel::RUN);
assert(ircd::run::level == ircd::run::level::RUN);
if(op_init)
return false;

View file

@ -60,11 +60,11 @@ on_run()
/// We can't do that on this module init for two reason:
/// - More conf items will load in other modules after this module.
/// - Events can't be safely sent to the !conf room until the RUN state.
const ircd::runlevel_changed
const ircd::run::changed
rehash_on_run{[]
(const auto &runlevel)
(const auto &level)
{
if(runlevel == ircd::runlevel::RUN)
if(level == ircd::run::level::RUN)
ctx::context
{
"confhash", 256_KiB, on_run, ctx::context::POST
@ -142,7 +142,7 @@ noexcept try
unquote(content.at("value"))
};
if(runlevel == runlevel::START && !conf::exists(key))
if(run::level == run::level::START && !conf::exists(key))
return;
// Conf items marked with a persist=false property are not read from

View file

@ -161,13 +161,13 @@ load_listener(const m::event &event)
static bool
_listener_proffer(const net::ipport &ipport)
{
if(unlikely(ircd::runlevel != ircd::runlevel::RUN))
if(unlikely(ircd::run::level != ircd::run::level::RUN))
{
log::dwarning
{
"Refusing to add new client from %s in runlevel %s",
string(ipport),
reflect(ircd::runlevel)
reflect(ircd::run::level)
};
return false;