-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
72 lines (49 loc) · 1.46 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include "coroutines.c"
#include "functions.c"
/*
Author: Hamad Al Marri
Credits: https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
by Simon Tatham
*/
void callbackFib(long f) {
printf("callbackFib:\t\t%ld\n", f);
}
int main() {
initializeCoroutines();
// simple coroutine example
addCoroutine(&print_numbers, NULL);
// another simple coroutine example
addCoroutine(&print_letters, NULL);
// passing arguments to coroutine function example 1
void* args[] = {COROUTINE_ARG "$$$$$", COROUTINE_ARG 6};
addCoroutine(&print_args1, args);
// passing arguments to coroutine function example 2
void* args2[] = {COROUTINE_ARG "####", COROUTINE_ARG 11};
addCoroutine(&print_args2, args2);
// calling the same coroutine function 5 times example
void *args3[5][3];
for (long i = 0; i < 5; ++i) {
char *is = (char*) malloc(sizeof(long));
snprintf (is, sizeof(is), "%ld", i);
args3[i][0] = COROUTINE_ARG is; // name
args3[i][1] = COROUTINE_ARG 0; // i
args3[i][2] = COROUTINE_ARG 10; // times
addCoroutine(&multipule_copies, args3[i]);
}
// callback function is passed to args example
void* args4[] = {
COROUTINE_ARG 0, // i
COROUTINE_ARG 0, // a
COROUTINE_ARG 1, // b
COROUTINE_ARG 20, // size
COROUTINE_ARG &callbackFib, // callback function
};
addCoroutine(&fib, args4);
startCoroutines();
// free memory
for (long i = 0; i < 5; ++i)
free(args3[i][0]);
return 0;
}