File: fiber_cond.c

package info (click to toggle)
tarantool 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 85,364 kB
  • sloc: ansic: 513,760; cpp: 69,489; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,173; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (89 lines) | stat: -rw-r--r-- 1,526 bytes parent folder | download | duplicates (4)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "memory.h"
#include "fiber.h"
#include "fiber_cond.h"
#include "unit.h"

static int
fiber_cond_basic_f(va_list ap)
{
	struct fiber_cond *cond = va_arg(ap, struct fiber_cond *);
	int *check = va_arg(ap, int *);

	int rc;

	rc = fiber_cond_wait_timeout(cond, 0.0);
	ok(rc != 0, "timeout");

	rc = fiber_cond_wait(cond);
	is(rc, 0, "signal");

	(*check)++;

	rc = fiber_cond_wait(cond);
	is(rc, 0, "broadcast");

	return 0;
}

static void
fiber_cond_basic()
{
	struct fiber_cond *cond = fiber_cond_new();
	int check = 0;

	struct fiber *f1 = fiber_new("f1", fiber_cond_basic_f);
	assert(f1 != NULL);
	fiber_start(f1, cond, &check);
	fiber_set_joinable(f1, true);

	struct fiber *f2 = fiber_new("f2", fiber_cond_basic_f);
	assert(f2 != NULL);
	fiber_start(f2, cond, &check);
	fiber_set_joinable(f2, true);

	/* check timeout */
	fiber_sleep(0.0);
	fiber_sleep(0.0);

	/* Wake up the first fiber */
	fiber_cond_signal(cond);
	fiber_sleep(0.0);

	/* Wake ip the second fiber */
	fiber_cond_signal(cond);
	fiber_sleep(0.0);

	/* Check that fiber scheduling is fair */
	is(check, 2, "order");

	fiber_cond_broadcast(cond);
	fiber_sleep(0.0);

	fiber_join(f1);
	fiber_join(f2);

	fiber_cond_delete(cond);
}

static int
main_f(va_list ap)
{
	(void) ap;
	fiber_cond_basic();
	ev_break(loop(), EVBREAK_ALL);
	return 0;
}

int
main()
{
	plan(7);
	memory_init();
	fiber_init(fiber_c_invoke);
	struct fiber *f = fiber_new("main", main_f);
	fiber_wakeup(f);
	ev_run(loop(), 0);
	fiber_free();
	memory_free();
	return check_plan();
}