blob: f71f19fd034f7f2dacb9b2f76fbf8d6ca0d672dc [file] [log] [blame]
bartd45d9952009-05-31 18:53:54 +00001/* 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
bart06524322014-03-07 14:38:14 +000014static pthread_barrier_t s_barrier;
bartd45d9952009-05-31 18:53:54 +000015static pthread_mutex_t s_mutex;
16static pthread_cond_t s_cond;
17static int s_counter;
18
19static 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
bart06524322014-03-07 14:38:14 +000031 pthread_barrier_wait(&s_barrier);
32
bartd45d9952009-05-31 18:53:54 +000033 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
46int main(int arg, char** argv)
47{
48 int i;
49 pthread_t tid[NUM_THREADS];
50
bart06524322014-03-07 14:38:14 +000051 pthread_barrier_init(&s_barrier, NULL, NUM_THREADS);
bartd45d9952009-05-31 18:53:54 +000052 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}