Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 761f331

Browse files
author
Zachary Turner
committed
Kill the LLVM global lock.
This patch removes the LLVM global lock, and updates all existing users of the global lock to use their own mutex. None of the existing users of the global lock were protecting code that was mutually exclusive with any of the other users of the global lock, so its purpose was not being met. Reviewed by: rnk Differential Revision: http://reviews.llvm.org/D4142 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211277 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 591f9ee commit 761f331

File tree

5 files changed

+22
-30
lines changed

5 files changed

+22
-30
lines changed

docs/ProgrammersManual.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,11 +2226,6 @@ Note that, because no other threads are allowed to issue LLVM API calls before
22262226
``llvm_start_multithreaded()`` returns, it is possible to have
22272227
``ManagedStatic``\ s of ``llvm::sys::Mutex``\ s.
22282228

2229-
The ``llvm_acquire_global_lock()`` and ``llvm_release_global_lock`` APIs provide
2230-
access to the global lock used to implement the double-checked locking for lazy
2231-
initialization. These should only be used internally to LLVM, and only if you
2232-
know what you're doing!
2233-
22342229
.. _llvmcontext:
22352230

22362231
Achieving Isolation with ``LLVMContext``

include/llvm/Support/Threading.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ namespace llvm {
3333
/// mode or not.
3434
bool llvm_is_multithreaded();
3535

36-
/// acquire_global_lock - Acquire the global lock. This is a no-op if called
37-
/// before llvm_start_multithreaded().
38-
void llvm_acquire_global_lock();
39-
40-
/// release_global_lock - Release the global lock. This is a no-op if called
41-
/// before llvm_start_multithreaded().
42-
void llvm_release_global_lock();
43-
4436
/// llvm_execute_on_thread - Execute the given \p UserFn on a separate
4537
/// thread, passing it the provided \p UserData.
4638
///

lib/Support/ManagedStatic.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,32 @@
1515
#include "llvm/Config/config.h"
1616
#include "llvm/Support/Atomic.h"
1717
#include <cassert>
18+
#include <mutex>
1819
using namespace llvm;
1920

2021
static const ManagedStaticBase *StaticList = nullptr;
2122

23+
// ManagedStatics can get created during execution of static constructors. As a
24+
// result, we cannot use a global static std::mutex object for the lock since it
25+
// may not have been constructed. Instead, we do a call-once initialization of
26+
// a pointer to a mutex.
27+
static std::once_flag MutexInitializationFlag;
28+
static std::recursive_mutex* ManagedStaticMutex = nullptr;
29+
30+
// Not all supported platforms (in particular VS2012) have thread-safe function
31+
// static initialization, so roll our own.
32+
static std::recursive_mutex& GetManagedStaticMutex() {
33+
std::call_once(MutexInitializationFlag,
34+
[]() { ManagedStaticMutex = new std::recursive_mutex(); } );
35+
36+
return *ManagedStaticMutex;
37+
}
38+
2239
void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
2340
void (*Deleter)(void*)) const {
2441
assert(Creator);
2542
if (llvm_is_multithreaded()) {
26-
llvm_acquire_global_lock();
43+
std::lock_guard<std::recursive_mutex> Lock(GetManagedStaticMutex());
2744

2845
if (!Ptr) {
2946
void* tmp = Creator();
@@ -43,8 +60,6 @@ void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
4360
Next = StaticList;
4461
StaticList = this;
4562
}
46-
47-
llvm_release_global_lock();
4863
} else {
4964
assert(!Ptr && !DeleterFn && !Next &&
5065
"Partially initialized ManagedStatic!?");
@@ -75,6 +90,8 @@ void ManagedStaticBase::destroy() const {
7590

7691
/// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
7792
void llvm::llvm_shutdown() {
93+
std::lock_guard<std::recursive_mutex> Lock(GetManagedStaticMutex());
94+
7895
while (StaticList)
7996
StaticList->destroy();
8097

lib/Support/Threading.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ using namespace llvm;
2121

2222
static bool multithreaded_mode = false;
2323

24-
static sys::Mutex* global_lock = nullptr;
25-
2624
bool llvm::llvm_start_multithreaded() {
2725
#if LLVM_ENABLE_THREADS != 0
2826
assert(!multithreaded_mode && "Already multithreaded!");
2927
multithreaded_mode = true;
30-
global_lock = new sys::Mutex(true);
3128

3229
// We fence here to ensure that all initialization is complete BEFORE we
3330
// return from llvm_start_multithreaded().
@@ -47,22 +44,13 @@ void llvm::llvm_stop_multithreaded() {
4744
sys::MemoryFence();
4845

4946
multithreaded_mode = false;
50-
delete global_lock;
5147
#endif
5248
}
5349

5450
bool llvm::llvm_is_multithreaded() {
5551
return multithreaded_mode;
5652
}
5753

58-
void llvm::llvm_acquire_global_lock() {
59-
if (multithreaded_mode) global_lock->acquire();
60-
}
61-
62-
void llvm::llvm_release_global_lock() {
63-
if (multithreaded_mode) global_lock->release();
64-
}
65-
6654
#if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H)
6755
#include <pthread.h>
6856

lib/Support/Timer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/Support/Format.h"
2020
#include "llvm/Support/ManagedStatic.h"
2121
#include "llvm/Support/Mutex.h"
22+
#include "llvm/Support/MutexGuard.h"
2223
#include "llvm/Support/Process.h"
2324
#include "llvm/Support/raw_ostream.h"
2425
using namespace llvm;
@@ -84,14 +85,13 @@ static TimerGroup *getDefaultTimerGroup() {
8485
sys::MemoryFence();
8586
if (tmp) return tmp;
8687

87-
llvm_acquire_global_lock();
88+
sys::SmartScopedLock<true> Lock(*TimerLock);
8889
tmp = DefaultTimerGroup;
8990
if (!tmp) {
9091
tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
9192
sys::MemoryFence();
9293
DefaultTimerGroup = tmp;
9394
}
94-
llvm_release_global_lock();
9595

9696
return tmp;
9797
}

0 commit comments

Comments
 (0)