From 7e3e4311c56a975e4671a881c297231402ce5002 Mon Sep 17 00:00:00 2001 From: Dylan Noblesmith Date: Sat, 23 Aug 2014 22:49:17 +0000 Subject: [PATCH] Support/Unix: use ScopedLock wherever possible Only one function remains a bit too complicated for a simple mutex guard. No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216335 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/Unix/Signals.inc | 46 +++++++++++++++++------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/Support/Unix/Signals.inc b/lib/Support/Unix/Signals.inc index 1841fea25e2..8161bfbf2a3 100644 --- a/lib/Support/Unix/Signals.inc +++ b/lib/Support/Unix/Signals.inc @@ -196,37 +196,37 @@ static RETSIGTYPE SignalHandler(int Sig) { } void llvm::sys::RunInterruptHandlers() { - SignalsMutex.acquire(); + sys::SmartScopedLock Guard(SignalsMutex); RemoveFilesToRemove(); - SignalsMutex.release(); } void llvm::sys::SetInterruptFunction(void (*IF)()) { - SignalsMutex.acquire(); - InterruptFunction = IF; - SignalsMutex.release(); + { + sys::SmartScopedLock Guard(SignalsMutex); + InterruptFunction = IF; + } RegisterHandlers(); } // RemoveFileOnSignal - The public API bool llvm::sys::RemoveFileOnSignal(StringRef Filename, std::string* ErrMsg) { - SignalsMutex.acquire(); - std::string *OldPtr = FilesToRemove.empty() ? nullptr : &FilesToRemove[0]; - FilesToRemove.push_back(Filename); - - // We want to call 'c_str()' on every std::string in this vector so that if - // the underlying implementation requires a re-allocation, it happens here - // rather than inside of the signal handler. If we see the vector grow, we - // have to call it on every entry. If it remains in place, we only need to - // call it on the latest one. - if (OldPtr == &FilesToRemove[0]) - FilesToRemove.back().c_str(); - else - for (unsigned i = 0, e = FilesToRemove.size(); i != e; ++i) - FilesToRemove[i].c_str(); - - SignalsMutex.release(); + { + sys::SmartScopedLock Guard(SignalsMutex); + std::string *OldPtr = FilesToRemove.empty() ? nullptr : &FilesToRemove[0]; + FilesToRemove.push_back(Filename); + + // We want to call 'c_str()' on every std::string in this vector so that if + // the underlying implementation requires a re-allocation, it happens here + // rather than inside of the signal handler. If we see the vector grow, we + // have to call it on every entry. If it remains in place, we only need to + // call it on the latest one. + if (OldPtr == &FilesToRemove[0]) + FilesToRemove.back().c_str(); + else + for (unsigned i = 0, e = FilesToRemove.size(); i != e; ++i) + FilesToRemove[i].c_str(); + } RegisterHandlers(); return false; @@ -234,7 +234,7 @@ bool llvm::sys::RemoveFileOnSignal(StringRef Filename, // DontRemoveFileOnSignal - The public API void llvm::sys::DontRemoveFileOnSignal(StringRef Filename) { - SignalsMutex.acquire(); + sys::SmartScopedLock Guard(SignalsMutex); std::vector::reverse_iterator RI = std::find(FilesToRemove.rbegin(), FilesToRemove.rend(), Filename); std::vector::iterator I = FilesToRemove.end(); @@ -247,8 +247,6 @@ void llvm::sys::DontRemoveFileOnSignal(StringRef Filename) { // made on insertion become invalid by being copied down an element. for (std::vector::iterator E = FilesToRemove.end(); I != E; ++I) I->c_str(); - - SignalsMutex.release(); } /// AddSignalHandler - Add a function to be called when a signal is delivered -- 2.34.1