1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -Wthread-safety-beta %s 2f4a2713aSLionel Sambuc 3f4a2713aSLionel Sambuc #define LOCKABLE __attribute__ ((lockable)) 4f4a2713aSLionel Sambuc #define SCOPED_LOCKABLE __attribute__ ((scoped_lockable)) 5f4a2713aSLionel Sambuc #define GUARDED_BY(x) __attribute__ ((guarded_by(x))) 6f4a2713aSLionel Sambuc #define GUARDED_VAR __attribute__ ((guarded_var)) 7f4a2713aSLionel Sambuc #define PT_GUARDED_BY(x) __attribute__ ((pt_guarded_by(x))) 8f4a2713aSLionel Sambuc #define PT_GUARDED_VAR __attribute__ ((pt_guarded_var)) 9f4a2713aSLionel Sambuc #define ACQUIRED_AFTER(...) __attribute__ ((acquired_after(__VA_ARGS__))) 10f4a2713aSLionel Sambuc #define ACQUIRED_BEFORE(...) __attribute__ ((acquired_before(__VA_ARGS__))) 11f4a2713aSLionel Sambuc #define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__ ((exclusive_lock_function(__VA_ARGS__))) 12f4a2713aSLionel Sambuc #define SHARED_LOCK_FUNCTION(...) __attribute__ ((shared_lock_function(__VA_ARGS__))) 13f4a2713aSLionel Sambuc #define ASSERT_EXCLUSIVE_LOCK(...) __attribute__ ((assert_exclusive_lock(__VA_ARGS__))) 14f4a2713aSLionel Sambuc #define ASSERT_SHARED_LOCK(...) __attribute__ ((assert_shared_lock(__VA_ARGS__))) 15f4a2713aSLionel Sambuc #define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__ ((exclusive_trylock_function(__VA_ARGS__))) 16f4a2713aSLionel Sambuc #define SHARED_TRYLOCK_FUNCTION(...) __attribute__ ((shared_trylock_function(__VA_ARGS__))) 17f4a2713aSLionel Sambuc #define UNLOCK_FUNCTION(...) __attribute__ ((unlock_function(__VA_ARGS__))) 18f4a2713aSLionel Sambuc #define LOCK_RETURNED(x) __attribute__ ((lock_returned(x))) 19f4a2713aSLionel Sambuc #define LOCKS_EXCLUDED(...) __attribute__ ((locks_excluded(__VA_ARGS__))) 20f4a2713aSLionel Sambuc #define EXCLUSIVE_LOCKS_REQUIRED(...) \ 21f4a2713aSLionel Sambuc __attribute__ ((exclusive_locks_required(__VA_ARGS__))) 22f4a2713aSLionel Sambuc #define SHARED_LOCKS_REQUIRED(...) \ 23f4a2713aSLionel Sambuc __attribute__ ((shared_locks_required(__VA_ARGS__))) 24f4a2713aSLionel Sambuc #define NO_THREAD_SAFETY_ANALYSIS __attribute__ ((no_thread_safety_analysis)) 25f4a2713aSLionel Sambuc 26f4a2713aSLionel Sambuc // Define the mutex struct. 27f4a2713aSLionel Sambuc // Simplified only for test purpose. 28f4a2713aSLionel Sambuc struct LOCKABLE Mutex {}; 29f4a2713aSLionel Sambuc 30f4a2713aSLionel Sambuc struct Foo { 31f4a2713aSLionel Sambuc struct Mutex *mu_; 32f4a2713aSLionel Sambuc }; 33f4a2713aSLionel Sambuc 34*0a6a1f1dSLionel Sambuc // Declare mutex lock/unlock functions. 35*0a6a1f1dSLionel Sambuc void mutex_exclusive_lock(struct Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu); 36*0a6a1f1dSLionel Sambuc void mutex_shared_lock(struct Mutex *mu) SHARED_LOCK_FUNCTION(mu); 37*0a6a1f1dSLionel Sambuc void mutex_unlock(struct Mutex *mu) UNLOCK_FUNCTION(mu); 38*0a6a1f1dSLionel Sambuc void mutex_shared_unlock(struct Mutex *mu) __attribute__((release_shared_capability(mu))); 39*0a6a1f1dSLionel Sambuc void mutex_exclusive_unlock(struct Mutex *mu) __attribute__((release_capability(mu))); 40f4a2713aSLionel Sambuc 41f4a2713aSLionel Sambuc // Define global variables. 42f4a2713aSLionel Sambuc struct Mutex mu1; 43f4a2713aSLionel Sambuc struct Mutex mu2 ACQUIRED_AFTER(mu1); 44f4a2713aSLionel Sambuc struct Foo foo_ = {&mu1}; 45f4a2713aSLionel Sambuc int a_ GUARDED_BY(foo_.mu_); 46f4a2713aSLionel Sambuc int *b_ PT_GUARDED_BY(foo_.mu_) = &a_; 47f4a2713aSLionel Sambuc int c_ GUARDED_VAR; 48f4a2713aSLionel Sambuc int *d_ PT_GUARDED_VAR = &c_; 49f4a2713aSLionel Sambuc 50f4a2713aSLionel Sambuc // Define test functions. Foo_fun1(int i)51f4a2713aSLionel Sambucint Foo_fun1(int i) SHARED_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1) { 52f4a2713aSLionel Sambuc return i; 53f4a2713aSLionel Sambuc } 54f4a2713aSLionel Sambuc Foo_fun2(int i)55f4a2713aSLionel Sambucint Foo_fun2(int i) EXCLUSIVE_LOCKS_REQUIRED(mu2) SHARED_LOCKS_REQUIRED(mu1) { 56f4a2713aSLionel Sambuc return i; 57f4a2713aSLionel Sambuc } 58f4a2713aSLionel Sambuc Foo_func3(int i)59f4a2713aSLionel Sambucint Foo_func3(int i) LOCKS_EXCLUDED(mu1, mu2) { 60f4a2713aSLionel Sambuc return i; 61f4a2713aSLionel Sambuc } 62f4a2713aSLionel Sambuc Bar_fun1(int i)63f4a2713aSLionel Sambucstatic int Bar_fun1(int i) EXCLUSIVE_LOCKS_REQUIRED(mu1) { 64f4a2713aSLionel Sambuc return i; 65f4a2713aSLionel Sambuc } 66f4a2713aSLionel Sambuc set_value(int * a,int value)67f4a2713aSLionel Sambucvoid set_value(int *a, int value) EXCLUSIVE_LOCKS_REQUIRED(foo_.mu_) { 68f4a2713aSLionel Sambuc *a = value; 69f4a2713aSLionel Sambuc } 70f4a2713aSLionel Sambuc get_value(int * p)71f4a2713aSLionel Sambucint get_value(int *p) SHARED_LOCKS_REQUIRED(foo_.mu_){ 72f4a2713aSLionel Sambuc return *p; 73f4a2713aSLionel Sambuc } 74f4a2713aSLionel Sambuc main()75f4a2713aSLionel Sambucint main() { 76f4a2713aSLionel Sambuc 77*0a6a1f1dSLionel Sambuc Foo_fun1(1); // expected-warning{{calling function 'Foo_fun1' requires holding mutex 'mu2'}} \ 78*0a6a1f1dSLionel Sambuc expected-warning{{calling function 'Foo_fun1' requires holding mutex 'mu1' exclusively}} 79f4a2713aSLionel Sambuc 80f4a2713aSLionel Sambuc mutex_exclusive_lock(&mu1); 81f4a2713aSLionel Sambuc mutex_shared_lock(&mu2); 82f4a2713aSLionel Sambuc Foo_fun1(1); 83f4a2713aSLionel Sambuc 84*0a6a1f1dSLionel Sambuc mutex_shared_lock(&mu1); // expected-warning{{acquiring mutex 'mu1' that is already held}} 85f4a2713aSLionel Sambuc mutex_unlock(&mu1); 86f4a2713aSLionel Sambuc mutex_unlock(&mu2); 87f4a2713aSLionel Sambuc mutex_shared_lock(&mu1); 88f4a2713aSLionel Sambuc mutex_exclusive_lock(&mu2); 89f4a2713aSLionel Sambuc Foo_fun2(2); 90f4a2713aSLionel Sambuc 91f4a2713aSLionel Sambuc mutex_unlock(&mu2); 92f4a2713aSLionel Sambuc mutex_unlock(&mu1); 93f4a2713aSLionel Sambuc mutex_exclusive_lock(&mu1); 94f4a2713aSLionel Sambuc Bar_fun1(3); 95f4a2713aSLionel Sambuc mutex_unlock(&mu1); 96f4a2713aSLionel Sambuc 97f4a2713aSLionel Sambuc mutex_exclusive_lock(&mu1); 98*0a6a1f1dSLionel Sambuc Foo_func3(4); // expected-warning{{cannot call function 'Foo_func3' while mutex 'mu1' is held}} 99f4a2713aSLionel Sambuc mutex_unlock(&mu1); 100f4a2713aSLionel Sambuc 101f4a2713aSLionel Sambuc Foo_func3(5); 102f4a2713aSLionel Sambuc 103*0a6a1f1dSLionel Sambuc set_value(&a_, 0); // expected-warning{{calling function 'set_value' requires holding mutex 'foo_.mu_' exclusively}} 104*0a6a1f1dSLionel Sambuc get_value(b_); // expected-warning{{calling function 'get_value' requires holding mutex 'foo_.mu_'}} 105f4a2713aSLionel Sambuc mutex_exclusive_lock(foo_.mu_); 106f4a2713aSLionel Sambuc set_value(&a_, 1); 107f4a2713aSLionel Sambuc mutex_unlock(foo_.mu_); 108f4a2713aSLionel Sambuc mutex_shared_lock(foo_.mu_); 109f4a2713aSLionel Sambuc (void)(get_value(b_) == 1); 110f4a2713aSLionel Sambuc mutex_unlock(foo_.mu_); 111f4a2713aSLionel Sambuc 112*0a6a1f1dSLionel Sambuc c_ = 0; // expected-warning{{writing variable 'c_' requires holding any mutex exclusively}} 113*0a6a1f1dSLionel Sambuc (void)(*d_ == 0); // expected-warning{{reading the value pointed to by 'd_' requires holding any mutex}} 114f4a2713aSLionel Sambuc mutex_exclusive_lock(foo_.mu_); 115f4a2713aSLionel Sambuc c_ = 1; 116f4a2713aSLionel Sambuc (void)(*d_ == 1); 117f4a2713aSLionel Sambuc mutex_unlock(foo_.mu_); 118f4a2713aSLionel Sambuc 119*0a6a1f1dSLionel Sambuc mutex_exclusive_lock(&mu1); 120*0a6a1f1dSLionel Sambuc mutex_shared_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' using shared access, expected exclusive access}} 121*0a6a1f1dSLionel Sambuc mutex_exclusive_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' that was not held}} 122*0a6a1f1dSLionel Sambuc 123*0a6a1f1dSLionel Sambuc mutex_shared_lock(&mu1); 124*0a6a1f1dSLionel Sambuc mutex_exclusive_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' using exclusive access, expected shared access}} 125*0a6a1f1dSLionel Sambuc mutex_shared_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' that was not held}} 126*0a6a1f1dSLionel Sambuc 127f4a2713aSLionel Sambuc return 0; 128f4a2713aSLionel Sambuc } 129