Skip to content

Commit 933c93c

Browse files
committed
reworked signal handler
1 parent 332a365 commit 933c93c

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

radius_clustering/utils/main-emos.c

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,27 @@ Copyright (C) 2024, Haenn Quentin.
1010
#include <stdio.h>
1111
#include <stdlib.h>
1212
#include <string.h>
13-
#include <signal.h>
1413
#include <time.h>
14+
#include <assert.h>
15+
#include <math.h>
16+
1517
#ifdef _WIN32
16-
#include <windows.h>
18+
#include <windows.h>
19+
#include <process.h>
20+
#include <direct.h>
21+
#define SIGINT 2
22+
typedef void (*SignalHandlerFn)(int);
1723
#elif defined(__APPLE__) || defined(__linux__)
18-
#include <sys/time.h>
19-
#include <sys/resource.h>
20-
#include <sys/types.h>
24+
#include <sys/time.h>
25+
#include <sys/resource.h>
26+
#include <sys/types.h>
27+
#include <unistd.h>
28+
#include <signal.h>
2129
#else
22-
#error "Unsupported platform"
30+
#error "Unsupported platform"
2331
#endif
24-
#include <limits.h>
25-
#include <unistd.h>
26-
#include <math.h>
27-
#include <assert.h>
32+
33+
2834
#include "mds3-util.h"
2935
#include "util_heap.h"
3036

@@ -1590,6 +1596,24 @@ void cleanup(){
15901596
}
15911597
}
15921598

1599+
#ifdef _WIN32
1600+
static BOOL WINAPI win32_handler(DWORD signal) {
1601+
if (signal == CTRL_C_EVENT) {
1602+
handler(SIGINT);
1603+
return TRUE;
1604+
}
1605+
return FALSE;
1606+
}
1607+
1608+
static void setup_signal_handler(SignalHandlerFn handler_fn) {
1609+
SetConsoleCtrlHandler(win32_handler, TRUE);
1610+
}
1611+
#else
1612+
static void setup_signal_handler(void (*handler_fn)(int)) {
1613+
signal(SIGINT, handler_fn);
1614+
}
1615+
#endif
1616+
15931617
void handler(int sig) {
15941618
cleanup();
15951619
exit(sig);
@@ -1598,7 +1622,7 @@ void handler(int sig) {
15981622
struct Result* emos_main(int* edges, int n, int nb_edge) {
15991623

16001624
// Set the signal handler
1601-
signal(SIGINT, handler);
1625+
setup_signal_handler(SIGINT, handler);
16021626

16031627
_read_graph_from_edge_list(edges, n, nb_edge);
16041628
NB_NODE_O = NB_NODE;

0 commit comments

Comments
 (0)