Add hidden_def.
[glibc.git] / nptl / cond-perf.c
blob53d85637b8743b89575f4bc76fe4d44a69445348
1 #include <pthread.h>
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
8 static pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
9 static pthread_mutex_t mut1 = PTHREAD_MUTEX_INITIALIZER;
11 static pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
12 static pthread_mutex_t mut2 = PTHREAD_MUTEX_INITIALIZER;
14 static bool last_round;
15 static int ntogo;
16 static bool alldone;
19 static void *
20 cons (void *arg)
22 pthread_mutex_lock (&mut1);
26 if (--ntogo == 0)
28 pthread_mutex_lock (&mut2);
29 alldone = true;
30 pthread_cond_signal (&cond2);
31 pthread_mutex_unlock (&mut2);
34 pthread_cond_wait (&cond1, &mut1);
36 while (! last_round);
38 pthread_mutex_unlock (&mut1);
40 return NULL;
44 int
45 main (int argc, char *argv[])
47 int opt;
48 int err;
49 int nthreads = 10;
50 int nrounds = 100;
51 bool keeplock = false;
53 while ((opt = getopt (argc, argv, "n:r:k")) != -1)
54 switch (opt)
56 case 'n':
57 nthreads = atol (optarg);
58 break;
59 case 'r':
60 nrounds = atol (optarg);
61 break;
62 case 'k':
63 keeplock = true;
64 break;
67 ntogo = nthreads;
69 pthread_t th[nthreads];
70 int i;
71 for (i = 0; i < nthreads; ++i)
72 if (__builtin_expect ((err = pthread_create (&th[i], NULL, cons, (void *) (long) i)) != 0, 0))
73 printf ("pthread_create: %s\n", strerror (err));
75 for (i = 0; i < nrounds; ++i)
77 pthread_mutex_lock (&mut2);
78 while (! alldone)
79 pthread_cond_wait (&cond2, &mut2);
80 pthread_mutex_unlock (&mut2);
82 pthread_mutex_lock (&mut1);
83 if (! keeplock)
84 pthread_mutex_unlock (&mut1);
86 ntogo = nthreads;
87 alldone = false;
88 if (i + 1 >= nrounds)
89 last_round = true;
91 pthread_cond_broadcast (&cond1);
93 if (keeplock)
94 pthread_mutex_unlock (&mut1);
97 for (i = 0; i < nthreads; ++i)
98 if ((err = pthread_join (th[i], NULL)) != 0)
99 printf ("pthread_create: %s\n", strerror (err));
101 return 0;