1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wthread-safety-verbose -Wno-thread-safety-negative -fcxx-exceptions %s 2 3 #define LOCKABLE __attribute__ ((lockable)) 4 #define SCOPED_LOCKABLE __attribute__ ((scoped_lockable)) 5 #define GUARDED_BY(x) __attribute__ ((guarded_by(x))) 6 #define GUARDED_VAR __attribute__ ((guarded_var)) 7 #define PT_GUARDED_BY(x) __attribute__ ((pt_guarded_by(x))) 8 #define PT_GUARDED_VAR __attribute__ ((pt_guarded_var)) 9 #define ACQUIRED_AFTER(...) __attribute__ ((acquired_after(__VA_ARGS__))) 10 #define ACQUIRED_BEFORE(...) __attribute__ ((acquired_before(__VA_ARGS__))) 11 #define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__ ((exclusive_lock_function(__VA_ARGS__))) 12 #define SHARED_LOCK_FUNCTION(...) __attribute__ ((shared_lock_function(__VA_ARGS__))) 13 #define ASSERT_EXCLUSIVE_LOCK(...) __attribute__ ((assert_exclusive_lock(__VA_ARGS__))) 14 #define ASSERT_SHARED_LOCK(...) __attribute__ ((assert_shared_lock(__VA_ARGS__))) 15 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__ ((exclusive_trylock_function(__VA_ARGS__))) 16 #define SHARED_TRYLOCK_FUNCTION(...) __attribute__ ((shared_trylock_function(__VA_ARGS__))) 17 #define UNLOCK_FUNCTION(...) __attribute__ ((unlock_function(__VA_ARGS__))) 18 #define LOCK_RETURNED(x) __attribute__ ((lock_returned(x))) 19 #define LOCKS_EXCLUDED(...) __attribute__ ((locks_excluded(__VA_ARGS__))) 20 #define EXCLUSIVE_LOCKS_REQUIRED(...) \ 21 __attribute__ ((exclusive_locks_required(__VA_ARGS__))) 22 #define SHARED_LOCKS_REQUIRED(...) \ 23 __attribute__ ((shared_locks_required(__VA_ARGS__))) 24 #define NO_THREAD_SAFETY_ANALYSIS __attribute__ ((no_thread_safety_analysis)) 25 26 27 class __attribute__((lockable)) Mutex { 28 public: 29 void Lock() __attribute__((exclusive_lock_function)); 30 void ReaderLock() __attribute__((shared_lock_function)); 31 void Unlock() __attribute__((unlock_function)); 32 bool TryLock() __attribute__((exclusive_trylock_function(true))); 33 bool ReaderTryLock() __attribute__((shared_trylock_function(true))); 34 void LockWhen(const int &cond) __attribute__((exclusive_lock_function)); 35 36 // for negative capabilities 37 const Mutex& operator!() const { return *this; } 38 39 void AssertHeld() ASSERT_EXCLUSIVE_LOCK(); 40 void AssertReaderHeld() ASSERT_SHARED_LOCK(); 41 }; 42 43 44 class Test { 45 Mutex mu; 46 int a GUARDED_BY(mu); // expected-note3 {{Guarded_by declared here.}} 47 48 void foo1() EXCLUSIVE_LOCKS_REQUIRED(mu); 49 void foo2() SHARED_LOCKS_REQUIRED(mu); 50 void foo3() LOCKS_EXCLUDED(mu); 51 52 void test1() { // expected-note {{Thread warning in function 'test1'}} 53 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} 54 } 55 56 void test2() { // expected-note {{Thread warning in function 'test2'}} 57 int b = a; // expected-warning {{reading variable 'a' requires holding mutex 'mu'}} 58 } 59 60 void test3() { // expected-note {{Thread warning in function 'test3'}} 61 foo1(); // expected-warning {{calling function 'foo1' requires holding mutex 'mu' exclusively}} 62 } 63 64 void test4() { // expected-note {{Thread warning in function 'test4'}} 65 foo2(); // expected-warning {{calling function 'foo2' requires holding mutex 'mu'}} 66 } 67 68 void test5() { // expected-note {{Thread warning in function 'test5'}} 69 mu.ReaderLock(); 70 foo1(); // expected-warning {{calling function 'foo1' requires holding mutex 'mu' exclusively}} 71 mu.Unlock(); 72 } 73 74 void test6() { // expected-note {{Thread warning in function 'test6'}} 75 mu.ReaderLock(); 76 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} 77 mu.Unlock(); 78 } 79 80 void test7() { // expected-note {{Thread warning in function 'test7'}} 81 mu.Lock(); 82 foo3(); // expected-warning {{cannot call function 'foo3' while mutex 'mu' is held}} 83 mu.Unlock(); 84 } 85 }; 86 87