Skip to content

Commit

Permalink
main: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lhmouse committed Jan 7, 2024
1 parent d73e31f commit 8389501
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions poseidon/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,41 +338,59 @@ do_daemonize_finish()
daemon_pipe_wfd.reset();
}

ROCKET_NEVER_INLINE
void
do_check_random()
{
long seed;
if(::RAND_priv_bytes((uint8_t*) &seed, sizeof(seed)) != 1)
do_exit_printf(exit_system_error,
"Could not initialize OpenSSL random number generator: %s\n",
::ERR_reason_error_string(::ERR_peek_error()));

// Initialize standard random functions, too.
::srand((unsigned) seed);
::srand48(seed);
}

template<class ObjectT>
void
do_create_resident_thread(ObjectT& obj, const char* name)
{
auto thrd_function = +[](void* ptr) noexcept
static ::sigset_t s_blocked_signals[1];

// Initialize the list of signals to block. This is done only once.
if(::sigismember(s_blocked_signals, SIGINT) == 0)
for(int sig : { SIGINT, SIGTERM, SIGHUP, SIGALRM, SIGQUIT })
::sigaddset(s_blocked_signals, sig);

// Create the thread now.
::pthread_t thrd;
int err = ::pthread_create(
&thrd,
nullptr,
+[](void* thread_param) -> void*
{
// Set thread information. Errors are ignored.
::sigset_t sigset;
::sigemptyset(&sigset);
::sigaddset(&sigset, SIGINT);
::sigaddset(&sigset, SIGTERM);
::sigaddset(&sigset, SIGHUP);
::sigaddset(&sigset, SIGALRM);
::pthread_sigmask(SIG_BLOCK, &sigset, nullptr);
auto& xobj = *(ObjectT*) thread_param;

::pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, nullptr);
::pthread_sigmask(SIG_BLOCK, s_blocked_signals, nullptr);

// Enter an infinite loop.
for(;;)
try {
((ObjectT*) ptr)->thread_loop();
xobj.thread_loop();
}
catch(exception& stdex) {
::fprintf(stderr,
"WARNING: Caught an exception from thread loop: %s\n"
"[static class `%s`]\n"
"[exception class `%s`]\n",
stdex.what(), typeid(ObjectT).name(), typeid(stdex).name());
stdex.what(), typeid(xobj).name(), typeid(stdex).name());
}
},
::std::addressof(obj)
);

// Make the return type deducible.
return (void*) nullptr;
};

::pthread_t thrd;
int err = ::pthread_create(&thrd, nullptr, thrd_function, ::std::addressof(obj));
if(err != 0)
do_exit_printf(exit_system_error,
"Could not create thread '%s': %s\n",
Expand All @@ -383,21 +401,6 @@ do_create_resident_thread(ObjectT& obj, const char* name)
::pthread_detach(thrd);
}

ROCKET_NEVER_INLINE
void
do_check_random()
{
long seed;
if(::RAND_priv_bytes((uint8_t*) &seed, sizeof(seed)) != 1)
do_exit_printf(exit_system_error,
"Could not initialize OpenSSL random number generator: %s\n",
::ERR_reason_error_string(::ERR_peek_error()));

// Initialize standard random functions, too.
::srand((unsigned) seed);
::srand48(seed);
}

ROCKET_NEVER_INLINE
void
do_create_threads()
Expand Down Expand Up @@ -442,11 +445,8 @@ ROCKET_NEVER_INLINE
void
do_init_signal_handlers()
{
struct ::sigaction sigact;
::sigemptyset(&(sigact.sa_mask));

// Ignore some signals for good.
sigact.sa_flags = 0;
// Ignore certain signals for good.
struct ::sigaction sigact = { };
sigact.sa_handler = SIG_IGN;
::sigaction(SIGPIPE, &sigact, nullptr);
::sigaction(SIGCHLD, &sigact, nullptr);
Expand Down

0 comments on commit 8389501

Please sign in to comment.