DeLesley Hutchins | e8d2a9d | 2014-08-14 21:54:34 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-beta -Wthread-safety-negative -fcxx-exceptions %s |
DeLesley Hutchins | 3efd049 | 2014-08-04 22:13:06 +0000 | [diff] [blame] | 2 | |
| 3 | // FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 -Wc++98-compat %s |
| 4 | // FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety %s |
| 5 | |
| 6 | #define LOCKABLE __attribute__ ((lockable)) |
| 7 | #define SCOPED_LOCKABLE __attribute__ ((scoped_lockable)) |
| 8 | #define GUARDED_BY(x) __attribute__ ((guarded_by(x))) |
| 9 | #define GUARDED_VAR __attribute__ ((guarded_var)) |
| 10 | #define PT_GUARDED_BY(x) __attribute__ ((pt_guarded_by(x))) |
| 11 | #define PT_GUARDED_VAR __attribute__ ((pt_guarded_var)) |
| 12 | #define ACQUIRED_AFTER(...) __attribute__ ((acquired_after(__VA_ARGS__))) |
| 13 | #define ACQUIRED_BEFORE(...) __attribute__ ((acquired_before(__VA_ARGS__))) |
| 14 | #define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__ ((exclusive_lock_function(__VA_ARGS__))) |
| 15 | #define SHARED_LOCK_FUNCTION(...) __attribute__ ((shared_lock_function(__VA_ARGS__))) |
| 16 | #define ASSERT_EXCLUSIVE_LOCK(...) __attribute__ ((assert_exclusive_lock(__VA_ARGS__))) |
| 17 | #define ASSERT_SHARED_LOCK(...) __attribute__ ((assert_shared_lock(__VA_ARGS__))) |
| 18 | #define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__ ((exclusive_trylock_function(__VA_ARGS__))) |
| 19 | #define SHARED_TRYLOCK_FUNCTION(...) __attribute__ ((shared_trylock_function(__VA_ARGS__))) |
| 20 | #define UNLOCK_FUNCTION(...) __attribute__ ((unlock_function(__VA_ARGS__))) |
| 21 | #define LOCK_RETURNED(x) __attribute__ ((lock_returned(x))) |
| 22 | #define LOCKS_EXCLUDED(...) __attribute__ ((locks_excluded(__VA_ARGS__))) |
| 23 | #define EXCLUSIVE_LOCKS_REQUIRED(...) \ |
| 24 | __attribute__ ((exclusive_locks_required(__VA_ARGS__))) |
| 25 | #define SHARED_LOCKS_REQUIRED(...) \ |
| 26 | __attribute__ ((shared_locks_required(__VA_ARGS__))) |
| 27 | #define NO_THREAD_SAFETY_ANALYSIS __attribute__ ((no_thread_safety_analysis)) |
| 28 | |
| 29 | |
| 30 | class __attribute__((lockable)) Mutex { |
| 31 | public: |
| 32 | void Lock() __attribute__((exclusive_lock_function)); |
| 33 | void ReaderLock() __attribute__((shared_lock_function)); |
| 34 | void Unlock() __attribute__((unlock_function)); |
| 35 | bool TryLock() __attribute__((exclusive_trylock_function(true))); |
| 36 | bool ReaderTryLock() __attribute__((shared_trylock_function(true))); |
| 37 | void LockWhen(const int &cond) __attribute__((exclusive_lock_function)); |
| 38 | |
| 39 | // for negative capabilities |
| 40 | const Mutex& operator!() const { return *this; } |
| 41 | |
| 42 | void AssertHeld() ASSERT_EXCLUSIVE_LOCK(); |
| 43 | void AssertReaderHeld() ASSERT_SHARED_LOCK(); |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | namespace SimpleTest { |
| 48 | |
| 49 | class Bar { |
| 50 | Mutex mu; |
| 51 | int a GUARDED_BY(mu); |
| 52 | |
| 53 | public: |
| 54 | void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) { |
| 55 | mu.Lock(); |
| 56 | a = 0; |
| 57 | mu.Unlock(); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | class Foo { |
| 63 | Mutex mu; |
| 64 | int a GUARDED_BY(mu); |
| 65 | |
| 66 | public: |
| 67 | void foo() { |
| 68 | mu.Lock(); // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}} |
| 69 | baz(); // expected-warning {{cannot call function 'baz' while mutex 'mu' is held}} |
| 70 | bar(); |
| 71 | mu.Unlock(); |
| 72 | } |
| 73 | |
| 74 | void bar() { |
| 75 | baz(); // expected-warning {{calling function 'baz' requires holding '!mu'}} |
| 76 | } |
| 77 | |
| 78 | void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) { |
| 79 | mu.Lock(); |
| 80 | a = 0; |
| 81 | mu.Unlock(); |
| 82 | } |
| 83 | |
| 84 | void test() { |
| 85 | Bar b; |
| 86 | b.baz(); // no warning -- in different class. |
| 87 | } |
| 88 | |
| 89 | void test2() { |
| 90 | mu.Lock(); // expected-warning {{acquiring mutex 'mu' requires negative capability '!mu'}} |
| 91 | a = 0; |
| 92 | mu.Unlock(); |
| 93 | baz(); // no warning -- !mu in set. |
| 94 | } |
| 95 | |
| 96 | void test3() EXCLUSIVE_LOCKS_REQUIRED(!mu) { |
| 97 | mu.Lock(); |
| 98 | a = 0; |
| 99 | mu.Unlock(); |
| 100 | baz(); // no warning -- !mu in set. |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | } // end namespace SimpleTest |
Richard Trieu | 948bd5e | 2015-03-18 21:52:47 +0000 | [diff] [blame] | 105 | |
| 106 | namespace DoubleAttribute { |
| 107 | |
| 108 | struct Foo { |
| 109 | Mutex &mutex(); |
| 110 | }; |
| 111 | |
| 112 | template <typename A> |
| 113 | class TemplateClass { |
| 114 | template <typename B> |
| 115 | static void Function(Foo *F) |
| 116 | EXCLUSIVE_LOCKS_REQUIRED(F->mutex()) UNLOCK_FUNCTION(F->mutex()) {} |
| 117 | }; |
| 118 | |
| 119 | void test() { TemplateClass<int> TC; } |
| 120 | |
| 121 | } // end namespace DoubleAttribute |