blob: 4aa5a380e861fbdd2f11787b4d16ffd5b3a3facc [file] [log] [blame]
bartb43825e2010-03-07 19:59:35 +00001/*
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
14static int s_i;
15
16
17static void* thread_func(void* arg)
18{
19 int i;
20
barta7103ba2010-09-02 09:38:55 +000021 U_ANNOTATE_HAPPENS_AFTER(&s_i);
bartb43825e2010-03-07 19:59:35 +000022 i = s_i;
barta7103ba2010-09-02 09:38:55 +000023 U_ANNOTATE_HAPPENS_AFTER(&s_i);
bartb43825e2010-03-07 19:59:35 +000024 *(int*)arg = i;
25 return NULL;
26}
27
28int main(int argc, char** argv)
29{
bartc2f5a532011-07-30 16:50:33 +000030 const struct timespec delay = { 0, 100 * 1000 * 1000 };
bartb43825e2010-03-07 19:59:35 +000031 pthread_t tid[2];
32 int result[2];
33
barta7103ba2010-09-02 09:38:55 +000034 U_ANNOTATE_HAPPENS_BEFORE(&s_i);
bartb43825e2010-03-07 19:59:35 +000035 pthread_create(&tid[0], 0, thread_func, &result[0]);
bartc2f5a532011-07-30 16:50:33 +000036 pthread_create(&tid[1], 0, thread_func, &result[1]);
37
38 nanosleep(&delay, 0);
39
bartb43825e2010-03-07 19:59:35 +000040 s_i = 1;
41
42 pthread_join(tid[0], NULL);
bartc2f5a532011-07-30 16:50:33 +000043 pthread_join(tid[1], NULL);
bartb43825e2010-03-07 19:59:35 +000044
45 fprintf(stderr, "Done.\n");
46
47 return 0;
48}