Skip to content

Commit 552d9e4

Browse files
author
Mark Seaborn
committed
Fix RWMutex to be thread-safe when pthread_rwlock is not available
lib/Support/RWMutex.cpp contains an implementation of RWMutex that uses pthread_rwlock, but when pthread_rwlock is not available (such as under NaCl, when using newlib), it silently falls back to using the no-op definition in lib/Support/Unix/RWMutex.inc, which is not thread-safe. Fix this case to be thread-safe by using a normal mutex. Differential Revision: http://llvm-reviews.chandlerc.com/D2892 llvm-svn: 202570
1 parent bbae512 commit 552d9e4

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

llvm/lib/Support/Unix/RWMutex.inc

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,36 @@
1616
//=== is guaranteed to work on *all* UNIX variants.
1717
//===----------------------------------------------------------------------===//
1818

19+
#include "llvm/Support/Mutex.h"
20+
1921
namespace llvm {
2022

2123
using namespace sys;
2224

23-
RWMutexImpl::RWMutexImpl() { }
25+
// This naive implementation treats readers the same as writers. This
26+
// will therefore deadlock if a thread tries to acquire a read lock
27+
// multiple times.
28+
29+
RWMutexImpl::RWMutexImpl() : data_(new Mutex(false)) { }
2430

25-
RWMutexImpl::~RWMutexImpl() { }
31+
RWMutexImpl::~RWMutexImpl() {
32+
delete static_cast<Mutex *>(data_);
33+
}
2634

2735
bool RWMutexImpl::reader_acquire() {
28-
return true;
36+
return static_cast<Mutex *>(data_)->acquire();
2937
}
3038

3139
bool RWMutexImpl::reader_release() {
32-
return true;
40+
return static_cast<Mutex *>(data_)->release();
3341
}
3442

3543
bool RWMutexImpl::writer_acquire() {
36-
return true;
44+
return static_cast<Mutex *>(data_)->acquire();
3745
}
3846

3947
bool RWMutexImpl::writer_release() {
40-
return true;
48+
return static_cast<Mutex *>(data_)->release();
4149
}
4250

4351
}

0 commit comments

Comments
 (0)