|
24 | 24 | __attribute__ ((shared_locks_required(__VA_ARGS__)))
|
25 | 25 | #define NO_THREAD_SAFETY_ANALYSIS __attribute__ ((no_thread_safety_analysis))
|
26 | 26 |
|
| 27 | +#define __READ_ONCE(x) (*(const volatile __typeof__(x) *)&(x)) |
| 28 | +#define __WRITE_ONCE(x, val) do { *(volatile __typeof__(x) *)&(x) = (val); } while (0) |
| 29 | + |
27 | 30 | // Define the mutex struct.
|
28 | 31 | // Simplified only for test purpose.
|
29 | 32 | struct LOCKABLE Mutex {};
|
@@ -142,9 +145,25 @@ int main(void) {
|
142 | 145 | (void)(get_value(b_) == 1);
|
143 | 146 | mutex_unlock(foo_.mu_);
|
144 | 147 |
|
| 148 | + a_ = 0; // expected-warning{{writing variable 'a_' requires holding mutex 'foo_.mu_'}} |
| 149 | + __WRITE_ONCE(a_, 0); // expected-warning{{writing variable 'a_' requires holding mutex 'foo_.mu_'}} |
| 150 | + (void)(a_ == 0); // expected-warning{{reading variable 'a_' requires holding mutex 'foo_.mu_'}} |
| 151 | + (void)(__READ_ONCE(a_) == 0); // expected-warning{{reading variable 'a_' requires holding mutex 'foo_.mu_'}} |
| 152 | + *b_ = 0; // expected-warning{{writing the value pointed to by 'b_' requires holding mutex 'foo_.mu_' exclusively}} |
| 153 | + __WRITE_ONCE(*b_, 0); // expected-warning{{writing the value pointed to by 'b_' requires holding mutex 'foo_.mu_' exclusively}} |
| 154 | + (void)(*b_ == 0); // expected-warning{{reading the value pointed to by 'b_' requires holding mutex 'foo_.mu_'}} |
| 155 | + (void)(__READ_ONCE(*b_) == 0); // expected-warning{{reading the value pointed to by 'b_' requires holding mutex 'foo_.mu_'}} |
145 | 156 | c_ = 0; // expected-warning{{writing variable 'c_' requires holding any mutex exclusively}}
|
146 | 157 | (void)(*d_ == 0); // expected-warning{{reading the value pointed to by 'd_' requires holding any mutex}}
|
147 | 158 | mutex_exclusive_lock(foo_.mu_);
|
| 159 | + a_ = 0; |
| 160 | + __WRITE_ONCE(a_, 0); |
| 161 | + (void)(a_ == 0); |
| 162 | + (void)(__READ_ONCE(a_) == 0); |
| 163 | + *b_ = 0; |
| 164 | + __WRITE_ONCE(*b_, 0); |
| 165 | + (void)(*b_ == 0); |
| 166 | + (void)(__READ_ONCE(*b_) == 0); |
148 | 167 | c_ = 1;
|
149 | 168 | (void)(*d_ == 1);
|
150 | 169 | mutex_unlock(foo_.mu_);
|
|
0 commit comments