1 // RUN: %clang -fsyntax-only -verify -Wthread-safety -Wthread-safety-beta -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 // Define the mutex struct. 27 // Simplified only for test purpose. 28 struct LOCKABLE Mutex {}; 29 30 struct Foo { 31 struct Mutex *mu_; 32 }; 33 34 // Define mutex lock/unlock functions. 35 void mutex_exclusive_lock(struct Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu) { 36 } 37 38 void mutex_shared_lock(struct Mutex *mu) SHARED_LOCK_FUNCTION(mu) { 39 } 40 41 void mutex_unlock(struct Mutex *mu) UNLOCK_FUNCTION(mu) { 42 } 43 44 // Define global variables. 45 struct Mutex mu1; 46 struct Mutex mu2 ACQUIRED_AFTER(mu1); 47 struct Foo foo_ = {&mu1}; 48 int a_ GUARDED_BY(foo_.mu_); 49 int *b_ PT_GUARDED_BY(foo_.mu_) = &a_; 50 int c_ GUARDED_VAR; 51 int *d_ PT_GUARDED_VAR = &c_; 52 53 // Define test functions. 54 int Foo_fun1(int i) SHARED_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1) { 55 return i; 56 } 57 58 int Foo_fun2(int i) EXCLUSIVE_LOCKS_REQUIRED(mu2) SHARED_LOCKS_REQUIRED(mu1) { 59 return i; 60 } 61 62 int Foo_func3(int i) LOCKS_EXCLUDED(mu1, mu2) { 63 return i; 64 } 65 66 static int Bar_fun1(int i) EXCLUSIVE_LOCKS_REQUIRED(mu1) { 67 return i; 68 } 69 70 void set_value(int *a, int value) EXCLUSIVE_LOCKS_REQUIRED(foo_.mu_) { 71 *a = value; 72 } 73 74 int get_value(int *p) SHARED_LOCKS_REQUIRED(foo_.mu_){ 75 return *p; 76 } 77 78 int main() { 79 80 Foo_fun1(1); // expected-warning{{calling function 'Foo_fun1' requires shared lock on 'mu2'}} \ 81 expected-warning{{calling function 'Foo_fun1' requires exclusive lock on 'mu1'}} 82 83 mutex_exclusive_lock(&mu1); 84 mutex_shared_lock(&mu2); 85 Foo_fun1(1); 86 87 mutex_shared_lock(&mu1); // expected-warning{{locking 'mu1' that is already locked}} 88 mutex_unlock(&mu1); 89 mutex_unlock(&mu2); 90 mutex_shared_lock(&mu1); 91 mutex_exclusive_lock(&mu2); 92 Foo_fun2(2); 93 94 mutex_unlock(&mu2); 95 mutex_unlock(&mu1); 96 mutex_exclusive_lock(&mu1); 97 Bar_fun1(3); 98 mutex_unlock(&mu1); 99 100 mutex_exclusive_lock(&mu1); 101 Foo_func3(4); // expected-warning{{cannot call function 'Foo_func3' while mutex 'mu1' is locked}} 102 mutex_unlock(&mu1); 103 104 Foo_func3(5); 105 106 set_value(&a_, 0); // expected-warning{{calling function 'setA' requires exclusive lock on 'foo_.mu_'}} 107 get_value(b_); // expected-warning{{calling function 'getB' requires shared lock on 'foo_.mu_'}} 108 mutex_exclusive_lock(foo_.mu_); 109 set_value(&a_, 1); 110 mutex_unlock(foo_.mu_); 111 mutex_shared_lock(foo_.mu_); 112 (void)(get_value(b_) == 1); 113 mutex_unlock(foo_.mu_); 114 115 c_ = 0; // expected-warning{{writing variable 'c_' requires locking any mutex exclusively}} 116 (void)(*d_ == 0); // expected-warning{{reading the value pointed to by 'd_' requires locking any mutex}} 117 mutex_exclusive_lock(foo_.mu_); 118 c_ = 1; 119 (void)(*d_ == 1); 120 mutex_unlock(foo_.mu_); 121 122 return 0; 123 } 124