Skip to content

Commit

Permalink
feat: adapted signal handling functionality to work on Windows platform
Browse files Browse the repository at this point in the history
  • Loading branch information
jacopodl committed May 8, 2024
1 parent da54f7b commit b45c635
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions argon/vm/signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ARGON_FUNCTION(signal_default_sigint, default_sigint, "", "i: signum", false, fa

bool argon::vm::SignalInit(Context *context) {
static const DefaultHandler def_handlers[] = {
//{&signal_default_sigint, SIGINT}
{nullptr, SIGINT}
};

if (default_handlers != nullptr)
Expand All @@ -45,8 +45,13 @@ bool argon::vm::SignalInit(Context *context) {
return false;

for (auto handler: def_handlers) {
if ((default_handlers[handler.signum] = FunctionNew(handler.handler, nullptr, nullptr)) == nullptr)
goto ERROR;
if (handler.handler != nullptr) {
auto *func = FunctionNew(handler.handler, nullptr, nullptr);
if (func == nullptr)
goto ERROR;

default_handlers[handler.signum] = func;
}
}

if ((handlers = (SigHandler *) memory::Alloc(sizeof(SigHandler) * NSIG)) == nullptr)
Expand Down Expand Up @@ -106,13 +111,15 @@ void argon::vm::SignalResetHandlers() {
SignalAddHandler(i, default_handlers[i]);
}

#ifndef _ARGON_PLATFORM_WINDOWS
void argon::vm::SignalProcMask() {
sigset_t mask{};

sigfillset(&mask);

pthread_sigmask(SIG_SETMASK, &mask, nullptr);
}
#endif

void signal_handler(int signum) {
ArObject *argv[1];
Expand All @@ -122,12 +129,16 @@ void signal_handler(int signum) {
std::unique_lock lock(handler->lock);

auto *func = IncRef(handler->handler);

lock.unlock();

if (func == nullptr)
return;

#ifdef _ARGON_PLATFORM_WINDOWS
// On Windows you need to install the signal handler every time it is triggered.
signal(signum, signal_handler);
#endif

lock.unlock();

if ((argv[0] = (ArObject *) IntNew(signum)) == nullptr) {
Release(func);

Expand Down

0 comments on commit b45c635

Please sign in to comment.