bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 1 | /* Test whether assigning names to threads works properly. */ |
| 2 | |
| 3 | |
| 4 | #include <pthread.h> |
| 5 | #include <stddef.h> |
| 6 | #include <stdio.h> |
| 7 | #include <string.h> |
| 8 | #include "../../drd/drd.h" |
| 9 | |
| 10 | |
| 11 | #define NUM_THREADS 10 |
| 12 | |
| 13 | |
bart | 0652432 | 2014-03-07 14:38:14 +0000 | [diff] [blame] | 14 | static pthread_barrier_t s_barrier; |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 15 | static pthread_mutex_t s_mutex; |
| 16 | static pthread_cond_t s_cond; |
| 17 | static int s_counter; |
| 18 | |
| 19 | static void* thread_func(void* argp) |
| 20 | { |
| 21 | const int thread_num = (ptrdiff_t)(argp); |
| 22 | pthread_mutex_t invalid_mutex; |
| 23 | char thread_name[32]; |
| 24 | |
| 25 | snprintf(thread_name, sizeof(thread_name), |
| 26 | "thread_func instance %d", thread_num + 1); |
| 27 | ANNOTATE_THREAD_NAME(thread_name); |
| 28 | |
| 29 | memset(&invalid_mutex, 0xff, sizeof(invalid_mutex)); |
| 30 | |
bart | 0652432 | 2014-03-07 14:38:14 +0000 | [diff] [blame] | 31 | pthread_barrier_wait(&s_barrier); |
| 32 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 33 | pthread_mutex_lock(&s_mutex); |
| 34 | while (s_counter != thread_num) |
| 35 | pthread_cond_wait(&s_cond, &s_mutex); |
| 36 | fprintf(stderr, "\n%s\n\n", thread_name); |
| 37 | pthread_mutex_unlock(&invalid_mutex); |
| 38 | s_counter++; |
| 39 | pthread_cond_broadcast(&s_cond); |
| 40 | pthread_mutex_unlock(&s_mutex); |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | int main(int arg, char** argv) |
| 47 | { |
| 48 | int i; |
| 49 | pthread_t tid[NUM_THREADS]; |
| 50 | |
bart | 0652432 | 2014-03-07 14:38:14 +0000 | [diff] [blame] | 51 | pthread_barrier_init(&s_barrier, NULL, NUM_THREADS); |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 52 | pthread_mutex_init(&s_mutex, 0); |
| 53 | pthread_cond_init(&s_cond, 0); |
| 54 | |
| 55 | for (i = 0; i < NUM_THREADS; i++) |
| 56 | pthread_create(&tid[i], 0, thread_func, (void*)(ptrdiff_t)i); |
| 57 | |
| 58 | for (i = 0; i < NUM_THREADS; i++) |
| 59 | pthread_join(tid[i], 0); |
| 60 | |
| 61 | pthread_cond_destroy(&s_cond); |
| 62 | pthread_mutex_destroy(&s_mutex); |
| 63 | |
| 64 | return 0; |
| 65 | } |