10#include <cosmos/cosmos.hxx>
11#include <cosmos/private/Initable.hxx>
12#include <cosmos/private/cosmos.hxx>
13#include <cosmos/proc/process.hxx>
20typedef std::map<InitPrio, Initable*> InitableMap;
24 void add(
const InitPrio prio,
Initable *init_if);
28 InitableMap initables;
32 std::atomic<std::size_t> init_counter;
40 void createInitData() {
55 init_data =
new InitData;
60void InitData::add(
const InitPrio prio, Initable *init_if) {
62 auto ret = initables.insert(std::make_pair(prio, init_if));
64 if (ret.second !=
true) {
65 std::cerr <<
"Conflicting priority of Initables!\n";
70void InitData::init() {
71 for (
auto &[prio, initable]: initables) {
73 initable->m_lib_initialized =
true;
77void InitData::finish() {
79 for (
auto it = initables.rbegin(); it != initables.rend(); it++) {
80 auto initable = it->second;
82 initable->m_lib_initialized =
false;
86void Initable::registerInitable(
const InitPrio prio) {
89 init_data->add(prio,
this);
93 if (init_counter++ != 0)
105 if (--init_counter != 0)
115RestartOnIntr auto_restart_syscalls{
true};
117void set_restart_syscall_on_interrupt(
const bool auto_restart) {
118 auto_restart_syscalls = RestartOnIntr{auto_restart};
121void fatal_error(
const std::string_view msg,
const std::exception *ex) {
122 std::cerr <<
"[libcosmos] FATAL: " << msg <<
"\n";
124 std::cerr <<
"Exception context:\n\n" << ex->what() <<
"\n";
126 std::cerr <<
"Aborting program.\n";
130void noncritical_error(
131 const std::string_view msg,
132 const std::exception &ex) {
133 std::cerr <<
"[libcosmos] WARNING: " << msg <<
"\n";
134 std::cerr <<
"Exception context:\n\n" << ex.what() <<
"\n";
138RunningOnValgrind running_on_valgrind{
false};
143class RunningOnValgrindInit :
147 RunningOnValgrindInit() : Initable(InitPrio::RUNNING_ON_VALGRIND) {
152 void libInit()
override {
161 if (
auto ld_preload = proc::get_env_var(
"LD_PRELOAD"); ld_preload) {
162 auto view = ld_preload->view();
163 if (view.find(
"valgrind") != view.npos) {
164 running_on_valgrind = RunningOnValgrind{
true};
169 void libExit()
override {
174RunningOnValgrindInit g_running_on_valgrind_init;
Pure virtual base class for the library init system.