1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 11:42:57 +01:00

Add locking around signal handler registration.

llvm-svn: 79254
This commit is contained in:
Owen Anderson 2009-08-17 17:07:22 +00:00
parent d1c6b5de4e
commit ed218e5820

View File

@ -14,6 +14,7 @@
#include "Unix.h" #include "Unix.h"
#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/STLExtras.h"
#include "llvm/System/Mutex.h"
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#if HAVE_EXECINFO_H #if HAVE_EXECINFO_H
@ -33,6 +34,8 @@ using namespace llvm;
static RETSIGTYPE SignalHandler(int Sig); // defined below. static RETSIGTYPE SignalHandler(int Sig); // defined below.
static SmartMutex<true> SignalsMutex;
/// InterruptFunction - The function to call if ctrl-c is pressed. /// InterruptFunction - The function to call if ctrl-c is pressed.
static void (*InterruptFunction)() = 0; static void (*InterruptFunction)() = 0;
@ -113,6 +116,7 @@ static RETSIGTYPE SignalHandler(int Sig) {
sigfillset(&SigMask); sigfillset(&SigMask);
sigprocmask(SIG_UNBLOCK, &SigMask, 0); sigprocmask(SIG_UNBLOCK, &SigMask, 0);
SignalsMutex.acquire();
if (FilesToRemove != 0) if (FilesToRemove != 0)
while (!FilesToRemove->empty()) { while (!FilesToRemove->empty()) {
FilesToRemove->back().eraseFromDisk(true); FilesToRemove->back().eraseFromDisk(true);
@ -122,14 +126,19 @@ static RETSIGTYPE SignalHandler(int Sig) {
if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) { if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) {
if (InterruptFunction) { if (InterruptFunction) {
void (*IF)() = InterruptFunction; void (*IF)() = InterruptFunction;
SignalsMutex.release();
InterruptFunction = 0; InterruptFunction = 0;
IF(); // run the interrupt function. IF(); // run the interrupt function.
return; return;
} }
SignalsMutex.release();
raise(Sig); // Execute the default handler. raise(Sig); // Execute the default handler.
return; return;
} }
SignalsMutex.release();
// Otherwise if it is a fault (like SEGV) run any handler. // Otherwise if it is a fault (like SEGV) run any handler.
if (CallBacksToRun) if (CallBacksToRun)
for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i) for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i)
@ -139,18 +148,23 @@ static RETSIGTYPE SignalHandler(int Sig) {
void llvm::sys::SetInterruptFunction(void (*IF)()) { void llvm::sys::SetInterruptFunction(void (*IF)()) {
SignalsMutex.acquire();
InterruptFunction = IF; InterruptFunction = IF;
SignalsMutex.release();
RegisterHandlers(); RegisterHandlers();
} }
// RemoveFileOnSignal - The public API // RemoveFileOnSignal - The public API
bool llvm::sys::RemoveFileOnSignal(const sys::Path &Filename, bool llvm::sys::RemoveFileOnSignal(const sys::Path &Filename,
std::string* ErrMsg) { std::string* ErrMsg) {
SignalsMutex.acquire();
if (FilesToRemove == 0) if (FilesToRemove == 0)
FilesToRemove = new std::vector<sys::Path>(); FilesToRemove = new std::vector<sys::Path>();
FilesToRemove->push_back(Filename); FilesToRemove->push_back(Filename);
SignalsMutex.release();
RegisterHandlers(); RegisterHandlers();
return false; return false;
} }