-
Notifications
You must be signed in to change notification settings - Fork 0
/
coro_manager_poc.c
628 lines (578 loc) · 17.7 KB
/
coro_manager_poc.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
// Copyright © 2018-2023 ButenkoMS. All rights reserved.
// Licensed under the Apache License, Version 2.0.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "coroutine.h"
enum CoroRequests
{
CoroRequestNone = 0,
CoroRequestYield = 1,
CoroRequestSleep,
CoroRequestSocketRead,
CoroRequestSocketWrite
};
struct CoroData
{
int coro_id;
void *data;
};
struct ServerData
{
struct schedule *shed;
int coro_list_len;
struct CoroData *coro_list; // data is respone
int pending_coro_list_len;
struct CoroData *pending_coro_list; // data is request
enum CoroRequests coro_request_type;
void *request;
void *response;
};
struct RequestData
{
enum CoroRequests coro_request_type;
void *request;
};
struct CoroArgs
{
struct ServerData *server_data;
struct client_state *csp;
int n;
};
static void serv_coro(struct schedule *S, void *ud);
static void listen_loop_coro();
static int find_free_cell(struct CoroData *coro_list, int coro_list_len);
static void server_put_coro_to_list(struct CoroData *coro_list,
int coro_index, int coro_id, void *data);
static void *memcp_to_bigger(void *data, long long data_size, long long new_size, int default_value);
static int put_or_realloc(struct CoroData **coro_list_ptr, int *coro_list_len_ptr, int coro_id);
static void server_free_request(struct ServerData *server_data);
static void server_free_response(struct ServerData *server_data);
static void *server_request(struct ServerData *server_data,
enum CoroRequests coro_request_type,
void *request);
static int server_register_coro(struct ServerData *server_data, int n);
static void server_free_coro_args(struct CoroArgs *coro_args);
static void server_free_request_data(struct RequestData *request_data);
static void server_free_response_data(void *response);
static void remove_coro_from_list_imp(struct CoroData *coro_list, int coro_index);
static void server_remove_coro(struct ServerData *server_data, int coro_index);
static void server_remove_pending_coro(struct ServerData *server_data, int coro_index);
static void server_loop_coro(struct ServerData *server_data);
static void server_move_request_to_services(struct ServerData *server_data, int coro_index);
static void server_move_response_to_coro(struct ServerData *server_data, int coro_id, void *response);
static void server_put_request_to_service(struct ServerData *server_data, int coro_id, struct RequestData *request_data);
static void server_run_all_services(struct ServerData *server_data);
static void server_loop_services(struct ServerData *server_data);
static void server_loop(struct ServerData *server_data);
static struct ServerData *server_create();
static void server_free(struct ServerData *server_data);
static void
foo(struct ServerData *server_data, int n)
{
printf("COROUTINE FOO (%d) - START\n", n);
int start = n;
int i;
for (i = 0; i < 500; i++)
{
printf("COROUTINE FOO (%d) %d : %d\n", n, coroutine_running(server_data->shed), start + i);
if (server_data)
{
server_request(server_data, CoroRequestYield, NULL);
}
}
printf("COROUTINE FOO (%d) - END\n", n);
}
static void
bar(struct ServerData *server_data, int n)
{
printf("COROUTINE BAR (%d) - START\n", n);
int start = n;
int i;
for (i = 0; i < 500; i++)
{
printf("COROUTINE BAR (%d) %d : %d\n", n, coroutine_running(server_data->shed), start + i);
if (server_data)
{
server_request(server_data, CoroRequestYield, NULL);
}
}
printf("COROUTINE BAR (%d) - END\n", n);
}
static void serv_coro(struct schedule *S, void *ud)
{
struct CoroArgs *coro_args = (struct CoroArgs *)ud;
if (coro_args->n % 2)
{
foo(coro_args->server_data, coro_args->n);
}
else
{
bar(coro_args->server_data, coro_args->n);
}
server_free_coro_args(coro_args);
}
/*********************************************************************
*
* Function : listen_loop_coro
*
* Description : bind the listen port and enter a "FOREVER" listening loop.
*
* Parameters : N/A
*
* Returns : Never.
*
*********************************************************************/
static void listen_loop_coro()
{
printf("SERVER PAUSED BEFORE DATA CREATION.\n");
getchar();
struct ServerData *server_data = server_create();
printf("SERVER DATA CREATED. PAUSED.\n");
getchar();
long long iter_index = 0;
for (;;)
{
DEBUG_PRINTF(("ITERATION %d START\n", iter_index));
int child_id;
int max_coro_qnt = 10000;
if (max_coro_qnt > iter_index)
{
if ((max_coro_qnt>>1) == iter_index)
{
printf("LOOP PAUSED ON ITERATION %d\n", iter_index);
int c = getchar();
}
DEBUG_PRINTF(("BEFORE REGISTER NEW CORO\n"));
child_id = server_register_coro(server_data, iter_index);
DEBUG_PRINTF(("NEW CORO %d REGISTERED\n", child_id));
}
else if ((max_coro_qnt + 20) == iter_index)
{
printf("LOOP PAUSED ON ITERATION %d\n", iter_index);
int c = getchar();
}
else if ((max_coro_qnt + 60) == iter_index)
{
printf("LOOP FINISHED ON ITERATION %d\n", iter_index);
break;
}
server_loop(server_data);
DEBUG_PRINTF(("ITERATION %d END\n", iter_index));
iter_index++;
}
printf("SERVER PAUSED BEFORE DATA FREE.\n");
getchar();
server_free(server_data);
printf("SERVER DATA FREE. PAUSED.\n");
getchar();
}
static int find_free_cell(struct CoroData *coro_list, int coro_list_len)
{
for (int i = 0; i < coro_list_len; i++)
{
if (0 > coro_list[i].coro_id)
{
return i;
}
}
return -1;
}
static void server_put_coro_to_list(struct CoroData *coro_list,
int coro_index, int coro_id, void *data)
{
coro_list[coro_index].coro_id = coro_id;
coro_list[coro_index].data = data;
}
static void *memcp_to_bigger(void *data, long long data_size, long long new_size, int default_value)
{
if (new_size < data_size)
{
return NULL;
}
void *new_data = malloc(new_size);
if (!new_data)
{
return NULL;
}
memset(new_data, default_value, new_size);
memcpy(new_data, data, data_size);
free(data);
return new_data;
}
static int put_or_realloc(struct CoroData **coro_list_ptr, int *coro_list_len_ptr, int coro_id)
{
int add_coro_list_len = 1024;
int free_cell = find_free_cell(*coro_list_ptr, (*coro_list_len_ptr));
if (0 <= free_cell)
{
server_put_coro_to_list(*coro_list_ptr, free_cell, coro_id, NULL);
}
else
{
int coro_list_size = sizeof(struct CoroData) * (*coro_list_len_ptr);
int new_coro_list_len = (*coro_list_len_ptr) + add_coro_list_len;
(*coro_list_len_ptr) = new_coro_list_len;
int new_coro_list_size = sizeof(struct CoroData) * new_coro_list_len;
(*coro_list_ptr) = memcp_to_bigger(
(void *)(*coro_list_ptr),
coro_list_size,
new_coro_list_size,
-1);
if (!(*coro_list_ptr))
{
(*coro_list_len_ptr) = 0;
return -1;
}
free_cell = find_free_cell((*coro_list_ptr), (*coro_list_len_ptr));
if (0 <= free_cell)
{
server_put_coro_to_list((*coro_list_ptr), free_cell, coro_id, NULL);
}
else
{
return -1;
}
}
return free_cell;
}
static void server_free_request(struct ServerData *server_data)
{
if (!server_data)
{
return;
}
if (server_data->request)
{
free(server_data->request);
server_data->request = NULL;
}
server_data->coro_request_type = CoroRequestNone;
}
static void server_free_response(struct ServerData *server_data)
{
if (!server_data)
{
return;
}
if (server_data->response)
{
free(server_data->response);
server_data->response = NULL;
}
}
static void *server_request(struct ServerData *server_data,
enum CoroRequests coro_request_type,
void *request)
{
if (!server_data)
{
return NULL;
}
server_free_request(server_data);
server_free_response(server_data);
server_data->coro_request_type = coro_request_type;
server_data->request = request;
coroutine_yield(server_data->shed);
return server_data->response;
}
static int server_register_coro(struct ServerData *server_data, int n)
{
if (!server_data)
{
return -1;
}
int coro_index = put_or_realloc(&(server_data->coro_list), &(server_data->coro_list_len), -1);
if (0 > coro_index)
{
return -1;
}
struct CoroArgs *coro_args = (struct CoroArgs *)malloc(sizeof(struct CoroArgs));
coro_args->server_data = server_data;
coro_args->n = n;
int coro_id = coroutine_new(server_data->shed, serv_coro, coro_args, NULL);
coro_index = put_or_realloc(&(server_data->coro_list), &(server_data->coro_list_len), coro_id);
return coro_index;
}
static void server_free_coro_args(struct CoroArgs *coro_args)
{
free(coro_args);
}
static void server_free_request_data(struct RequestData *request_data)
{
if (!request_data)
{
return;
}
request_data->coro_request_type = CoroRequestNone;
if (request_data->request)
{
free(request_data->request);
request_data->request = NULL;
}
free(request_data);
}
static void server_free_response_data(void *response)
{
if (!response)
{
return;
}
free(response);
}
static void remove_coro_from_list_imp(struct CoroData *coro_list, int coro_index)
{
coro_list[coro_index].coro_id = -1;
coro_list[coro_index].data = NULL;
}
static void server_remove_coro(struct ServerData *server_data, int coro_index)
{
remove_coro_from_list_imp(server_data->coro_list, coro_index);
}
static void server_remove_pending_coro(struct ServerData *server_data, int coro_index)
{
remove_coro_from_list_imp(server_data->pending_coro_list, coro_index);
}
static void server_loop_coro(struct ServerData *server_data)
{
DEBUG_PRINTF(("SERVER LOOP CORO - START\n"));
if (!server_data)
{
DEBUG_PRINTF(("SERVER LOOP CORO - ERROR. EXITING\n"));
return;
}
for (int i = 0; i < server_data->coro_list_len; i++)
{
int coro_id = server_data->coro_list[i].coro_id;
void *coro_data = server_data->coro_list[i].data;
if (0 > coro_id)
{
continue;
}
bool need_to_remove_coro = false;
if (coroutine_status(server_data->shed, coro_id))
{
server_free_request(server_data);
if (coro_data)
{
server_data->response = coro_data;
}
coroutine_resume(server_data->shed, coro_id);
server_free_response(server_data);
if (coroutine_status(server_data->shed, coro_id))
{
server_move_request_to_services(server_data, i);
}
else
{
need_to_remove_coro = true;
}
}
else
{
need_to_remove_coro = true;
}
if (need_to_remove_coro)
{
if (server_data->coro_list[i].data)
{
free(server_data->coro_list[i].data);
}
server_remove_coro(server_data, i);
}
}
server_free_request(server_data);
server_free_response(server_data);
DEBUG_PRINTF(("SERVER LOOP CORO - END\n"));
}
static void server_move_request_to_services(struct ServerData *server_data, int coro_index)
{
int coro_id = server_data->coro_list[coro_index].coro_id;
server_remove_coro(server_data, coro_index);
enum CoroRequests coro_request_type = server_data->coro_request_type;
server_data->coro_request_type = CoroRequestNone;
void *request = server_data->request;
server_data->request = NULL;
struct RequestData *request_data = (struct RequestData *)malloc(sizeof(struct RequestData));
request_data->coro_request_type = coro_request_type;
request_data->request = request;
coro_index = put_or_realloc(&(server_data->pending_coro_list), &(server_data->pending_coro_list_len), coro_id);
if (0 > coro_index)
{
return;
}
server_data->pending_coro_list[coro_index].data = (void *)request_data;
}
static void server_move_response_to_coro(struct ServerData *server_data, int coro_id, void *response)
{
int coro_index = put_or_realloc(&(server_data->coro_list), &(server_data->coro_list_len), coro_id);
if (0 > coro_index)
{
return;
}
server_data->coro_list[coro_index].data = response;
}
static void server_put_request_to_service(struct ServerData *server_data, int coro_id, struct RequestData *request_data)
{
switch (request_data->coro_request_type)
{
case CoroRequestSocketRead:
{
break;
}
case CoroRequestSleep:
{
break;
}
case CoroRequestYield:
default:
{
server_move_response_to_coro(server_data, coro_id, NULL);
}
}
}
static void server_run_all_services(struct ServerData *server_data)
{
DEBUG_PRINTF(("SERVER RUN ALL SERVICES - START\n"));
DEBUG_PRINTF(("SERVER RUN ALL SERVICES - END\n"));
}
static void server_loop_services(struct ServerData *server_data)
{
DEBUG_PRINTF(("SERVER LOOP SERVICES - START\n"));
if (!server_data)
{
DEBUG_PRINTF(("SERVER LOOP SERVICES - ERROR. EXITING\n"));
return;
}
for (int i = 0; i < server_data->pending_coro_list_len; i++)
{
int coro_id = server_data->pending_coro_list[i].coro_id;
if (0 > coro_id)
{
continue;
}
struct RequestData *request_data = (struct RequestData *)(server_data->pending_coro_list[i].data);
server_data->pending_coro_list[i].data = NULL;
server_remove_pending_coro(server_data, i);
server_put_request_to_service(server_data, coro_id, request_data);
server_free_request_data(request_data);
}
server_run_all_services(server_data);
DEBUG_PRINTF(("SERVER LOOP SERVICES - END\n"));
}
static void server_loop(struct ServerData *server_data)
{
DEBUG_PRINTF(("SERVER LOOP - START\n"));
if (!server_data)
{
DEBUG_PRINTF(("SERVER LOOP - ERROR. EXITING\n"));
return;
}
server_loop_coro(server_data);
server_loop_services(server_data);
DEBUG_PRINTF(("SERVER LOOP - FINISH\n"));
}
static struct ServerData *server_create()
{
struct ServerData *server_data = (struct ServerData *)malloc(sizeof(struct ServerData));
server_data->shed = coro_server_open();
server_data->coro_list_len = 1024;
size_t coro_list_size = sizeof(struct CoroData) * server_data->coro_list_len;
server_data->coro_list = (struct CoroData *)malloc(coro_list_size);
// memset(server_data->coro_list, -1, coro_list_size);
for (int i = 0; i < server_data->coro_list_len; i++)
{
server_data->coro_list[i].coro_id = -1;
server_data->coro_list[i].data = NULL;
}
server_data->pending_coro_list_len = server_data->coro_list_len;
size_t pending_coro_list_size = sizeof(struct CoroData) * server_data->pending_coro_list_len;
server_data->pending_coro_list = (struct CoroData *)malloc(pending_coro_list_size);
// memset(server_data->pending_coro_list, -1, pending_coro_list_size);
for (int i = 0; i < server_data->pending_coro_list_len; i++)
{
server_data->pending_coro_list[i].coro_id = -1;
server_data->pending_coro_list[i].data = NULL;
}
server_data->coro_request_type = CoroRequestNone;
server_data->request = NULL;
server_data->response = NULL;
return server_data;
}
static void server_free(struct ServerData *server_data)
{
DEBUG_PRINTF(("SERVER FREE - START\n"));
if (!server_data)
{
DEBUG_PRINTF(("SERVER FREE - ERROR. EXITING\n"));
return;
}
for (int i = 0; i < server_data->coro_list_len; i++)
{
int coro_id = server_data->coro_list[i].coro_id;
if (0 <= coro_id)
{
server_data->coro_list[i].coro_id = -1;
}
void *data = server_data->coro_list[i].data;
if (data)
{
free(data);
server_data->coro_list[i].data = NULL;
}
// DEBUG_PRINTF(("SERVER FREE - CORO LIST DATA ITEM %d - FLUSHED AND FREE\n", i));
}
DEBUG_PRINTF(("SERVER FREE - CORO LIST DATA - FREE\n"));
server_data->coro_list_len = 0;
free(server_data->coro_list);
server_data->coro_list = NULL;
DEBUG_PRINTF(("SERVER FREE - CORO LIST - FREE\n"));
for (int i = 0; i < server_data->pending_coro_list_len; i++)
{
int coro_id = server_data->pending_coro_list[i].coro_id;
if (0 <= coro_id)
{
server_data->pending_coro_list[i].coro_id = -1;
}
void *data = server_data->pending_coro_list[i].data;
if (data)
{
free(data);
server_data->pending_coro_list[i].data = NULL;
}
// DEBUG_PRINTF(("SERVER FREE - PENDING CORO LIST DATA ITEM %d - FLUSHED AND FREE\n", i));
}
DEBUG_PRINTF(("SERVER FREE - PENDING CORO LIST DATA - FREE\n"));
server_data->pending_coro_list_len = 0;
free(server_data->pending_coro_list);
server_data->pending_coro_list = NULL;
DEBUG_PRINTF(("SERVER FREE - PENDING CORO LIST - FREE\n"));
coro_server_close(server_data->shed);
DEBUG_PRINTF(("SERVER FREE - COROUTINE - CLOSED\n"));
server_data->coro_request_type = CoroRequestNone;
DEBUG_PRINTF(("SERVER FREE - CORO REQUEST TYPE - FLUSHED\n"));
if (server_data->request)
{
free(server_data->request);
server_data->request = NULL;
}
DEBUG_PRINTF(("SERVER FREE - REQUEST - FLUSHED AND FREE\n"));
if (server_data->response)
{
free(server_data->response);
server_data->response = NULL;
}
DEBUG_PRINTF(("SERVER FREE - RESPONSE - FLUSHED AND FREE\n"));
free(server_data);
DEBUG_PRINTF(("SERVER FREE - SERVER DATA - FREE\n"));
server_data = NULL;
DEBUG_PRINTF(("SERVER FREE - END\n"));
}
int main()
{
printf("SERVER START\n");
listen_loop_coro();
printf("SERVER END\n");
return 0;
}