bart | b56542a | 2011-07-29 12:32:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Test program that triggers pthread_barrier_wait() where each |
| 3 | * pthread_barrier_wait() call is invoked by another thread. This is the only |
| 4 | * test program that triggers the code guarded by if (q->thread_finished) in |
| 5 | * DRD_(barrier_pre_wait)(). |
| 6 | */ |
| 7 | |
| 8 | #define _GNU_SOURCE |
| 9 | |
| 10 | #include <assert.h> |
| 11 | #include <pthread.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <unistd.h> |
| 15 | |
bart | 6c73cf6 | 2011-07-30 09:28:13 +0000 | [diff] [blame] | 16 | static pthread_barrier_t* s_barrier; |
bart | b56542a | 2011-07-29 12:32:53 +0000 | [diff] [blame] | 17 | |
| 18 | static void* thread(void* arg) |
| 19 | { |
| 20 | write(STDOUT_FILENO, ".", 1); |
bart | 6c73cf6 | 2011-07-30 09:28:13 +0000 | [diff] [blame] | 21 | pthread_barrier_wait(s_barrier); |
bart | b56542a | 2011-07-29 12:32:53 +0000 | [diff] [blame] | 22 | return NULL; |
| 23 | } |
| 24 | |
| 25 | int main(int argc, char** argv) |
| 26 | { |
| 27 | pthread_t *tid; |
bart | 6c73cf6 | 2011-07-30 09:28:13 +0000 | [diff] [blame] | 28 | int barriers = argc > 1 ? atoi(argv[1]) : 20; |
bart | b56542a | 2011-07-29 12:32:53 +0000 | [diff] [blame] | 29 | int barrier_participants = 2; |
| 30 | int thread_count = barriers * barrier_participants; |
bart | 6c73cf6 | 2011-07-30 09:28:13 +0000 | [diff] [blame] | 31 | int res, i; |
bart | b56542a | 2011-07-29 12:32:53 +0000 | [diff] [blame] | 32 | |
bart | 6c73cf6 | 2011-07-30 09:28:13 +0000 | [diff] [blame] | 33 | s_barrier = malloc(sizeof(*s_barrier)); |
| 34 | res = pthread_barrier_init(s_barrier, NULL, barrier_participants); |
| 35 | assert(res == 0); |
bart | b56542a | 2011-07-29 12:32:53 +0000 | [diff] [blame] | 36 | |
| 37 | tid = malloc(thread_count * sizeof(*tid)); |
| 38 | assert(tid); |
bart | 6c73cf6 | 2011-07-30 09:28:13 +0000 | [diff] [blame] | 39 | for (i = 0; i < thread_count; i++) { |
| 40 | res = pthread_create(&tid[i], NULL, thread, NULL); |
| 41 | assert(res == 0); |
| 42 | } |
| 43 | for (i = 0; i < thread_count; i++) { |
| 44 | res = pthread_join(tid[i], NULL); |
| 45 | assert(res == 0); |
| 46 | } |
bart | b56542a | 2011-07-29 12:32:53 +0000 | [diff] [blame] | 47 | free(tid); |
| 48 | |
bart | 6c73cf6 | 2011-07-30 09:28:13 +0000 | [diff] [blame] | 49 | res = pthread_barrier_destroy(s_barrier); |
| 50 | assert(res == 0); |
| 51 | free(s_barrier); |
| 52 | s_barrier = NULL; |
bart | b56542a | 2011-07-29 12:32:53 +0000 | [diff] [blame] | 53 | |
| 54 | write(STDOUT_FILENO, "\n", 1); |
| 55 | fprintf(stderr, "Done.\n"); |
| 56 | |
| 57 | return 0; |
| 58 | } |