1#include "test.h"
2#include <atomic>
3#include <vector>
4#include <sanitizer/tsan_interface.h>
5
6// A very primitive mutex annotated with tsan annotations.
7class Mutex {
8 public:
9 Mutex(bool prof, unsigned create_flags, unsigned destroy_flags=0)
10 : prof_(prof)
11 , locked_(false)
12 , seq_(0)
13 , destroy_flags_(destroy_flags) {
14 __tsan_mutex_create(addr: this, flags: create_flags);
15 }
16
17 ~Mutex() {
18 __tsan_mutex_destroy(addr: this, flags: destroy_flags_);
19 }
20
21 void Lock() {
22 __tsan_mutex_pre_lock(addr: this, flags: 0);
23 LockImpl();
24 __tsan_mutex_post_lock(addr: this, flags: 0, recursion: 0);
25 }
26
27 bool TryLock() {
28 __tsan_mutex_pre_lock(addr: this, flags: __tsan_mutex_try_lock);
29 bool ok = TryLockImpl();
30 __tsan_mutex_post_lock(addr: this, flags: __tsan_mutex_try_lock |
31 (ok ? 0 : __tsan_mutex_try_lock_failed), recursion: 0);
32 return ok;
33 }
34
35 void Unlock() {
36 __tsan_mutex_pre_unlock(addr: this, flags: 0);
37 UnlockImpl();
38 __tsan_mutex_post_unlock(addr: this, flags: 0);
39 }
40
41 void Wait() {
42 for (int seq = seq_; seq == seq_;) {
43 Unlock();
44 usleep(useconds: 100);
45 Lock();
46 }
47 }
48
49 void Broadcast() {
50 __tsan_mutex_pre_signal(addr: this, flags: 0);
51 LockImpl(prof: false);
52 seq_++;
53 UnlockImpl();
54 __tsan_mutex_post_signal(addr: this, flags: 0);
55 }
56
57 private:
58 const bool prof_;
59 std::atomic<bool> locked_;
60 int seq_;
61 unsigned destroy_flags_;
62
63 // This models mutex profiling subsystem.
64 static Mutex prof_mu_;
65 static int prof_data_;
66
67 void LockImpl(bool prof = true) {
68 while (!TryLockImpl())
69 usleep(useconds: 100);
70 if (prof && prof_)
71 Prof();
72 }
73
74 bool TryLockImpl() {
75 return !locked_.exchange(true);
76 }
77
78 void UnlockImpl() {
79 locked_.store(false);
80 }
81
82 void Prof() {
83 // This happens inside of mutex lock annotations.
84 __tsan_mutex_pre_divert(addr: this, flags: 0);
85 prof_mu_.Lock();
86 prof_data_++;
87 prof_mu_.Unlock();
88 __tsan_mutex_post_divert(addr: this, flags: 0);
89 }
90};
91
92Mutex Mutex::prof_mu_(false, __tsan_mutex_linker_init);
93int Mutex::prof_data_;
94

source code of compiler-rt/test/tsan/custom_mutex.h