| 1 |
/* { dg-do link { target c++11 } } */ |
| 2 |
/* { dg-final { simulate-thread } } */ |
| 3 |
/* { dg-require-effective-target sync_char_short } */ |
| 4 |
/* { dg-require-effective-target sync_int_long } */ |
| 5 |
|
| 6 |
/* Test that atomic int and atomic char work properly. */ |
| 7 |
|
| 8 |
using namespace std; |
| 9 |
|
| 10 |
#include <atomic> |
| 11 |
#include <limits.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include "simulate-thread.h" |
| 14 |
|
| 15 |
atomic<int> atomi; |
| 16 |
atomic<char> atomc; |
| 17 |
|
| 18 |
/* No need for parallel threads to do anything */ |
| 19 |
void simulate_thread_other_threads() |
| 20 |
{ |
| 21 |
} |
| 22 |
|
| 23 |
/* Verify after every instruction is executed, that the atmoic int and |
| 24 |
char have one of the 2 legitimate values. */ |
| 25 |
int simulate_thread_step_verify() |
| 26 |
{ |
| 27 |
if (atomi != 0 && atomi != INT_MAX) |
| 28 |
{ |
| 29 |
printf ("FAIL: invalid intermediate result for atomi (%d).\n", |
| 30 |
(int)atomi); |
| 31 |
return 1; |
| 32 |
} |
| 33 |
if (atomc != 0 && atomc != CHAR_MAX) |
| 34 |
{ |
| 35 |
printf ("FAIL: invalid intermediate result for atomc (%d).\n", |
| 36 |
(int)atomc); |
| 37 |
return 1; |
| 38 |
} |
| 39 |
return 0; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
/* Verify that both atmoics have the corerct value. */ |
| 44 |
int simulate_thread_final_verify() |
| 45 |
{ |
| 46 |
if (atomi != INT_MAX) |
| 47 |
{ |
| 48 |
printf ("FAIL: invalid final result for atomi (%d).\n", |
| 49 |
(int)atomi); |
| 50 |
return 1; |
| 51 |
} |
| 52 |
if (atomc != CHAR_MAX) |
| 53 |
{ |
| 54 |
printf ("FAIL: invalid final result for atomc (%d).\n", |
| 55 |
(int)atomc); |
| 56 |
return 1; |
| 57 |
} |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
/* Test a store to an atomic int and an atomic char. */ |
| 62 |
__attribute__((noinline)) |
| 63 |
void simulate_thread_main() |
| 64 |
{ |
| 65 |
atomi = INT_MAX; |
| 66 |
atomc = CHAR_MAX; |
| 67 |
} |
| 68 |
|
| 69 |
int main () |
| 70 |
{ |
| 71 |
simulate_thread_main(); |
| 72 |
simulate_thread_done(); |
| 73 |
return 0; |
| 74 |
} |