-
Notifications
You must be signed in to change notification settings - Fork 4
/
eventfd.c
82 lines (65 loc) · 1.8 KB
/
eventfd.c
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/eventfd.h>
#include <unistd.h>
typedef struct state {
int tx, rx; // relative to parent
} state;
state *new_state() { return calloc(sizeof(state), 1); }
void free_state(state *state) { free(state); }
int pre_fork_setup(state *state) {
if ((state->tx = eventfd(0 /* initval */, 0 /* flags */)) == -1) {
// uh-oh
}
if ((state->rx = eventfd(0 /* initval */, 0 /* flags */)) == -1) {
// uh-oh
}
return 0;
}
int cleanup(state *state __attribute__((unused))) { return 0; }
int child_post_fork_setup(state *state __attribute__((unused))) { return 0; }
int child_warmup(int warmup_iters __attribute__((unused)),
state *state __attribute__((unused))) {
return 0;
}
int child_loop(int warmup_iters __attribute__((unused)), state *state) {
for (;;) {
char msg[8];
read(state->tx, msg, 8);
write(state->rx, "\0\0\0\0\0\0\0\1", 8);
}
return 0;
}
int child_cleanup(state *state __attribute__((unused))) { return 0; }
int parent_post_fork_setup(state *state __attribute__((unused))) { return 0; }
int parent_warmup(int warmup_iters, state *state) {
int i;
for (i = 0; i < warmup_iters; ++i) {
char resp[8];
if (write(state->tx, "\0\0\0\0\0\0\0\1", 8) == -1) {
perror("could not write");
break;
};
if (read(state->rx, &resp, 8) == -1) {
perror("could not read");
break;
}
}
return 0;
}
int parent_loop(int iters, state *state) {
int i;
for (i = 0; i < iters; ++i) {
char resp[8];
if (write(state->tx, "\0\0\0\0\0\0\0\1", 8) == -1) {
perror("could not write");
break;
};
if (read(state->rx, &resp, 8) == -1) {
perror("could not read");
break;
}
}
return 0;
}
int parent_cleanup(state *state __attribute__((unused))) { return 0; }