1 #ifndef BENCHMARK_MUTEX_H_
2 #define BENCHMARK_MUTEX_H_
4 #include <condition_variable>
9 // Enable thread safety attributes only with clang.
10 // The attributes can be safely erased when compiling with other compilers.
11 #if defined(HAVE_THREAD_SAFETY_ATTRIBUTES)
12 #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
14 #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
17 #define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
19 #define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
21 #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
23 #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
25 #define ACQUIRED_BEFORE(...) \
26 THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
28 #define ACQUIRED_AFTER(...) \
29 THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
31 #define REQUIRES(...) \
32 THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
34 #define REQUIRES_SHARED(...) \
35 THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
37 #define ACQUIRE(...) \
38 THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
40 #define ACQUIRE_SHARED(...) \
41 THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
43 #define RELEASE(...) \
44 THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
46 #define RELEASE_SHARED(...) \
47 THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
49 #define TRY_ACQUIRE(...) \
50 THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
52 #define TRY_ACQUIRE_SHARED(...) \
53 THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
55 #define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
57 #define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
59 #define ASSERT_SHARED_CAPABILITY(x) \
60 THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
62 #define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
64 #define NO_THREAD_SAFETY_ANALYSIS \
65 THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
69 typedef std::condition_variable Condition
;
71 // NOTE: Wrappers for std::mutex and std::unique_lock are provided so that
72 // we can annotate them with thread safety attributes and use the
73 // -Wthread-safety warning with clang. The standard library types cannot be
74 // used directly because they do not provided the required annotations.
75 class CAPABILITY("mutex") Mutex
{
79 void lock() ACQUIRE() { mut_
.lock(); }
80 void unlock() RELEASE() { mut_
.unlock(); }
81 std::mutex
& native_handle() { return mut_
; }
87 class SCOPED_CAPABILITY MutexLock
{
88 typedef std::unique_lock
<std::mutex
> MutexLockImp
;
91 MutexLock(Mutex
& m
) ACQUIRE(m
) : ml_(m
.native_handle()) {}
92 ~MutexLock() RELEASE() {}
93 MutexLockImp
& native_handle() { return ml_
; }
101 Barrier(int num_threads
) : running_threads_(num_threads
) {}
103 // Called by each thread
104 bool wait() EXCLUDES(lock_
) {
105 bool last_thread
= false;
108 last_thread
= createBarrier(ml
);
110 if (last_thread
) phase_condition_
.notify_all();
114 void removeThread() EXCLUDES(lock_
) {
117 if (entered_
!= 0) phase_condition_
.notify_all();
122 Condition phase_condition_
;
123 int running_threads_
;
125 // State for barrier management
126 int phase_number_
= 0;
127 int entered_
= 0; // Number of threads that have entered this barrier
129 // Enter the barrier and wait until all other threads have also
130 // entered the barrier. Returns iff this is the last thread to
131 // enter the barrier.
132 bool createBarrier(MutexLock
& ml
) REQUIRES(lock_
) {
133 CHECK_LT(entered_
, running_threads_
);
135 if (entered_
< running_threads_
) {
136 // Wait for all threads to enter
137 int phase_number_cp
= phase_number_
;
138 auto cb
= [this, phase_number_cp
]() {
139 return this->phase_number_
> phase_number_cp
||
140 entered_
== running_threads_
; // A thread has aborted in error
142 phase_condition_
.wait(ml
.native_handle(), cb
);
143 if (phase_number_
> phase_number_cp
) return false;
144 // else (running_threads_ == entered_) and we are the last thread.
146 // Last thread has reached the barrier
153 } // end namespace benchmark
155 #endif // BENCHMARK_MUTEX_H_