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
14static pthread_mutex_t s_mutex;
15static pthread_cond_t  s_cond;
16static int s_counter;
17
18static void* thread_func(void* argp)
19{
20  const int thread_num = (ptrdiff_t)(argp);
21  pthread_mutex_t invalid_mutex;
22  char thread_name[32];
23
24  snprintf(thread_name, sizeof(thread_name),
25           "thread_func instance %d", thread_num + 1);
26  ANNOTATE_THREAD_NAME(thread_name);
27
28  memset(&invalid_mutex, 0xff, sizeof(invalid_mutex));
29
30  pthread_mutex_lock(&s_mutex);
31  while (s_counter != thread_num)
32    pthread_cond_wait(&s_cond, &s_mutex);
33  fprintf(stderr, "\n%s\n\n", thread_name);
34  pthread_mutex_unlock(&invalid_mutex);
35  s_counter++;
36  pthread_cond_broadcast(&s_cond);
37  pthread_mutex_unlock(&s_mutex);
38
39  return 0;
40}
41
42
43int main(int arg, char** argv)
44{
45  int i;
46  pthread_t tid[NUM_THREADS];
47
48  pthread_mutex_init(&s_mutex, 0);
49  pthread_cond_init(&s_cond, 0);
50
51  for (i = 0; i < NUM_THREADS; i++)
52    pthread_create(&tid[i], 0, thread_func, (void*)(ptrdiff_t)i);
53
54  for (i = 0; i < NUM_THREADS; i++)
55    pthread_join(tid[i], 0);
56
57  pthread_cond_destroy(&s_cond);
58  pthread_mutex_destroy(&s_mutex);
59
60  return 0;
61}
62