Updated to fedora-glibc-20090409T1422
[glibc/history.git] / nptl / cond-perf.c
blobe37914e6b7c48d37244371618e7f624f8e2ff2e0
1 #include <pthread.h>
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <atomic.h>
9 static pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
10 static pthread_mutex_t mut1 = PTHREAD_MUTEX_INITIALIZER;
12 static pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
13 static pthread_mutex_t mut2 = PTHREAD_MUTEX_INITIALIZER;
15 static bool last_round;
16 static int ntogo;
17 static bool alldone;
20 static void *
21 cons (void *arg)
23 pthread_mutex_lock (&mut1);
27 if (atomic_decrement_and_test (&ntogo))
29 pthread_mutex_lock (&mut2);
30 alldone = true;
31 pthread_cond_signal (&cond2);
32 pthread_mutex_unlock (&mut2);
35 pthread_cond_wait (&cond1, &mut1);
37 while (! last_round);
39 pthread_mutex_unlock (&mut1);
41 return NULL;
45 int
46 main (int argc, char *argv[])
48 int opt;
49 int err;
50 int nthreads = 10;
51 int nrounds = 100;
52 bool keeplock = false;
54 while ((opt = getopt (argc, argv, "n:r:k")) != -1)
55 switch (opt)
57 case 'n':
58 nthreads = atol (optarg);
59 break;
60 case 'r':
61 nrounds = atol (optarg);
62 break;
63 case 'k':
64 keeplock = true;
65 break;
68 ntogo = nthreads;
70 pthread_t th[nthreads];
71 int i;
72 for (i = 0; __builtin_expect (i < nthreads, 1); ++i)
73 if (__builtin_expect ((err = pthread_create (&th[i], NULL, cons, (void *) (long) i)) != 0, 0))
74 printf ("pthread_create: %s\n", strerror (err));
76 for (i = 0; __builtin_expect (i < nrounds, 1); ++i)
78 pthread_mutex_lock (&mut2);
79 while (! alldone)
80 pthread_cond_wait (&cond2, &mut2);
81 pthread_mutex_unlock (&mut2);
83 pthread_mutex_lock (&mut1);
84 if (! keeplock)
85 pthread_mutex_unlock (&mut1);
87 ntogo = nthreads;
88 alldone = false;
89 if (i + 1 >= nrounds)
90 last_round = true;
92 pthread_cond_broadcast (&cond1);
94 if (keeplock)
95 pthread_mutex_unlock (&mut1);
98 for (i = 0; i < nthreads; ++i)
99 if ((err = pthread_join (th[i], NULL)) != 0)
100 printf ("pthread_create: %s\n", strerror (err));
102 return 0;