drd: Add a consistency check
[valgrind.git] / drd / tests / thread_name.c
blob98c7bb8a3feafd3b19f576d9eae8bde8a48138f7
1 /* Test whether assigning names to threads works properly. */
3 #define _GNU_SOURCE
4 #include <pthread.h>
5 #include <stddef.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include "../../drd/drd.h"
11 #define NUM_THREADS 10
14 static pthread_barrier_t s_barrier;
15 static pthread_mutex_t s_mutex;
16 static pthread_cond_t s_cond;
17 static int s_counter;
19 static void* thread_func(void* argp)
21 const int thread_num = (ptrdiff_t)(argp);
22 pthread_mutex_t invalid_mutex;
23 char thread_name[32];
25 snprintf(thread_name, sizeof(thread_name),
26 "thread_func instance %d", thread_num + 1);
27 ANNOTATE_THREAD_NAME(thread_name);
29 memset(&invalid_mutex, 0xff, sizeof(invalid_mutex));
31 pthread_barrier_wait(&s_barrier);
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);
42 return 0;
46 int main(int arg, char** argv)
48 int i;
49 pthread_t tid[NUM_THREADS];
51 pthread_barrier_init(&s_barrier, NULL, NUM_THREADS);
52 pthread_mutex_init(&s_mutex, 0);
53 pthread_cond_init(&s_cond, 0);
55 for (i = 0; i < NUM_THREADS; i++)
56 pthread_create(&tid[i], 0, thread_func, (void*)(ptrdiff_t)i);
58 for (i = 0; i < NUM_THREADS; i++)
59 pthread_join(tid[i], 0);
61 pthread_cond_destroy(&s_cond);
62 pthread_mutex_destroy(&s_mutex);
64 return 0;