bart | e636136 | 2009-08-13 09:30:57 +0000 | [diff] [blame] | 1 | /* Test program for the annotations that suppress write operations. */ |
| 2 | |
| 3 | #include <assert.h> /* assert() */ |
| 4 | #include <pthread.h> |
bart | de60fe5 | 2011-10-05 13:10:30 +0000 | [diff] [blame] | 5 | #include <stdint.h> |
bart | e636136 | 2009-08-13 09:30:57 +0000 | [diff] [blame] | 6 | #include <stdio.h> /* EOF */ |
| 7 | #include <unistd.h> /* getopt() */ |
| 8 | #include "../../drd/drd.h" |
| 9 | |
bart | de60fe5 | 2011-10-05 13:10:30 +0000 | [diff] [blame] | 10 | static int8_t s_a; |
| 11 | static int8_t s_b; |
| 12 | static int8_t s_c; |
bart | e636136 | 2009-08-13 09:30:57 +0000 | [diff] [blame] | 13 | |
| 14 | static void* thread_func(void* arg) |
| 15 | { |
| 16 | /* Read s_a and modify s_b. */ |
| 17 | s_b = s_a; |
| 18 | /* Modify s_c. */ |
| 19 | s_c = 1; |
| 20 | |
| 21 | return NULL; |
| 22 | } |
| 23 | |
| 24 | int main(int argc, char** argv) |
| 25 | { |
bart | cfca2d4 | 2011-07-28 09:33:55 +0000 | [diff] [blame] | 26 | const struct timespec delay = { 0, 100 * 1000 * 1000 }; |
bart | e636136 | 2009-08-13 09:30:57 +0000 | [diff] [blame] | 27 | int optchar; |
| 28 | int ign_rw = 1; |
| 29 | pthread_t tid; |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 30 | |
bart | e636136 | 2009-08-13 09:30:57 +0000 | [diff] [blame] | 31 | while ((optchar = getopt(argc, argv, "r")) != EOF) |
| 32 | { |
| 33 | switch (optchar) |
| 34 | { |
| 35 | case 'r': |
| 36 | ign_rw = 0; |
| 37 | break; |
| 38 | default: |
| 39 | assert(0); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pthread_create(&tid, 0, thread_func, 0); |
bart | cfca2d4 | 2011-07-28 09:33:55 +0000 | [diff] [blame] | 44 | |
| 45 | /* Let the code in the created thread run first. */ |
| 46 | nanosleep(&delay, 0); |
| 47 | |
bart | e636136 | 2009-08-13 09:30:57 +0000 | [diff] [blame] | 48 | if (ign_rw) |
| 49 | ANNOTATE_IGNORE_WRITES_BEGIN(); |
| 50 | /* Read s_b and modify s_a. */ |
| 51 | s_a = s_b; |
| 52 | if (ign_rw) |
| 53 | ANNOTATE_IGNORE_WRITES_END(); |
| 54 | |
| 55 | /* |
| 56 | * Insert a delay here in order to make sure the load of s_c happens |
| 57 | * after s_c has been modified. |
| 58 | */ |
bart | cfca2d4 | 2011-07-28 09:33:55 +0000 | [diff] [blame] | 59 | nanosleep(&delay, 0); |
bart | e636136 | 2009-08-13 09:30:57 +0000 | [diff] [blame] | 60 | |
| 61 | /* Read s_c and modify s_a. */ |
| 62 | s_a = s_c; |
| 63 | |
| 64 | pthread_join(tid, 0); |
| 65 | |
| 66 | fprintf(stderr, "Finished.\n"); |
| 67 | |
| 68 | return 0; |
| 69 | } |