2017-03-26 23:27:04 +08:00
|
|
|
#include "test.h"
|
|
|
|
#include <atomic>
|
|
|
|
#include <vector>
|
|
|
|
#include <sanitizer/tsan_interface.h>
|
|
|
|
|
|
|
|
// A very primitive mutex annotated with tsan annotations.
|
|
|
|
class Mutex {
|
|
|
|
public:
|
[tsan] Add Mutex annotation flag for constant-initialized __tsan_mutex_linker_init behavior
Add a new flag, __tsan_mutex_not_static, which has the opposite sense
of __tsan_mutex_linker_init. When the new __tsan_mutex_not_static flag
is passed to __tsan_mutex_destroy, tsan ignores the destruction unless
the mutex was also created with the __tsan_mutex_not_static flag.
This is useful for constructors that otherwise woud set
__tsan_mutex_linker_init but cannot, because they are declared constexpr.
Google has a custom mutex with two constructors, a "linker initialized"
constructor that relies on zero-initialization and sets
__tsan_mutex_linker_init, and a normal one which sets no tsan flags.
The "linker initialized" constructor is morally constexpr, but we can't
declare it constexpr because of the need to call into tsan as a side effect.
With this new flag, the normal c'tor can set __tsan_mutex_not_static,
the "linker initialized" constructor can rely on tsan's lazy initialization,
and __tsan_mutex_destroy can still handle both cases correctly.
Author: Greg Falcon (gfalcon)
Reviewed in: https://reviews.llvm.org/D39095
llvm-svn: 316209
2017-10-20 20:08:53 +08:00
|
|
|
Mutex(bool prof, unsigned create_flags, unsigned destroy_flags=0)
|
2017-03-26 23:27:04 +08:00
|
|
|
: prof_(prof)
|
|
|
|
, locked_(false)
|
[tsan] Add Mutex annotation flag for constant-initialized __tsan_mutex_linker_init behavior
Add a new flag, __tsan_mutex_not_static, which has the opposite sense
of __tsan_mutex_linker_init. When the new __tsan_mutex_not_static flag
is passed to __tsan_mutex_destroy, tsan ignores the destruction unless
the mutex was also created with the __tsan_mutex_not_static flag.
This is useful for constructors that otherwise woud set
__tsan_mutex_linker_init but cannot, because they are declared constexpr.
Google has a custom mutex with two constructors, a "linker initialized"
constructor that relies on zero-initialization and sets
__tsan_mutex_linker_init, and a normal one which sets no tsan flags.
The "linker initialized" constructor is morally constexpr, but we can't
declare it constexpr because of the need to call into tsan as a side effect.
With this new flag, the normal c'tor can set __tsan_mutex_not_static,
the "linker initialized" constructor can rely on tsan's lazy initialization,
and __tsan_mutex_destroy can still handle both cases correctly.
Author: Greg Falcon (gfalcon)
Reviewed in: https://reviews.llvm.org/D39095
llvm-svn: 316209
2017-10-20 20:08:53 +08:00
|
|
|
, seq_(0)
|
|
|
|
, destroy_flags_(destroy_flags) {
|
|
|
|
__tsan_mutex_create(this, create_flags);
|
2017-03-26 23:27:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~Mutex() {
|
[tsan] Add Mutex annotation flag for constant-initialized __tsan_mutex_linker_init behavior
Add a new flag, __tsan_mutex_not_static, which has the opposite sense
of __tsan_mutex_linker_init. When the new __tsan_mutex_not_static flag
is passed to __tsan_mutex_destroy, tsan ignores the destruction unless
the mutex was also created with the __tsan_mutex_not_static flag.
This is useful for constructors that otherwise woud set
__tsan_mutex_linker_init but cannot, because they are declared constexpr.
Google has a custom mutex with two constructors, a "linker initialized"
constructor that relies on zero-initialization and sets
__tsan_mutex_linker_init, and a normal one which sets no tsan flags.
The "linker initialized" constructor is morally constexpr, but we can't
declare it constexpr because of the need to call into tsan as a side effect.
With this new flag, the normal c'tor can set __tsan_mutex_not_static,
the "linker initialized" constructor can rely on tsan's lazy initialization,
and __tsan_mutex_destroy can still handle both cases correctly.
Author: Greg Falcon (gfalcon)
Reviewed in: https://reviews.llvm.org/D39095
llvm-svn: 316209
2017-10-20 20:08:53 +08:00
|
|
|
__tsan_mutex_destroy(this, destroy_flags_);
|
2017-03-26 23:27:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lock() {
|
|
|
|
__tsan_mutex_pre_lock(this, 0);
|
|
|
|
LockImpl();
|
|
|
|
__tsan_mutex_post_lock(this, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TryLock() {
|
|
|
|
__tsan_mutex_pre_lock(this, __tsan_mutex_try_lock);
|
|
|
|
bool ok = TryLockImpl();
|
|
|
|
__tsan_mutex_post_lock(this, __tsan_mutex_try_lock |
|
|
|
|
(ok ? 0 : __tsan_mutex_try_lock_failed), 0);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unlock() {
|
|
|
|
__tsan_mutex_pre_unlock(this, 0);
|
|
|
|
UnlockImpl();
|
|
|
|
__tsan_mutex_post_unlock(this, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Wait() {
|
|
|
|
for (int seq = seq_; seq == seq_;) {
|
|
|
|
Unlock();
|
|
|
|
usleep(100);
|
|
|
|
Lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Broadcast() {
|
|
|
|
__tsan_mutex_pre_signal(this, 0);
|
|
|
|
LockImpl(false);
|
|
|
|
seq_++;
|
|
|
|
UnlockImpl();
|
|
|
|
__tsan_mutex_post_signal(this, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const bool prof_;
|
|
|
|
std::atomic<bool> locked_;
|
|
|
|
int seq_;
|
[tsan] Add Mutex annotation flag for constant-initialized __tsan_mutex_linker_init behavior
Add a new flag, __tsan_mutex_not_static, which has the opposite sense
of __tsan_mutex_linker_init. When the new __tsan_mutex_not_static flag
is passed to __tsan_mutex_destroy, tsan ignores the destruction unless
the mutex was also created with the __tsan_mutex_not_static flag.
This is useful for constructors that otherwise woud set
__tsan_mutex_linker_init but cannot, because they are declared constexpr.
Google has a custom mutex with two constructors, a "linker initialized"
constructor that relies on zero-initialization and sets
__tsan_mutex_linker_init, and a normal one which sets no tsan flags.
The "linker initialized" constructor is morally constexpr, but we can't
declare it constexpr because of the need to call into tsan as a side effect.
With this new flag, the normal c'tor can set __tsan_mutex_not_static,
the "linker initialized" constructor can rely on tsan's lazy initialization,
and __tsan_mutex_destroy can still handle both cases correctly.
Author: Greg Falcon (gfalcon)
Reviewed in: https://reviews.llvm.org/D39095
llvm-svn: 316209
2017-10-20 20:08:53 +08:00
|
|
|
unsigned destroy_flags_;
|
2017-03-26 23:27:04 +08:00
|
|
|
|
|
|
|
// This models mutex profiling subsystem.
|
|
|
|
static Mutex prof_mu_;
|
|
|
|
static int prof_data_;
|
|
|
|
|
|
|
|
void LockImpl(bool prof = true) {
|
|
|
|
while (!TryLockImpl())
|
|
|
|
usleep(100);
|
|
|
|
if (prof && prof_)
|
|
|
|
Prof();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TryLockImpl() {
|
|
|
|
return !locked_.exchange(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnlockImpl() {
|
|
|
|
locked_.store(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Prof() {
|
|
|
|
// This happens inside of mutex lock annotations.
|
|
|
|
__tsan_mutex_pre_divert(this, 0);
|
|
|
|
prof_mu_.Lock();
|
|
|
|
prof_data_++;
|
|
|
|
prof_mu_.Unlock();
|
|
|
|
__tsan_mutex_post_divert(this, 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-13 17:37:51 +08:00
|
|
|
Mutex Mutex::prof_mu_(false, __tsan_mutex_linker_init);
|
2017-03-26 23:27:04 +08:00
|
|
|
int Mutex::prof_data_;
|