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// SPDX-FileCopyrightText: 2012-2023 Brian Watling <brian@oxbo.dev>
// SPDX-License-Identifier: MIT
#include <time.h>
#include "fiber_manager.h"
#include "fiber_multi_channel.h"
#include "test_helper.h"
int send_count = 100000;
int64_t time_diff(const struct timespec* start, const struct timespec* end) {
return (end->tv_sec * 1000000000LL + end->tv_nsec) -
(start->tv_sec * 1000000000LL + start->tv_nsec);
}
void receiver(fiber_multi_channel_t* ch) {
fiber_t* this_fiber = fiber_manager_get()->current_fiber;
struct timespec last;
clock_gettime(CLOCK_MONOTONIC, &last);
int i;
for (i = 0; i < send_count; ++i) {
intptr_t n = (intptr_t)fiber_multi_channel_receive(ch);
if (n % 10000000 == 0) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
printf("%p Received 10000000 in %lf seconds\n", this_fiber,
0.000000001 * time_diff(&last, &now));
last = now;
}
}
}
void sender(fiber_multi_channel_t* ch) {
intptr_t n = 0;
int i;
for (i = 0; i < send_count; ++i) {
fiber_multi_channel_send(ch, (void*)n);
n += 1;
}
}
fiber_multi_channel_t* ch1 = NULL;
int main(int argc, char* argv[]) {
int num_threads = 4;
int count = 2;
if (argc > 1) {
count = atoi(argv[1]);
}
if (argc > 2) {
send_count = atoi(argv[2]);
}
if (argc > 3) {
num_threads = atoi(argv[3]);
}
fiber_manager_init(num_threads);
ch1 = fiber_multi_channel_create(10);
fiber_t** fibers = calloc(count, sizeof(fiber_t*));
int i;
for (i = 0; i < count; ++i) {
fibers[i] =
fiber_create(102400, (fiber_run_function_t)&receiver, (void*)ch1);
fiber_create(102400, (fiber_run_function_t)&sender, (void*)ch1);
}
for (i = 0; i < count; ++i) {
fiber_join(fibers[i], NULL);
}
free(fibers);
fiber_manager_print_stats();
fiber_shutdown();
return 0;
}