Fix parameter name.
[glibc.git] / nptl / cond-perf.c
blobc3305b391d8f8a26a406eaa12d32cd900873a208
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 alldone = true;
29 pthread_cond_signal (&cond2);
32 pthread_cond_wait (&cond1, &mut1);
34 while (! last_round);
36 pthread_mutex_unlock (&mut1);
38 return NULL;
42 int
43 main (int argc, char *argv[])
45 int opt;
46 int err;
47 int nthreads = 10;
48 int nrounds = 100;
49 bool keeplock = false;
51 while ((opt = getopt (argc, argv, "n:r:k")) != -1)
52 switch (opt)
54 case 'n':
55 nthreads = atol (optarg);
56 break;
57 case 'r':
58 nrounds = atol (optarg);
59 break;
60 case 'k':
61 keeplock = true;
62 break;
65 ntogo = nthreads;
67 pthread_t th[nthreads];
68 int i;
69 for (i = 0; i < nthreads; ++i)
70 if ((err = pthread_create (&th[i], NULL, cons, (void *) (long) i)) != 0)
71 printf ("pthread_create: %s\n", strerror (err));
73 for (i = 0; i < nrounds; ++i)
75 pthread_mutex_lock (&mut2);
76 while (! alldone)
77 pthread_cond_wait (&cond2, &mut2);
78 pthread_mutex_unlock (&mut2);
80 pthread_mutex_lock (&mut1);
81 if (! keeplock)
82 pthread_mutex_unlock (&mut1);
84 ntogo = nthreads;
85 alldone = false;
86 if (i + 1 >= nrounds)
87 last_round = true;
89 pthread_cond_broadcast (&cond1);
91 if (keeplock)
92 pthread_mutex_unlock (&mut1);
95 for (i = 0; i < nthreads; ++i)
96 if ((err = pthread_join (th[i], NULL)) != 0)
97 printf ("pthread_create: %s\n", strerror (err));
99 return 0;