summaryrefslogtreecommitdiff
path: root/lib/libc_r/BENCH/yield.c
blob: c197afd2b2dbc4e0dc317c3ed4f06b59c3921e9c (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
#include <pthread.h>
#include <sched.h>
#include <string.h>
#include <err.h>
#include "bench.h"

static char name[] =  "Thread Yield Time (Busy)";
static char doc[] = 
"\tThread yield time is defined as the amount of time between that\n"
"\tpoint when a running thread voluntarily gives up the CPU until\n"
"\tthe highest priority runnable thread begins execution of its\n"
"\tapplication code.";

#ifdef DEBUG
volatile int state = 0;
#endif
bench_t	b;

void *
other_thread(arg)
	void *arg;
{

	pthread_set_name_np(pthread_self(), "oth");
	bench_amortize(&b, BENCH_LOOPS) {
#ifdef DEBUG
		if (state != 0)	abort();
		state = 1;
#endif
		sched_yield();
	}
}

int
main() {
	pthread_t other;

	bench_init(&b, name, doc, "per yield");
	b.n = BENCH_LOOPS;
	bench_header(&b);
	pthread_create(&other, NULL, other_thread, NULL);
	while (b.i < b.n) {
#ifdef DEBUG
		if (state != 1) abort();
		state = 0;
#endif
		sched_yield();
	}
	pthread_join(other, NULL);
	b.divisor = 2;
	bench_report(&b);
	exit(0);
}