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