| 1 |
/* { dg-do link } */ |
| 2 |
/* { dg-final { simulate-thread } } */ |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include "simulate-thread.h" |
| 6 |
|
| 7 |
/* This test verifies that data races aren't introduced by structure subfield |
| 8 |
stores. */ |
| 9 |
|
| 10 |
struct test_struct { |
| 11 |
char a; |
| 12 |
char b; |
| 13 |
char c; |
| 14 |
char d; |
| 15 |
} var = {0,0,0,0}; |
| 16 |
|
| 17 |
|
| 18 |
/* This routine sets field a to 'x'. If executed properly, it will |
| 19 |
not affect any of the other fields in the structure. An improper |
| 20 |
implementation may load an entire word, change the 8 bits for field |
| 21 |
'a' and write the entire word back out. */ |
| 22 |
__attribute__((noinline)) |
| 23 |
void set_a(char x) |
| 24 |
{ |
| 25 |
var.a = x; |
| 26 |
} |
| 27 |
|
| 28 |
static int global = 0; |
| 29 |
|
| 30 |
/* The other thread increments the value of each of the other fields |
| 31 |
in the structure every cycle. If the store to the 'a' field does |
| 32 |
an incorrect full or partial word load, mask and store, it will |
| 33 |
write back an incorrect value to one or more of the other |
| 34 |
fields. */ |
| 35 |
void simulate_thread_other_threads() |
| 36 |
{ |
| 37 |
global++; |
| 38 |
var.b = global; |
| 39 |
var.c = global; |
| 40 |
var.d = global; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
/* Make sure that none of the other fields have been changed. */ |
| 45 |
int simulate_thread_step_verify() |
| 46 |
{ |
| 47 |
int ret = 0; |
| 48 |
if (var.b != global) |
| 49 |
{ |
| 50 |
printf("FAIL: Unexpected value. var.b is %d, should be %d\n", |
| 51 |
var.b, global); |
| 52 |
ret = 1; |
| 53 |
} |
| 54 |
if (var.c != global) |
| 55 |
{ |
| 56 |
printf("FAIL: Unexpected value. var.c is %d, should be %d\n", |
| 57 |
var.c, global); |
| 58 |
ret = 1; |
| 59 |
} |
| 60 |
if (var.d != global) |
| 61 |
{ |
| 62 |
printf("FAIL: Unexpected value. var.d is %d, should be %d\n", |
| 63 |
var.d, global); |
| 64 |
ret = 1; |
| 65 |
} |
| 66 |
return ret; |
| 67 |
} |
| 68 |
|
| 69 |
/* Verify that every variable has the correct value. */ |
| 70 |
int simulate_thread_final_verify() |
| 71 |
{ |
| 72 |
int ret = simulate_thread_step_verify(); |
| 73 |
if (var.a != 1) |
| 74 |
{ |
| 75 |
printf("FAIL: Unexpected value. var.a is %d, should be %d\n", var.a, 1); |
| 76 |
ret = 1; |
| 77 |
} |
| 78 |
return ret; |
| 79 |
} |
| 80 |
|
| 81 |
__attribute__((noinline)) |
| 82 |
void simulate_thread_main() |
| 83 |
{ |
| 84 |
set_a(1); |
| 85 |
} |
| 86 |
|
| 87 |
int main () |
| 88 |
{ |
| 89 |
simulate_thread_main(); |
| 90 |
simulate_thread_done(); |
| 91 |
return 0; |
| 92 |
} |