summaryrefslogtreecommitdiff
path: root/lib/libc_r/BENCH/cond_wake.c
blob: c654a674bd27b278f711311c5831b308d1f7b45a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include <string.h>
#include <err.h>
#include "bench.h"

static char name[] =  "Condition Variable, Wake Up";
static char doc[] = 
"\tThis is the amount of time from when one thread calls\n"
"\tpthread_cond_signal() and a thread blocked on that condition\n"
"\tvariable returns from its pthread_cond_wait() call. The condition\n"
"\tand its associated mutex should not be used by any other thread.\n"
"\tMetrics shall be provided for both the case when the\n"
"\tpthread_cond_signal() call is executed under the associated mutex,\n"
"\tas well as not under the mutex.";

/* BROKEN */

pthread_mutex_t	m1, m2;
pthread_cond_t	c;
bench_t	b;

void *
other_thread(arg)
	void *arg;
{

	pthread_set_name_np(pthread_self(), "oth");
	pthread_mutex_lock(&m2);
	
	bench_amortize(&b, BENCH_LOOPS) {
		pthread_cond_wait(&c, &m2);
		pthread_cond_signal(&c);
	}
	pthread_mutex_unlock(&m2);
}

int
main() {
	pthread_t other;
	bench_init(&b, name, doc, "per call");
	b.n = BENCH_LOOPS;
	bench_header(&b);
	pthread_cond_init(&c, NULL);
	pthread_mutex_init(&m1, NULL);
	pthread_mutex_init(&m2, NULL);
	pthread_mutex_lock(&m1);
	pthread_create(&other, NULL, other_thread, NULL);

	sched_yield();
	while (b.i < b.n) {
		pthread_cond_signal(&c);
		pthread_cond_wait(&c, &m1);
	}

	b.divisor = 2;
	bench_report(&b);
	pthread_join(other, NULL);
	exit(0);
}