2018-11-07 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / g++.dg / tsan / aligned_vs_unaligned_race.C
blobfe64007e4e5c0eafc93287b77738ae31116383f8
1 /* { dg-shouldfail "tsan" } */
2 /* { dg-additional-options "-ldl" } */
4 #include <pthread.h>
5 #include <stdio.h>
6 #include <stdint.h>
7 #include "tsan_barrier.h"
9 static pthread_barrier_t barrier;
10 uint64_t Global[2];
12 void *Thread1(void *x) {
13   barrier_wait(&barrier);
14   Global[1]++;
15   return NULL;
18 void *Thread2(void *x) {
19   char *p1 = reinterpret_cast<char *>(&Global[0]);
20   struct __attribute__((packed, aligned(1))) u_uint64_t { uint64_t val; };
21   u_uint64_t *p4 = reinterpret_cast<u_uint64_t *>(p1 + 1);
22   (*p4).val++;
23   barrier_wait(&barrier);
24   return NULL;
27 int main() {
28   barrier_init(&barrier, 2);
29   pthread_t t[2];
30   pthread_create(&t[0], NULL, Thread1, NULL);
31   pthread_create(&t[1], NULL, Thread2, NULL);
32   pthread_join(t[0], NULL);
33   pthread_join(t[1], NULL);
34   fprintf(stderr, "Pass\n");
35   /* { dg-output "WARNING: ThreadSanitizer: data race.*(\n|\r\n|\r)" } */
36   /* { dg-output "Pass.*" } */
37   return 0;