For PR797:
[oota-llvm.git] / lib / System / Win32 / Signals.inc
index 262c240b3e4fda6196f0a66c2ab422155956fca0..c2bfb01b386c2269d5a0497cc0013baa0a819308 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "Win32.h"
-#include <llvm/System/Signals.h>
 #include <stdio.h>
 #include <vector>
 
-#ifdef __MINGW
-#include <imagehlp.h>
+#ifdef __MINGW32__
+ #include <imagehlp.h>
 #else
-#include <dbghelp.h>
+ #include <dbghelp.h>
 #endif
 #include <psapi.h>
 
-#pragma comment(lib, "psapi.lib")
-#pragma comment(lib, "dbghelp.lib")
+#ifdef __MINGW32__
+ #if ((HAVE_LIBIMAGEHLP != 1) || (HAVE_LIBPSAPI != 1))
+  #error "libimagehlp.a & libpsapi.a should be present"
+ #endif
+#else
+ #pragma comment(lib, "psapi.lib")
+ #pragma comment(lib, "dbghelp.lib")
+#endif
 
 // Forward declare.
 static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep);
 static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType);
 
+// InterruptFunction - The function to call if ctrl-c is pressed.
+static void (*InterruptFunction)() = 0;
+
 static std::vector<llvm::sys::Path> *FilesToRemove = NULL;
 static std::vector<llvm::sys::Path> *DirectoriesToRemove = NULL;
 static bool RegisteredUnhandledExceptionFilter = false;
@@ -72,7 +80,7 @@ static void RegisterHandler() {
 }
 
 // RemoveFileOnSignal - The public API
-void sys::RemoveFileOnSignal(const std::string &Filename) {
+void sys::RemoveFileOnSignal(const sys::Path &Filename) {
   RegisterHandler();
 
   if (CleanupExecuted)
@@ -81,24 +89,26 @@ void sys::RemoveFileOnSignal(const std::string &Filename) {
   if (FilesToRemove == NULL)
     FilesToRemove = new std::vector<sys::Path>;
 
-  FilesToRemove->push_back(sys::Path(Filename));
+  FilesToRemove->push_back(Filename);
 
   LeaveCriticalSection(&CriticalSection);
 }
 
 // RemoveDirectoryOnSignal - The public API
 void sys::RemoveDirectoryOnSignal(const sys::Path& path) {
+  // Not a directory?
+  sys::FileStatus Status;
+  if (path.getFileStatus(Status) || !Status.isDir)
+    return;
+
   RegisterHandler();
 
   if (CleanupExecuted)
     throw std::string("Process terminating -- cannot register for removal");
 
-  if (path.isDirectory()) {
-    if (DirectoriesToRemove == NULL)
-      DirectoriesToRemove = new std::vector<sys::Path>;
-
-    DirectoriesToRemove->push_back(path);
-  }
+  if (DirectoriesToRemove == NULL)
+    DirectoriesToRemove = new std::vector<sys::Path>;
+  DirectoriesToRemove->push_back(path);
 
   LeaveCriticalSection(&CriticalSection);
 }
@@ -110,6 +120,12 @@ void sys::PrintStackTraceOnErrorSignal() {
   LeaveCriticalSection(&CriticalSection);
 }
 
+
+void sys::SetInterruptFunction(void (*IF)()) {
+  RegisterHandler();
+  InterruptFunction = IF;
+  LeaveCriticalSection(&CriticalSection);
+}
 }
 
 static void Cleanup() {
@@ -124,7 +140,7 @@ static void Cleanup() {
   if (FilesToRemove != NULL)
     while (!FilesToRemove->empty()) {
       try {
-        FilesToRemove->back().destroyFile();
+        FilesToRemove->back().eraseFromDisk();
       } catch (...) {
       }
       FilesToRemove->pop_back();
@@ -133,7 +149,7 @@ static void Cleanup() {
   if (DirectoriesToRemove != NULL)
     while (!DirectoriesToRemove->empty()) {
       try {
-        DirectoriesToRemove->back().destroyDirectory(true);
+        DirectoriesToRemove->back().eraseFromDisk(true);
       } catch (...) {
       }
       DirectoriesToRemove->pop_back();
@@ -231,10 +247,25 @@ static LONG WINAPI LLVMUnhandledExceptionFilter(LPEXCEPTION_POINTERS ep) {
 }
 
 static BOOL WINAPI LLVMConsoleCtrlHandler(DWORD dwCtrlType) {
+  // We are running in our very own thread, courtesy of Windows.
+  EnterCriticalSection(&CriticalSection);
   Cleanup();
 
+  // If an interrupt function has been set, go and run one it; otherwise,
+  // the process dies.
+  void (*IF)() = InterruptFunction;
+  InterruptFunction = 0;      // Don't run it on another CTRL-C.
+
+  if (IF) {
+    // Note: if the interrupt function throws an exception, there is nothing
+    // to catch it in this thread so it will kill the process.
+    IF();                     // Run it now.
+    LeaveCriticalSection(&CriticalSection);
+    return TRUE;              // Don't kill the process.
+  }
+
   // Allow normal processing to take place; i.e., the process dies.
+  LeaveCriticalSection(&CriticalSection);
   return FALSE;
 }
 
-// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab