bart | b43825e | 2010-03-07 19:59:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Test program with happens-before / happens-after annotations that triggers |
| 3 | * a data race. The data race will only be reported if happens-after |
| 4 | * annotations that occur in different threads are not totally ordered. Or: |
| 5 | * this is a test for the implementation of ordering annotations. |
| 6 | */ |
| 7 | |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <pthread.h> |
| 11 | #include "unified_annotations.h" |
| 12 | |
| 13 | |
| 14 | static int s_i; |
| 15 | |
| 16 | |
| 17 | static void* thread_func(void* arg) |
| 18 | { |
| 19 | int i; |
| 20 | |
bart | a7103ba | 2010-09-02 09:38:55 +0000 | [diff] [blame] | 21 | U_ANNOTATE_HAPPENS_AFTER(&s_i); |
bart | b43825e | 2010-03-07 19:59:35 +0000 | [diff] [blame] | 22 | i = s_i; |
bart | a7103ba | 2010-09-02 09:38:55 +0000 | [diff] [blame] | 23 | U_ANNOTATE_HAPPENS_AFTER(&s_i); |
bart | b43825e | 2010-03-07 19:59:35 +0000 | [diff] [blame] | 24 | *(int*)arg = i; |
| 25 | return NULL; |
| 26 | } |
| 27 | |
| 28 | int main(int argc, char** argv) |
| 29 | { |
bart | c2f5a53 | 2011-07-30 16:50:33 +0000 | [diff] [blame] | 30 | const struct timespec delay = { 0, 100 * 1000 * 1000 }; |
bart | b43825e | 2010-03-07 19:59:35 +0000 | [diff] [blame] | 31 | pthread_t tid[2]; |
| 32 | int result[2]; |
| 33 | |
bart | a7103ba | 2010-09-02 09:38:55 +0000 | [diff] [blame] | 34 | U_ANNOTATE_HAPPENS_BEFORE(&s_i); |
bart | b43825e | 2010-03-07 19:59:35 +0000 | [diff] [blame] | 35 | pthread_create(&tid[0], 0, thread_func, &result[0]); |
bart | c2f5a53 | 2011-07-30 16:50:33 +0000 | [diff] [blame] | 36 | pthread_create(&tid[1], 0, thread_func, &result[1]); |
| 37 | |
| 38 | nanosleep(&delay, 0); |
| 39 | |
bart | b43825e | 2010-03-07 19:59:35 +0000 | [diff] [blame] | 40 | s_i = 1; |
| 41 | |
| 42 | pthread_join(tid[0], NULL); |
bart | c2f5a53 | 2011-07-30 16:50:33 +0000 | [diff] [blame] | 43 | pthread_join(tid[1], NULL); |
bart | b43825e | 2010-03-07 19:59:35 +0000 | [diff] [blame] | 44 | |
| 45 | fprintf(stderr, "Done.\n"); |
| 46 | |
| 47 | return 0; |
| 48 | } |