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

Commit 9be5c8c

Browse files
author
Zachary Turner
committed
Users of the llvm global mutex must now acquire it manually.
This allows the mutex to be acquired in a guarded, RAII fashion. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211066 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 8b3a8d6 commit 9be5c8c

File tree

5 files changed

+16
-26
lines changed

5 files changed

+16
-26
lines changed

include/llvm/Support/Mutex.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
#define LLVM_SUPPORT_MUTEX_H
1616

1717
#include "llvm/Support/Compiler.h"
18-
#include "llvm/Support/Threading.h"
1918
#include <cassert>
2019

2120
namespace llvm
2221
{
22+
// Forward declare.
23+
bool llvm_is_multithreaded();
24+
2325
namespace sys
2426
{
2527
/// @brief Platform agnostic Mutex class.

include/llvm/Support/Threading.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
#ifndef LLVM_SUPPORT_THREADING_H
1515
#define LLVM_SUPPORT_THREADING_H
1616

17+
#include "llvm/Support/Mutex.h"
18+
1719
namespace llvm {
20+
/// llvm_get_global_lock - returns the llvm global lock object.
21+
sys::Mutex& llvm_get_global_lock();
22+
1823
/// llvm_start_multithreaded - Allocate and initialize structures needed to
1924
/// make LLVM safe for multithreading. The return value indicates whether
2025
/// multithreaded initialization succeeded. LLVM will still be operational
@@ -33,14 +38,6 @@ namespace llvm {
3338
/// mode or not.
3439
bool llvm_is_multithreaded();
3540

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-
4441
/// llvm_execute_on_thread - Execute the given \p UserFn on a separate
4542
/// thread, passing it the provided \p UserData.
4643
///

lib/Support/ManagedStatic.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/Support/ManagedStatic.h"
1515
#include "llvm/Config/config.h"
1616
#include "llvm/Support/Atomic.h"
17+
#include "llvm/Support/MutexGuard.h"
1718
#include <cassert>
1819
using namespace llvm;
1920

@@ -23,7 +24,7 @@ void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
2324
void (*Deleter)(void*)) const {
2425
assert(Creator);
2526
if (llvm_is_multithreaded()) {
26-
llvm_acquire_global_lock();
27+
llvm::MutexGuard Lock(llvm::llvm_get_global_lock());
2728

2829
if (!Ptr) {
2930
void* tmp = Creator();
@@ -43,8 +44,6 @@ void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
4344
Next = StaticList;
4445
StaticList = this;
4546
}
46-
47-
llvm_release_global_lock();
4847
} else {
4948
assert(!Ptr && !DeleterFn && !Next &&
5049
"Partially initialized ManagedStatic!?");

lib/Support/Threading.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ using namespace llvm;
2121

2222
static bool multithreaded_mode = false;
2323

24-
static sys::Mutex* global_lock = nullptr;
24+
sys::Mutex& llvm::llvm_get_global_lock() {
25+
static sys::Mutex global_lock;
26+
return global_lock;
27+
}
2528

2629
bool llvm::llvm_start_multithreaded() {
2730
#if LLVM_ENABLE_THREADS != 0
2831
assert(!multithreaded_mode && "Already multithreaded!");
2932
multithreaded_mode = true;
30-
global_lock = new sys::Mutex(true);
3133

3234
// We fence here to ensure that all initialization is complete BEFORE we
3335
// return from llvm_start_multithreaded().
@@ -47,22 +49,13 @@ void llvm::llvm_stop_multithreaded() {
4749
sys::MemoryFence();
4850

4951
multithreaded_mode = false;
50-
delete global_lock;
5152
#endif
5253
}
5354

5455
bool llvm::llvm_is_multithreaded() {
5556
return multithreaded_mode;
5657
}
5758

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-
6659
#if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H)
6760
#include <pthread.h>
6861

lib/Support/Timer.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "llvm/Support/FileSystem.h"
1919
#include "llvm/Support/Format.h"
2020
#include "llvm/Support/ManagedStatic.h"
21-
#include "llvm/Support/Mutex.h"
21+
#include "llvm/support/MutexGuard.h"
2222
#include "llvm/Support/Process.h"
2323
#include "llvm/Support/raw_ostream.h"
2424
using namespace llvm;
@@ -84,14 +84,13 @@ static TimerGroup *getDefaultTimerGroup() {
8484
sys::MemoryFence();
8585
if (tmp) return tmp;
8686

87-
llvm_acquire_global_lock();
87+
llvm::MutexGuard Lock(llvm::llvm_get_global_lock());
8888
tmp = DefaultTimerGroup;
8989
if (!tmp) {
9090
tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
9191
sys::MemoryFence();
9292
DefaultTimerGroup = tmp;
9393
}
94-
llvm_release_global_lock();
9594

9695
return tmp;
9796
}

0 commit comments

Comments
 (0)