| 1 |
/* { dg-do run } */ |
| 2 |
/* { dg-require-effective-target c99_runtime } */ |
| 3 |
/* { dg-require-effective-target pthread } */ |
| 4 |
/* { dg-options "-std=c99 -pthread" } */ |
| 5 |
|
| 6 |
/* https://github.com/llvm/llvm-project/pull/67391#issuecomment-1752403934 |
| 7 |
reported that this had failed with GCC and 3A6000. */ |
| 8 |
|
| 9 |
#include <pthread.h> |
| 10 |
#include <stdatomic.h> |
| 11 |
#include <stdbool.h> |
| 12 |
#include <stdio.h> |
| 13 |
|
| 14 |
static unsigned int tags[32]; |
| 15 |
static unsigned int vals[32]; |
| 16 |
|
| 17 |
static void * |
| 18 |
writer_entry (void *data) |
| 19 |
{ |
| 20 |
atomic_uint *pt = (atomic_uint *)tags; |
| 21 |
atomic_uint *pv = (atomic_uint *)vals; |
| 22 |
|
| 23 |
for (unsigned int n = 1; n < 10000; n++) |
| 24 |
{ |
| 25 |
atomic_store_explicit (&pv[n & 31], n, memory_order_release); |
| 26 |
atomic_store_explicit (&pt[n & 31], n, memory_order_release); |
| 27 |
} |
| 28 |
|
| 29 |
return NULL; |
| 30 |
} |
| 31 |
|
| 32 |
static void * |
| 33 |
reader_entry (void *data) |
| 34 |
{ |
| 35 |
atomic_uint *pt = (atomic_uint *)tags; |
| 36 |
atomic_uint *pv = (atomic_uint *)vals; |
| 37 |
int i; |
| 38 |
|
| 39 |
for (;;) |
| 40 |
{ |
| 41 |
for (i = 0; i < 32; i++) |
| 42 |
{ |
| 43 |
unsigned int tag = 0; |
| 44 |
bool res; |
| 45 |
|
| 46 |
res = atomic_compare_exchange_weak_explicit ( |
| 47 |
&pt[i], &tag, 0, memory_order_acquire, memory_order_acquire); |
| 48 |
if (!res) |
| 49 |
{ |
| 50 |
unsigned int val; |
| 51 |
|
| 52 |
val = atomic_load_explicit (&pv[i], memory_order_relaxed); |
| 53 |
if (val < tag) |
| 54 |
__builtin_trap (); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return NULL; |
| 60 |
} |
| 61 |
|
| 62 |
int |
| 63 |
main (int argc, char *argv[]) |
| 64 |
{ |
| 65 |
pthread_t writer; |
| 66 |
pthread_t reader; |
| 67 |
int res; |
| 68 |
|
| 69 |
res = pthread_create (&writer, NULL, writer_entry, NULL); |
| 70 |
if (res < 0) |
| 71 |
__builtin_trap (); |
| 72 |
|
| 73 |
res = pthread_create (&reader, NULL, reader_entry, NULL); |
| 74 |
if (res < 0) |
| 75 |
__builtin_trap (); |
| 76 |
|
| 77 |
res = pthread_join (writer, NULL); |
| 78 |
if (res < 0) |
| 79 |
__builtin_trap (); |
| 80 |
|
| 81 |
return 0; |
| 82 |
} |