bart | 6b0e2dd | 2008-06-28 16:06:01 +0000 | [diff] [blame] | 1 | /** Trigger two kinds of errors: once that condition variable s_cond is |
| 2 | * associated with two different mutexes (s_mutex1 and s_mutex2), and two |
| 3 | * times that pthread_cond_signal() is called without that the mutex |
| 4 | * associated with the condition variable is locked. |
| 5 | */ |
| 6 | |
| 7 | |
bart | 76baf21 | 2009-07-21 11:36:46 +0000 | [diff] [blame] | 8 | #include <errno.h> // ETIMEDOUT |
bart | 3bb1cec | 2008-06-28 16:01:43 +0000 | [diff] [blame] | 9 | #include <pthread.h> |
| 10 | #include <semaphore.h> |
bart | 76baf21 | 2009-07-21 11:36:46 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
bart | d658a04 | 2009-07-23 17:50:02 +0000 | [diff] [blame] | 12 | #include <stdlib.h> // malloc() |
bart | 76baf21 | 2009-07-21 11:36:46 +0000 | [diff] [blame] | 13 | #include <string.h> // memset() |
| 14 | #include <sys/time.h> // gettimeofday() |
| 15 | #include <time.h> // struct timespec |
tom | d759a83 | 2010-04-29 09:36:35 +0000 | [diff] [blame] | 16 | #include <fcntl.h> // O_CREAT |
bart | 3bb1cec | 2008-06-28 16:01:43 +0000 | [diff] [blame] | 17 | #include <unistd.h> |
bart | 0ccdc4a | 2010-04-29 06:28:43 +0000 | [diff] [blame] | 18 | #include "../../config.h" |
bart | 3bb1cec | 2008-06-28 16:01:43 +0000 | [diff] [blame] | 19 | |
bart | 6970e6a | 2009-07-21 16:19:34 +0000 | [diff] [blame] | 20 | |
| 21 | #define PTH_CALL(expr) \ |
| 22 | do \ |
| 23 | { \ |
| 24 | int err = (expr); \ |
bart | 9ccf35f | 2009-07-24 06:29:57 +0000 | [diff] [blame] | 25 | if (! s_quiet && err) \ |
bart | 6970e6a | 2009-07-21 16:19:34 +0000 | [diff] [blame] | 26 | { \ |
| 27 | fprintf(stderr, \ |
| 28 | "%s:%d %s returned error code %d (%s)\n", \ |
| 29 | __FILE__, \ |
| 30 | __LINE__, \ |
| 31 | #expr, \ |
| 32 | err, \ |
| 33 | strerror(err)); \ |
| 34 | } \ |
| 35 | } while (0) |
| 36 | |
| 37 | |
bart | 9ccf35f | 2009-07-24 06:29:57 +0000 | [diff] [blame] | 38 | static pthread_cond_t s_cond; |
| 39 | static pthread_mutex_t s_mutex1; |
| 40 | static pthread_mutex_t s_mutex2; |
| 41 | static sem_t* s_sem; |
| 42 | static int s_quiet; |
bart | 3bb1cec | 2008-06-28 16:01:43 +0000 | [diff] [blame] | 43 | |
bart | 6970e6a | 2009-07-21 16:19:34 +0000 | [diff] [blame] | 44 | |
bart | 23d23a5 | 2009-07-23 18:02:52 +0000 | [diff] [blame] | 45 | static sem_t* create_semaphore(const char* const name) |
bart | d658a04 | 2009-07-23 17:50:02 +0000 | [diff] [blame] | 46 | { |
bart | 3e7c402 | 2011-03-05 14:11:40 +0000 | [diff] [blame] | 47 | #ifdef VGO_darwin |
bart | 8c7213f | 2011-03-05 14:49:12 +0000 | [diff] [blame] | 48 | char name_and_pid[32]; |
| 49 | snprintf(name_and_pid, sizeof(name_and_pid), "%s-%d", name, getpid()); |
| 50 | sem_t* p = sem_open(name_and_pid, O_CREAT | O_EXCL, 0600, 0); |
| 51 | if (p == SEM_FAILED) { |
| 52 | perror("sem_open"); |
| 53 | return NULL; |
| 54 | } |
bart | 23d23a5 | 2009-07-23 18:02:52 +0000 | [diff] [blame] | 55 | return p; |
| 56 | #else |
bart | d658a04 | 2009-07-23 17:50:02 +0000 | [diff] [blame] | 57 | sem_t* p = malloc(sizeof(*p)); |
| 58 | if (p) |
| 59 | sem_init(p, 0, 0); |
| 60 | return p; |
bart | 23d23a5 | 2009-07-23 18:02:52 +0000 | [diff] [blame] | 61 | #endif |
bart | d658a04 | 2009-07-23 17:50:02 +0000 | [diff] [blame] | 62 | } |
| 63 | |
bart | 23d23a5 | 2009-07-23 18:02:52 +0000 | [diff] [blame] | 64 | static void destroy_semaphore(const char* const name, sem_t* p) |
bart | d658a04 | 2009-07-23 17:50:02 +0000 | [diff] [blame] | 65 | { |
bart | 3e7c402 | 2011-03-05 14:11:40 +0000 | [diff] [blame] | 66 | #ifdef VGO_darwin |
bart | 23d23a5 | 2009-07-23 18:02:52 +0000 | [diff] [blame] | 67 | sem_close(p); |
| 68 | sem_unlink(name); |
| 69 | #else |
bart | d658a04 | 2009-07-23 17:50:02 +0000 | [diff] [blame] | 70 | sem_destroy(p); |
| 71 | free(p); |
bart | 23d23a5 | 2009-07-23 18:02:52 +0000 | [diff] [blame] | 72 | #endif |
bart | d658a04 | 2009-07-23 17:50:02 +0000 | [diff] [blame] | 73 | } |
| 74 | |
bart | 76baf21 | 2009-07-21 11:36:46 +0000 | [diff] [blame] | 75 | static void* thread_func(void* mutex) |
bart | 3bb1cec | 2008-06-28 16:01:43 +0000 | [diff] [blame] | 76 | { |
bart | 76baf21 | 2009-07-21 11:36:46 +0000 | [diff] [blame] | 77 | struct timeval now; |
| 78 | struct timespec deadline; |
bart | 3bb1cec | 2008-06-28 16:01:43 +0000 | [diff] [blame] | 79 | |
bart | 6970e6a | 2009-07-21 16:19:34 +0000 | [diff] [blame] | 80 | PTH_CALL(pthread_mutex_lock(mutex)); |
bart | d658a04 | 2009-07-23 17:50:02 +0000 | [diff] [blame] | 81 | sem_post(s_sem); |
bart | 76baf21 | 2009-07-21 11:36:46 +0000 | [diff] [blame] | 82 | gettimeofday(&now, 0); |
| 83 | memset(&deadline, 0, sizeof(deadline)); |
| 84 | deadline.tv_sec = now.tv_sec + 2; |
| 85 | deadline.tv_nsec = now.tv_usec * 1000; |
bart | 6970e6a | 2009-07-21 16:19:34 +0000 | [diff] [blame] | 86 | PTH_CALL(pthread_cond_timedwait(&s_cond, mutex, &deadline)); |
| 87 | PTH_CALL(pthread_mutex_unlock(mutex)); |
bart | 3bb1cec | 2008-06-28 16:01:43 +0000 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int main(int argc, char** argv) |
| 92 | { |
bart | 9ccf35f | 2009-07-24 06:29:57 +0000 | [diff] [blame] | 93 | char semaphore_name[32]; |
| 94 | int optchar; |
bart | 3bb1cec | 2008-06-28 16:01:43 +0000 | [diff] [blame] | 95 | pthread_t tid1; |
| 96 | pthread_t tid2; |
| 97 | |
bart | 9ccf35f | 2009-07-24 06:29:57 +0000 | [diff] [blame] | 98 | while ((optchar = getopt(argc, argv, "q")) != EOF) |
| 99 | { |
| 100 | switch (optchar) |
| 101 | { |
| 102 | case 'q': s_quiet = 1; break; |
| 103 | default: |
| 104 | fprintf(stderr, "Error: unknown option '%c'.\n", optchar); |
| 105 | return 1; |
| 106 | } |
| 107 | } |
| 108 | |
bart | 83d509a | 2008-09-09 18:18:26 +0000 | [diff] [blame] | 109 | /* Initialize synchronization objects. */ |
bart | 23d23a5 | 2009-07-23 18:02:52 +0000 | [diff] [blame] | 110 | snprintf(semaphore_name, sizeof(semaphore_name), "semaphore-%d", getpid()); |
| 111 | s_sem = create_semaphore(semaphore_name); |
bart | 6970e6a | 2009-07-21 16:19:34 +0000 | [diff] [blame] | 112 | PTH_CALL(pthread_cond_init(&s_cond, 0)); |
| 113 | PTH_CALL(pthread_mutex_init(&s_mutex1, 0)); |
| 114 | PTH_CALL(pthread_mutex_init(&s_mutex2, 0)); |
bart | 83d509a | 2008-09-09 18:18:26 +0000 | [diff] [blame] | 115 | |
| 116 | /* Create two threads. */ |
bart | 6970e6a | 2009-07-21 16:19:34 +0000 | [diff] [blame] | 117 | PTH_CALL(pthread_create(&tid1, 0, &thread_func, &s_mutex1)); |
| 118 | PTH_CALL(pthread_create(&tid2, 0, &thread_func, &s_mutex2)); |
bart | 83d509a | 2008-09-09 18:18:26 +0000 | [diff] [blame] | 119 | |
| 120 | /* Wait until both threads have called sem_post(). */ |
bart | d658a04 | 2009-07-23 17:50:02 +0000 | [diff] [blame] | 121 | sem_wait(s_sem); |
| 122 | sem_wait(s_sem); |
bart | 23d23a5 | 2009-07-23 18:02:52 +0000 | [diff] [blame] | 123 | destroy_semaphore(semaphore_name, s_sem); |
bart | d658a04 | 2009-07-23 17:50:02 +0000 | [diff] [blame] | 124 | s_sem = 0; |
bart | 83d509a | 2008-09-09 18:18:26 +0000 | [diff] [blame] | 125 | |
| 126 | /* Wait until both threads are waiting inside pthread_cond_wait(). */ |
bart | 6970e6a | 2009-07-21 16:19:34 +0000 | [diff] [blame] | 127 | PTH_CALL(pthread_mutex_lock(&s_mutex1)); |
| 128 | PTH_CALL(pthread_mutex_lock(&s_mutex2)); |
| 129 | PTH_CALL(pthread_mutex_unlock(&s_mutex2)); |
| 130 | PTH_CALL(pthread_mutex_unlock(&s_mutex1)); |
bart | 83d509a | 2008-09-09 18:18:26 +0000 | [diff] [blame] | 131 | |
| 132 | /* Signal s_cond twice. */ |
bart | 6970e6a | 2009-07-21 16:19:34 +0000 | [diff] [blame] | 133 | PTH_CALL(pthread_cond_signal(&s_cond)); |
| 134 | PTH_CALL(pthread_cond_signal(&s_cond)); |
bart | 83d509a | 2008-09-09 18:18:26 +0000 | [diff] [blame] | 135 | |
| 136 | /* Join both threads. */ |
bart | 6970e6a | 2009-07-21 16:19:34 +0000 | [diff] [blame] | 137 | PTH_CALL(pthread_join(tid1, 0)); |
| 138 | PTH_CALL(pthread_join(tid2, 0)); |
bart | 83d509a | 2008-09-09 18:18:26 +0000 | [diff] [blame] | 139 | |
bart | 3bb1cec | 2008-06-28 16:01:43 +0000 | [diff] [blame] | 140 | return 0; |
| 141 | } |