bart | 55df6e7 | 2009-02-16 19:42:17 +0000 | [diff] [blame] | 1 | /* Test data race detection between floating point variables. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 2 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 3 | |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 4 | #include <assert.h> |
bart | 55df6e7 | 2009-02-16 19:42:17 +0000 | [diff] [blame] | 5 | #include <stdio.h> /* printf() */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 6 | #include <pthread.h> |
bart | 3ab0baf | 2009-07-21 11:12:14 +0000 | [diff] [blame] | 7 | #include <unistd.h> /* sleep() */ |
bart | 5f57be9 | 2008-07-01 08:48:56 +0000 | [diff] [blame] | 8 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 9 | |
bart | 55df6e7 | 2009-02-16 19:42:17 +0000 | [diff] [blame] | 10 | /* Local functions declarations. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 11 | |
| 12 | static void* thread_func(void*); |
| 13 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 14 | |
bart | 55df6e7 | 2009-02-16 19:42:17 +0000 | [diff] [blame] | 15 | /* Local variables. */ |
| 16 | |
| 17 | /* s_mutex protects s_d3. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 18 | static pthread_mutex_t s_mutex; |
| 19 | |
bart | 55df6e7 | 2009-02-16 19:42:17 +0000 | [diff] [blame] | 20 | static double s_d1; /* accessed before thread creation and in the created */ |
| 21 | /* thread (not a race). */ |
| 22 | static double s_d2; /* accessed in the created thread and after the join */ |
| 23 | /* (not a race). */ |
| 24 | static double s_d3; /* accessed simultaneously from both threads (race). */ |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 25 | static int s_debug = 0; |
| 26 | static int s_do_printf = 0; |
| 27 | static int s_use_mutex = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 28 | |
| 29 | |
bart | 55df6e7 | 2009-02-16 19:42:17 +0000 | [diff] [blame] | 30 | /* Function definitions. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 31 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 32 | int main(int argc, char** argv) |
| 33 | { |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 34 | int optchar; |
| 35 | pthread_t threadid; |
| 36 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 37 | while ((optchar = getopt(argc, argv, "dmp")) != EOF) |
| 38 | { |
| 39 | switch (optchar) |
| 40 | { |
| 41 | case 'd': |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 42 | s_debug = 1; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 43 | break; |
| 44 | case 'm': |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 45 | s_use_mutex = 1; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 46 | break; |
| 47 | case 'p': |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 48 | s_do_printf = 1; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 49 | break; |
| 50 | default: |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 51 | assert(0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | pthread_mutex_init(&s_mutex, 0); |
| 56 | |
bart | 55df6e7 | 2009-02-16 19:42:17 +0000 | [diff] [blame] | 57 | /* |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame^] | 58 | * Switch to line-buffered mode, such that timing information can be |
bart | 55df6e7 | 2009-02-16 19:42:17 +0000 | [diff] [blame] | 59 | * obtained for each printf() call with strace. |
| 60 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 61 | setlinebuf(stdout); |
| 62 | |
| 63 | if (s_debug) |
| 64 | { |
| 65 | printf("&s_d1 = %p; &s_d2 = %p; &s_d3 = %p\n", &s_d1, &s_d2, &s_d3); |
| 66 | } |
| 67 | |
| 68 | s_d1 = 1; |
| 69 | s_d3 = 3; |
| 70 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 71 | pthread_create(&threadid, 0, thread_func, 0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 72 | |
bart | 1061b67 | 2009-03-11 19:13:34 +0000 | [diff] [blame] | 73 | sleep(1); /* Wait until thread_func() finished. */ |
| 74 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 75 | { |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 76 | if (s_use_mutex) pthread_mutex_lock(&s_mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 77 | s_d3++; |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 78 | if (s_use_mutex) pthread_mutex_unlock(&s_mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 79 | } |
| 80 | |
bart | 55df6e7 | 2009-02-16 19:42:17 +0000 | [diff] [blame] | 81 | /* Wait until the thread finished. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 82 | pthread_join(threadid, 0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 83 | if (s_do_printf) printf("s_d2 = %g (should be 2)\n", s_d2); |
| 84 | if (s_do_printf) printf("s_d3 = %g (should be 5)\n", s_d3); |
| 85 | |
| 86 | pthread_mutex_destroy(&s_mutex); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 91 | static void* thread_func(void* thread_arg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 92 | { |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 93 | if (s_do_printf) |
| 94 | { |
| 95 | printf("s_d1 = %g (should be 1)\n", s_d1); |
| 96 | } |
| 97 | s_d2 = 2; |
| 98 | { |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 99 | if (s_use_mutex) pthread_mutex_lock(&s_mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 100 | s_d3++; |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 101 | if (s_use_mutex) pthread_mutex_unlock(&s_mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 102 | } |
| 103 | return 0; |
| 104 | } |