<div dir="ltr">This change was reverted in r211072 due to accidentally pushing a whole branch instead of a single CL.<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jun 16, 2014 at 3:39 PM, Zachary Turner <span dir="ltr"><<a href="mailto:zturner@google.com" target="_blank">zturner@google.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: zturner<br>
Date: Mon Jun 16 17:39:38 2014<br>
New Revision: 211066<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=211066&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=211066&view=rev</a><br>
Log:<br>
Users of the llvm global mutex must now acquire it manually.<br>
<br>
This allows the mutex to be acquired in a guarded, RAII fashion.<br>
<br>
Modified:<br>
llvm/trunk/include/llvm/Support/Mutex.h<br>
llvm/trunk/include/llvm/Support/Threading.h<br>
llvm/trunk/lib/Support/ManagedStatic.cpp<br>
llvm/trunk/lib/Support/Threading.cpp<br>
llvm/trunk/lib/Support/Timer.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/Support/Mutex.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Mutex.h?rev=211066&r1=211065&r2=211066&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Mutex.h?rev=211066&r1=211065&r2=211066&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Support/Mutex.h (original)<br>
+++ llvm/trunk/include/llvm/Support/Mutex.h Mon Jun 16 17:39:38 2014<br>
@@ -15,11 +15,13 @@<br>
#define LLVM_SUPPORT_MUTEX_H<br>
<br>
#include "llvm/Support/Compiler.h"<br>
-#include "llvm/Support/Threading.h"<br>
#include <cassert><br>
<br>
namespace llvm<br>
{<br>
+ // Forward declare.<br>
+ bool llvm_is_multithreaded();<br>
+<br>
namespace sys<br>
{<br>
/// @brief Platform agnostic Mutex class.<br>
<br>
Modified: llvm/trunk/include/llvm/Support/Threading.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Threading.h?rev=211066&r1=211065&r2=211066&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Threading.h?rev=211066&r1=211065&r2=211066&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Support/Threading.h (original)<br>
+++ llvm/trunk/include/llvm/Support/Threading.h Mon Jun 16 17:39:38 2014<br>
@@ -14,7 +14,12 @@<br>
#ifndef LLVM_SUPPORT_THREADING_H<br>
#define LLVM_SUPPORT_THREADING_H<br>
<br>
+#include "llvm/Support/Mutex.h"<br>
+<br>
namespace llvm {<br>
+ /// llvm_get_global_lock - returns the llvm global lock object.<br>
+ sys::Mutex& llvm_get_global_lock();<br>
+<br>
/// llvm_start_multithreaded - Allocate and initialize structures needed to<br>
/// make LLVM safe for multithreading. The return value indicates whether<br>
/// multithreaded initialization succeeded. LLVM will still be operational<br>
@@ -33,14 +38,6 @@ namespace llvm {<br>
/// mode or not.<br>
bool llvm_is_multithreaded();<br>
<br>
- /// acquire_global_lock - Acquire the global lock. This is a no-op if called<br>
- /// before llvm_start_multithreaded().<br>
- void llvm_acquire_global_lock();<br>
-<br>
- /// release_global_lock - Release the global lock. This is a no-op if called<br>
- /// before llvm_start_multithreaded().<br>
- void llvm_release_global_lock();<br>
-<br>
/// llvm_execute_on_thread - Execute the given \p UserFn on a separate<br>
/// thread, passing it the provided \p UserData.<br>
///<br>
<br>
Modified: llvm/trunk/lib/Support/ManagedStatic.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/ManagedStatic.cpp?rev=211066&r1=211065&r2=211066&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/ManagedStatic.cpp?rev=211066&r1=211065&r2=211066&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Support/ManagedStatic.cpp (original)<br>
+++ llvm/trunk/lib/Support/ManagedStatic.cpp Mon Jun 16 17:39:38 2014<br>
@@ -14,6 +14,7 @@<br>
#include "llvm/Support/ManagedStatic.h"<br>
#include "llvm/Config/config.h"<br>
#include "llvm/Support/Atomic.h"<br>
+#include "llvm/Support/MutexGuard.h"<br>
#include <cassert><br>
using namespace llvm;<br>
<br>
@@ -23,7 +24,7 @@ void ManagedStaticBase::RegisterManagedS<br>
void (*Deleter)(void*)) const {<br>
assert(Creator);<br>
if (llvm_is_multithreaded()) {<br>
- llvm_acquire_global_lock();<br>
+ llvm::MutexGuard Lock(llvm::llvm_get_global_lock());<br>
<br>
if (!Ptr) {<br>
void* tmp = Creator();<br>
@@ -43,8 +44,6 @@ void ManagedStaticBase::RegisterManagedS<br>
Next = StaticList;<br>
StaticList = this;<br>
}<br>
-<br>
- llvm_release_global_lock();<br>
} else {<br>
assert(!Ptr && !DeleterFn && !Next &&<br>
"Partially initialized ManagedStatic!?");<br>
<br>
Modified: llvm/trunk/lib/Support/Threading.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Threading.cpp?rev=211066&r1=211065&r2=211066&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Threading.cpp?rev=211066&r1=211065&r2=211066&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Support/Threading.cpp (original)<br>
+++ llvm/trunk/lib/Support/Threading.cpp Mon Jun 16 17:39:38 2014<br>
@@ -21,13 +21,15 @@ using namespace llvm;<br>
<br>
static bool multithreaded_mode = false;<br>
<br>
-static sys::Mutex* global_lock = nullptr;<br>
+sys::Mutex& llvm::llvm_get_global_lock() {<br>
+ static sys::Mutex global_lock;<br>
+ return global_lock;<br>
+}<br>
<br>
bool llvm::llvm_start_multithreaded() {<br>
#if LLVM_ENABLE_THREADS != 0<br>
assert(!multithreaded_mode && "Already multithreaded!");<br>
multithreaded_mode = true;<br>
- global_lock = new sys::Mutex(true);<br>
<br>
// We fence here to ensure that all initialization is complete BEFORE we<br>
// return from llvm_start_multithreaded().<br>
@@ -47,7 +49,6 @@ void llvm::llvm_stop_multithreaded() {<br>
sys::MemoryFence();<br>
<br>
multithreaded_mode = false;<br>
- delete global_lock;<br>
#endif<br>
}<br>
<br>
@@ -55,14 +56,6 @@ bool llvm::llvm_is_multithreaded() {<br>
return multithreaded_mode;<br>
}<br>
<br>
-void llvm::llvm_acquire_global_lock() {<br>
- if (multithreaded_mode) global_lock->acquire();<br>
-}<br>
-<br>
-void llvm::llvm_release_global_lock() {<br>
- if (multithreaded_mode) global_lock->release();<br>
-}<br>
-<br>
#if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H)<br>
#include <pthread.h><br>
<br>
<br>
Modified: llvm/trunk/lib/Support/Timer.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Timer.cpp?rev=211066&r1=211065&r2=211066&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Timer.cpp?rev=211066&r1=211065&r2=211066&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Support/Timer.cpp (original)<br>
+++ llvm/trunk/lib/Support/Timer.cpp Mon Jun 16 17:39:38 2014<br>
@@ -18,7 +18,7 @@<br>
#include "llvm/Support/FileSystem.h"<br>
#include "llvm/Support/Format.h"<br>
#include "llvm/Support/ManagedStatic.h"<br>
-#include "llvm/Support/Mutex.h"<br>
+#include "llvm/support/MutexGuard.h"<br>
#include "llvm/Support/Process.h"<br>
#include "llvm/Support/raw_ostream.h"<br>
using namespace llvm;<br>
@@ -84,14 +84,13 @@ static TimerGroup *getDefaultTimerGroup(<br>
sys::MemoryFence();<br>
if (tmp) return tmp;<br>
<br>
- llvm_acquire_global_lock();<br>
+ llvm::MutexGuard Lock(llvm::llvm_get_global_lock());<br>
tmp = DefaultTimerGroup;<br>
if (!tmp) {<br>
tmp = new TimerGroup("Miscellaneous Ungrouped Timers");<br>
sys::MemoryFence();<br>
DefaultTimerGroup = tmp;<br>
}<br>
- llvm_release_global_lock();<br>
<br>
return tmp;<br>
}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>