bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 1 | /** Initialize several kinds of mutexes and lock each mutex twice. |
| 2 | * Note: locking a regular mutex twice causes a deadlock. |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | #define _GNU_SOURCE |
| 6 | |
| 7 | #include <stdio.h> |
bart | 4240ff3 | 2008-02-27 17:41:22 +0000 | [diff] [blame] | 8 | #include <unistd.h> |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 9 | #include <pthread.h> |
bart | 8556957 | 2008-05-03 09:15:25 +0000 | [diff] [blame] | 10 | #include "../../config.h" |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 11 | |
bart | 4240ff3 | 2008-02-27 17:41:22 +0000 | [diff] [blame] | 12 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame^] | 13 | #if !defined(VGO_darwin) |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 14 | static void lock_twice(pthread_mutex_t* const p) |
| 15 | { |
| 16 | pthread_mutex_lock(p); |
| 17 | pthread_mutex_lock(p); |
| 18 | pthread_mutex_unlock(p); |
| 19 | pthread_mutex_unlock(p); |
| 20 | } |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame^] | 21 | #endif |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 22 | |
| 23 | int main(int argc, char** argv) |
| 24 | { |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 25 | /* Let the program abort after 3 seconds instead of leaving it deadlocked. */ |
| 26 | alarm(3); |
bart | 8556957 | 2008-05-03 09:15:25 +0000 | [diff] [blame] | 27 | |
| 28 | #if defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 29 | { |
| 30 | pthread_mutex_t m = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
| 31 | |
| 32 | printf("Recursive mutex (statically initialized).\n"); |
| 33 | lock_twice(&m); |
bart | 68d80f4 | 2008-03-08 15:03:30 +0000 | [diff] [blame] | 34 | pthread_mutex_destroy(&m); |
bart | 8556957 | 2008-05-03 09:15:25 +0000 | [diff] [blame] | 35 | } |
| 36 | #endif |
| 37 | #if defined(HAVE_PTHREAD_MUTEX_RECURSIVE_NP) |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 38 | { |
| 39 | pthread_mutex_t m; |
| 40 | pthread_mutexattr_t attr; |
| 41 | |
| 42 | printf("Recursive mutex (initialized via mutex attributes).\n"); |
| 43 | pthread_mutexattr_init(&attr); |
| 44 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); |
| 45 | pthread_mutex_init(&m, &attr); |
| 46 | pthread_mutexattr_destroy(&attr); |
| 47 | lock_twice(&m); |
| 48 | pthread_mutex_destroy(&m); |
bart | 8556957 | 2008-05-03 09:15:25 +0000 | [diff] [blame] | 49 | } |
| 50 | #endif |
| 51 | #if defined(HAVE_PTHREAD_MUTEX_ERRORCHECK_NP) |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 52 | { |
| 53 | pthread_mutex_t m; |
| 54 | pthread_mutexattr_t attr; |
| 55 | |
| 56 | printf("Error checking mutex.\n"); |
| 57 | pthread_mutexattr_init(&attr); |
| 58 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP); |
| 59 | pthread_mutex_init(&m, &attr); |
| 60 | pthread_mutexattr_destroy(&attr); |
| 61 | lock_twice(&m); |
bart | 8556957 | 2008-05-03 09:15:25 +0000 | [diff] [blame] | 62 | pthread_mutex_destroy(&m); |
| 63 | } |
| 64 | #endif |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame^] | 65 | |
| 66 | // DDD: Darwin doesn't support signals yet, so the alarm() call doesn't kick |
| 67 | // in, which causes it to hang. |
| 68 | #if !defined(VGO_darwin) |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 69 | { |
| 70 | pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; |
| 71 | |
| 72 | printf("Non-recursive mutex.\n"); |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 73 | fflush(stdout); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 74 | lock_twice(&m); |
bart | 8556957 | 2008-05-03 09:15:25 +0000 | [diff] [blame] | 75 | } |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 76 | printf("Done.\n"); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame^] | 77 | #endif |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 78 | return 0; |
| 79 | } |