From 3e6dec5109f39b7be8f076f79ddcd237108aae86 Mon Sep 17 00:00:00 2001 From: Hugh Bellamy Date: Sat, 28 Jan 2017 15:30:15 +0000 Subject: [PATCH] Port swift specific compiler-rt code to Windows --- lib/profile/InstrProfilingFile.c | 10 +--------- lib/profile/InstrProfilingUtil.c | 19 +++++++++++++++++++ lib/profile/InstrProfilingUtil.h | 2 ++ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/profile/InstrProfilingFile.c b/lib/profile/InstrProfilingFile.c index 2c3add347..3365d986e 100644 --- a/lib/profile/InstrProfilingFile.c +++ b/lib/profile/InstrProfilingFile.c @@ -269,16 +269,8 @@ static void exitSignalHandler(int sig) { static void installExitSignalHandlers(void) { unsigned I; - struct sigaction sigact; - int err; for (I = 0; I < lprofCurFilename.NumExitSignals; ++I) { - memset(&sigact, 0, sizeof(sigact)); - sigact.sa_handler = exitSignalHandler; - err = sigaction(lprofCurFilename.ExitOnSignals[I], &sigact, NULL); - if (err) - PROF_WARN( - "Unable to install an exit signal handler for %d (errno = %d).\n", - lprofCurFilename.ExitOnSignals[I], err); + lprofInstallSignalHandler(lprofCurFilename.ExitOnSignals[I], exitSignalHandler); } } diff --git a/lib/profile/InstrProfilingUtil.c b/lib/profile/InstrProfilingUtil.c index 321c7192c..f891b041c 100644 --- a/lib/profile/InstrProfilingUtil.c +++ b/lib/profile/InstrProfilingUtil.c @@ -26,6 +26,7 @@ #include #endif +#include #include #include @@ -219,3 +220,21 @@ COMPILER_RT_VISIBILITY const char *lprofFindLastDirSeparator(const char *Path) { #endif return Sep; } + +COMPILER_RT_VISIBILITY void lprofInstallSignalHandler(int sig, + void (*handler)(int)) { +#ifdef _WIN32 + void (*err)(int) = signal(sig, handler); + if (err == SIG_ERR) + PROF_WARN("Unable to install an exit signal handler for %d (errno = %d).\n", + sig, errno); +#else + struct sigaction sigact; + memset(&sigact, 0, sizeof(sigact)); + sigact.sa_handler = handler; + int err = sigaction(sig, &sigact, NULL); + if (err) + PROF_WARN("Unable to install an exit signal handler for %d (errno = %d).\n", + sig, err); +#endif +} diff --git a/lib/profile/InstrProfilingUtil.h b/lib/profile/InstrProfilingUtil.h index a80fde77e..679e55243 100644 --- a/lib/profile/InstrProfilingUtil.h +++ b/lib/profile/InstrProfilingUtil.h @@ -51,4 +51,6 @@ int lprofGetHostName(char *Name, int Len); unsigned lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV); void *lprofPtrFetchAdd(void **Mem, long ByteIncr); +void lprofInstallSignalHandler(int sig, void(*handler)(int)); + #endif /* PROFILE_INSTRPROFILINGUTIL_H */