-
Notifications
You must be signed in to change notification settings - Fork 1
/
fibonacci.c
55 lines (45 loc) · 1.17 KB
/
fibonacci.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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "my_coroutines.h"
intptr_t fibonacci(intptr_t arg) {
// Keeps yielding fibonacci numbers until it is passed a true value
uintptr_t a = 0;
uintptr_t b = 1;
while(arg) {
uintptr_t c = a + b;
a = b;
b = c;
arg = co_yield(a);
}
return -1; // last await
}
int main(void) {
co_handle_t fib_handle = co_allocate(fibonacci);
for (intptr_t i = co_await(fib_handle, true); i > 0; i = co_await(fib_handle, true)) { // keep getting values until one is biggern than INTPTR_T_MAX
printf("%ld\n", i);
}
co_await(fib_handle, false);
co_free(fib_handle);
}
/*
void *test_func(void *arg) {
return (void*)(4 + (uintptr_t)arg);
}
int main(void) {
void **test = co_allocate(test_func);
printf("handle: %p\n\n", test);
printf("ip: %p\n", test[0]);
printf("sp: %p\n", test[1]);
printf("bp: %p\n", test[2]);
printf("next: %p\n\n", test[3]);
void **stack = test[1];
printf("arg (on stack): %p\n\n", stack[-1]);
printf("Finished? : %s\n", co_finished(test) ? "yes" : "no");
free(test);
}
void *test(void *(*func)(void*), void *arg) {
void *stack = malloc(2 << 20);
void **state_info = malloc(4 * sizeof(void*));
}
*/