aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDannyDaemonic <DannyDaemonic@gmail.com>2023-05-02 18:01:57 -0700
committerGitHub <noreply@github.com>2023-05-02 18:01:57 -0700
commit13b0c68ed7a9948db0720f7393df094ab1005b14 (patch)
tree0134d1d530b8ff5b91a2407d984bb23e31078272 /examples
parent55bc5f0900d925c539488901c5538b637d68665c (diff)
Handle signals properly on Windows (#1123)
Diffstat (limited to 'examples')
-rw-r--r--examples/main/main.cpp17
1 files changed, 7 insertions, 10 deletions
diff --git a/examples/main/main.cpp b/examples/main/main.cpp
index a10256a..125c189 100644
--- a/examples/main/main.cpp
+++ b/examples/main/main.cpp
@@ -22,6 +22,9 @@
#include <signal.h>
#include <unistd.h>
#elif defined (_WIN32)
+#define WIN32_LEAN_AND_MEAN
+#define NOMINMAX
+#include <windows.h>
#include <signal.h>
#endif
@@ -240,7 +243,10 @@ int main(int argc, char ** argv) {
sigint_action.sa_flags = 0;
sigaction(SIGINT, &sigint_action, NULL);
#elif defined (_WIN32)
- signal(SIGINT, sigint_handler);
+ auto console_ctrl_handler = [](DWORD ctrl_type) -> BOOL {
+ return (ctrl_type == CTRL_C_EVENT) ? (sigint_handler(SIGINT), true) : false;
+ };
+ SetConsoleCtrlHandler(static_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
#endif
fprintf(stderr, "%s: interactive mode on.\n", __func__);
@@ -519,11 +525,6 @@ int main(int argc, char ** argv) {
// potentially set color to indicate we are taking user input
set_console_color(con_st, CONSOLE_COLOR_USER_INPUT);
-#if defined (_WIN32)
- // Windows: must reactivate sigint handler after each signal
- signal(SIGINT, sigint_handler);
-#endif
-
if (params.instruct) {
printf("\n> ");
}
@@ -607,10 +608,6 @@ int main(int argc, char ** argv) {
}
}
-#if defined (_WIN32)
- signal(SIGINT, SIG_DFL);
-#endif
-
llama_print_timings(ctx);
llama_free(ctx);