Skip to content

Commit

Permalink
Add event loop with interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Jul 17, 2024
1 parent 87fbc14 commit f49fd02
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/bitcoin-mine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#include <tinyformat.h>
#include <util/translation.h>

#ifndef WIN32
// #include <cerrno>
#include <signal.h>
// #include <sys/stat.h>
#endif
static const char* const HELP_USAGE{R"(
bitcoin-mine is a test program for interacting with bitcoin-node via IPC.
Expand Down Expand Up @@ -48,6 +53,24 @@ static void AddArgs(ArgsManager& args)
init::AddLoggingArgs(args);
}

static bool g_interrupt{false};

#ifndef WIN32
static void registerSignalHandler(int signal, void(*handler)(int))
{
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(signal, &sa, nullptr);
}
static void HandleSIGTERM(int)
{
g_interrupt = true;
}

#endif

MAIN_FUNCTION
{
ArgsManager& args = gArgs;
Expand Down Expand Up @@ -105,5 +128,14 @@ MAIN_FUNCTION
std::unique_ptr<interfaces::Mining> mining{node_init->makeMining()};
assert(mining);

#ifndef WIN32
registerSignalHandler(SIGTERM, HandleSIGTERM);
registerSignalHandler(SIGINT, HandleSIGTERM);
#endif

while(!g_interrupt) {
UninterruptibleSleep(100ms);
}

return EXIT_SUCCESS;
}

0 comments on commit f49fd02

Please sign in to comment.