Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add signal support on windows #366

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions core/src/ten_runtime/global/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,57 @@ void ten_global_setup_signal_stuff(void) {

#else

void ten_global_setup_signal_stuff(void) {}
#include <windows.h>

void ten_global_signal_alt_stack_create(void) {}
static volatile LONG ctrl_c_count = 0;

void ten_global_signal_alt_stack_destroy(void) {}
BOOL WINAPI ConsoleHandler(DWORD dwCtrlType) {
switch (dwCtrlType) {
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
TEN_LOGW("Received CTRL+C/CTRL+BREAK.");

ten_mutex_lock(g_apps_mutex);

ten_list_foreach (&g_apps, iter) {
ten_app_t *app = ten_ptr_listnode_get(iter.node);
TEN_ASSERT(app, "Invalid argument.");

ten_app_close(app, NULL);
}

ten_mutex_unlock(g_apps_mutex);

ctrl_c_count++;
if (ctrl_c_count >= 2) {
TEN_LOGW("Received CTRL+C/CTRL+BREAK twice, exit directly.");
exit(EXIT_FAILURE);
}
return TRUE; // Signal has been handled.

default:
return FALSE; // Signal has _not_ been handled.
}
}

void ten_global_setup_signal_stuff(void) {
const char *disable_signal_trap = getenv("TEN_DISABLE_SIGNAL_TRAP");
if (disable_signal_trap && !strcmp(disable_signal_trap, "true")) {
// No trap signal, for nodejs / python / java bindings
} else {
if (!SetConsoleCtrlHandler(ConsoleHandler, TRUE)) {
TEN_LOGF("Failed to set control handler.");
exit(-TEN_ERRNO_GENERIC);
}
}
}

void ten_global_signal_alt_stack_create(void) {
// Windows does not support alternate signal stacks, so no need to implement.
}

void ten_global_signal_alt_stack_destroy(void) {
// Windows does not support alternate signal stacks, so no need to implement.
}

#endif
Loading