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 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 13 | static void lock_twice(pthread_mutex_t* const p) |
| 14 | { |
| 15 | pthread_mutex_lock(p); |
| 16 | pthread_mutex_lock(p); |
| 17 | pthread_mutex_unlock(p); |
| 18 | pthread_mutex_unlock(p); |
| 19 | } |
| 20 | |
| 21 | int main(int argc, char** argv) |
| 22 | { |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 23 | /* Let the program abort after 3 seconds instead of leaving it deadlocked. */ |
| 24 | alarm(3); |
bart | 8556957 | 2008-05-03 09:15:25 +0000 | [diff] [blame] | 25 | |
| 26 | #if defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 27 | { |
| 28 | pthread_mutex_t m = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
| 29 | |
| 30 | printf("Recursive mutex (statically initialized).\n"); |
| 31 | lock_twice(&m); |
bart | 68d80f4 | 2008-03-08 15:03:30 +0000 | [diff] [blame] | 32 | pthread_mutex_destroy(&m); |
bart | 8556957 | 2008-05-03 09:15:25 +0000 | [diff] [blame] | 33 | } |
| 34 | #endif |
| 35 | #if defined(HAVE_PTHREAD_MUTEX_RECURSIVE_NP) |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 36 | { |
| 37 | pthread_mutex_t m; |
| 38 | pthread_mutexattr_t attr; |
| 39 | |
| 40 | printf("Recursive mutex (initialized via mutex attributes).\n"); |
| 41 | pthread_mutexattr_init(&attr); |
| 42 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); |
| 43 | pthread_mutex_init(&m, &attr); |
| 44 | pthread_mutexattr_destroy(&attr); |
| 45 | lock_twice(&m); |
| 46 | pthread_mutex_destroy(&m); |
bart | 8556957 | 2008-05-03 09:15:25 +0000 | [diff] [blame] | 47 | } |
| 48 | #endif |
| 49 | #if defined(HAVE_PTHREAD_MUTEX_ERRORCHECK_NP) |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 50 | { |
| 51 | pthread_mutex_t m; |
| 52 | pthread_mutexattr_t attr; |
| 53 | |
| 54 | printf("Error checking mutex.\n"); |
| 55 | pthread_mutexattr_init(&attr); |
| 56 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP); |
| 57 | pthread_mutex_init(&m, &attr); |
| 58 | pthread_mutexattr_destroy(&attr); |
| 59 | lock_twice(&m); |
bart | 8556957 | 2008-05-03 09:15:25 +0000 | [diff] [blame] | 60 | pthread_mutex_destroy(&m); |
| 61 | } |
| 62 | #endif |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 63 | { |
| 64 | pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; |
| 65 | |
| 66 | printf("Non-recursive mutex.\n"); |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 67 | fflush(stdout); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 68 | lock_twice(&m); |
bart | 8556957 | 2008-05-03 09:15:25 +0000 | [diff] [blame] | 69 | } |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 70 | printf("Done.\n"); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 71 | return 0; |
| 72 | } |