-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler_experiments.cpp
125 lines (110 loc) · 3.86 KB
/
scheduler_experiments.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright © 2018-2023 ButenkoMS. All rights reserved.
// Licensed under the Apache License, Version 2.0.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <iostream>
#include <boost/chrono.hpp>
#include "coroutine.h"
#include "scheduler.h"
using namespace boost::chrono;
class my_payload {
public:
my_payload(long long iter_index) {
this->iter_index = iter_index;
}
long long iter_index;
};
void serve_coroutine(void* coro_payload, struct ServerData *server_data)
{
printf("\n<<<<\n");
printf("SC >> SERVE COROUTINE\n");
class my_payload* payload = (class my_payload*) coro_payload;
coro_id current_id = coroutine_id(server_current_coro(server_data));
printf("SC %llu >> SERVE COROUTINE: iter_index == %d\n", current_id, payload->iter_index);
printf("SC %llu >> SERVE COROUTINE: BEFORE yield\n", current_id);
if (server_data)
{
int* request = (int*)malloc(sizeof(*request));
*request = -current_id;
int* response = (int*) server_request(server_data, CoroRequestRevertSign, (void*)request);
printf("SC %llu >> SERVE COROUTINE: server_data response is %d!\n\n", current_id, *response);
server_request(server_data, CoroRequestYield, NULL);
} else {
printf("SC %llu >> SERVE COROUTINE: server_data is NULL!\n\n", current_id);
}
printf("SC %llu >> SERVE COROUTINE: AFTER yield\n\n", current_id);
delete payload;
printf("SC %llu >> SERVE COROUTINE: AFTER delete\n\n", current_id);
printf(">>>>\n\n");
}
/*********************************************************************
*
* Function : listen_loop_coro
*
* Description : bind the listen port and enter a "FOREVER" listening loop.
*
* Parameters : N/A
*
* Returns : Never.
*
*********************************************************************/
void listen_loop_coro()
{
printf("\n=============\nS >> PAUSED. SERVER PAUSED BEFORE DATA CREATION.");
getchar();
std::cout << high_resolution_clock::now() << "\n";
struct ServerData *server_data = server_create();
std::cout << high_resolution_clock::now() << "\n";
printf("S >> SERVER DATA CREATION DONE.\n=============\n");
printf("\n=============\nS >> PAUSED. BEFORE LOOP.");
getchar();
std::cout << high_resolution_clock::now() << "\n";
long long iter_index = 0;
for (;;)
{
int child_id = -1;
int max_coro_qnt = 10;
if (max_coro_qnt > iter_index)
{
my_payload* payload = new my_payload(iter_index);
child_id = server_register_coro(server_data, serve_coroutine, payload);
if (0 > child_id) {
printf("S >> FAILED TO CREATE CORO FOR A iter_indes == %d\n", iter_index);
}
}
else if (max_coro_qnt <= iter_index)
{
std::cout << high_resolution_clock::now() << "\n";
printf("S >> LOOP FINISHED ON ITERATION %d\n=============\n", iter_index);
break;
}
iter_index++;
}
printf("\n=============\nS >> PAUSED. BEFORE SERVER PROCESSING STARTED. COROUTINES CREATED: %d.", iter_index);
int c = getchar();
std::cout << high_resolution_clock::now() << "\n";
while (server_loop_iteration(server_data))
{
printf("S >> NUMBER OF LIVE COROUTINES: %d\n\n", server_data->last_live_coroutines_num);
}
std::cout << high_resolution_clock::now() << "\n";
printf("S >> SERVER PROCESSING FINISHED.\n=============\n");
printf("\n=============\nS >> PAUSED. SERVER PAUSED BEFORE DATA FREE.");
getchar();
std::cout << high_resolution_clock::now() << "\n";
server_free(server_data);
std::cout << high_resolution_clock::now() << "\n";
printf("S >> SERVER DATA FREE DONE.\n=============\n");
printf("\n=============\nS >> PAUSED. BEFORE EXIT.");
getchar();
}
int main()
{
printf("S >> SERVER START\n");
listen_loop_coro();
printf("S >> SERVER END\n");
return 0;
}