| 1 | #include <stdio.h> |
| 2 | #include <stddef.h> |
| 3 | #include "coroutine.h" |
| 4 | #include "generator.h" |
| 5 | #include "asleep.h" |
| 6 | #include "task.h" |
| 7 | |
| 8 | #include <dirent.h> |
| 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <time.h> |
| 12 | |
| 13 | #define DEMO_STACK_SIZE (8192*sizeof(void *)) |
| 14 | |
| 15 | Coroutine *cor_main; |
| 16 | |
| 17 | typedef struct { |
| 18 | intptr_t headroom; |
| 19 | bool canstartcoroutine; |
| 20 | } TestResult; |
| 21 | |
| 22 | |
| 23 | void *dotests( |
| 24 | void *param |
| 25 | ){ |
| 26 | (void)param; |
| 27 | |
| 28 | _Coroutine_Dump(); |
| 29 | Coroutine *active = Coroutine_GetActive(); |
| 30 | Coroutine *a = Coroutine_New(DEMO_STACK_SIZE, dotests); |
| 31 | Coroutine *b = Coroutine_New(DEMO_STACK_SIZE, dotests); |
| 32 | Coroutine *c = Coroutine_New(DEMO_STACK_SIZE, dotests); |
| 33 | printf("[stack=aabbcc]\n"); |
| 34 | _Coroutine_Dump(); |
| 35 | printf("%ld %ld %ld (a, b, c: a < b < c)\n", (unsigned char *)active - (unsigned char *)a, (unsigned char *)active - (unsigned char *)b, (unsigned char *)active - (unsigned char *)c); |
| 36 | Coroutine_Delete(b); |
| 37 | printf("[stack=aaffcc]\n"); |
| 38 | _Coroutine_Dump(); |
| 39 | b = Coroutine_New(DEMO_STACK_SIZE, dotests); |
| 40 | printf("[stack=aabbcc]\n"); |
| 41 | _Coroutine_Dump(); |
| 42 | printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b); |
| 43 | Coroutine_Delete(b); |
| 44 | printf("[stack=aaffcc]\n"); |
| 45 | _Coroutine_Dump(); |
| 46 | Coroutine *d = Coroutine_New(DEMO_STACK_SIZE*2, dotests); |
| 47 | printf("[stack=aaffccdddd]\n"); |
| 48 | printf("%ld (d: c < d)\n", (unsigned char *)active - (unsigned char *)d); |
| 49 | _Coroutine_Dump(); |
| 50 | printf("[testing merge before]\n"); |
| 51 | Coroutine_Delete(c); |
| 52 | printf("[stack=aaffffdddd]\n"); |
| 53 | _Coroutine_Dump(); |
| 54 | b = Coroutine_New(DEMO_STACK_SIZE*2, dotests); |
| 55 | printf("[stack=aabbbbdddd]\n"); |
| 56 | printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b); |
| 57 | _Coroutine_Dump(); |
| 58 | printf("[testing merge after]\n"); |
| 59 | Coroutine_Delete(b); |
| 60 | printf("[stack=aaffffdddd]\n"); |
| 61 | _Coroutine_Dump(); |
| 62 | Coroutine_Delete(a); |
| 63 | printf("[stack=ffffffdddd]\n"); |
| 64 | _Coroutine_Dump(); |
| 65 | a = Coroutine_New(DEMO_STACK_SIZE*2, dotests); |
| 66 | printf("[stack=aaaaffdddd]\n"); |
| 67 | printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a); |
| 68 | _Coroutine_Dump(); |
| 69 | printf("[testing merge both ways]\n"); |
| 70 | Coroutine_Delete(a); |
| 71 | printf("[stack=ffffffdddd]\n"); |
| 72 | _Coroutine_Dump(); |
| 73 | a = Coroutine_New(DEMO_STACK_SIZE, dotests); |
| 74 | printf("[stack=aaffffdddd]\n"); |
| 75 | _Coroutine_Dump(); |
| 76 | printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a); |
| 77 | b = Coroutine_New(DEMO_STACK_SIZE, dotests); |
| 78 | printf("[stack=aabbffdddd]\n"); |
| 79 | _Coroutine_Dump(); |
| 80 | printf("%ld (b again)\n", (unsigned char *)active - (unsigned char *)b); |
| 81 | c = Coroutine_New(DEMO_STACK_SIZE, dotests); |
| 82 | printf("[stack=aabbccdddd]\n"); |
| 83 | printf("%ld (c again)\n", (unsigned char *)active - (unsigned char *)c); |
| 84 | Coroutine_Delete(a); |
| 85 | printf("[stack=ffbbccdddd]\n"); |
| 86 | Coroutine_Delete(c); |
| 87 | printf("[stack=ffbbffdddd]\n"); |
| 88 | Coroutine_Delete(b); |
| 89 | printf("[stack=ffffffdddd]\n"); |
| 90 | _Coroutine_Dump(); |
| 91 | a = Coroutine_New(DEMO_STACK_SIZE*3, dotests); |
| 92 | printf("[stack=aaaaaadddd]\n"); |
| 93 | printf("%ld (a again)\n", (unsigned char *)active - (unsigned char *)a); |
| 94 | _Coroutine_Dump(); |
| 95 | |
| 96 | Coroutine_Delete(a); |
| 97 | printf("[stack=ffffffdddd]\n"); |
| 98 | _Coroutine_Dump(); |
| 99 | Coroutine_Delete(d); |
| 100 | printf("[stack=fffffffffff]\n"); |
| 101 | _Coroutine_Dump(); |
| 102 | a = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests); |
| 103 | printf("[stack=affffffffff]\n"); |
| 104 | _Coroutine_Dump(); |
| 105 | b = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests); |
| 106 | printf("[stack=abfffffffff]\n"); |
| 107 | _Coroutine_Dump(); |
| 108 | c = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests); |
| 109 | printf("[stack=abcffffffff]\n"); |
| 110 | _Coroutine_Dump(); |
| 111 | Coroutine_Delete(a); |
| 112 | printf("[stack=fbcffffffff]\n"); |
| 113 | _Coroutine_Dump(); |
| 114 | Coroutine_Delete(b); |
| 115 | printf("[stack=ffcffffffff]\n"); |
| 116 | _Coroutine_Dump(); |
| 117 | a = Coroutine_New(COROUTINE_MINIMUM_STACK_SIZE, dotests); |
| 118 | printf("[stack=afcffffffff]\n"); |
| 119 | _Coroutine_Dump(); |
| 120 | |
| 121 | Coroutine_Delete(c); |
| 122 | printf("[stack=affffffffff]\n"); |
| 123 | _Coroutine_Dump(); |
| 124 | |
| 125 | Coroutine_Delete(a); |
| 126 | printf("[stack=fffffffffff]\n"); |
| 127 | _Coroutine_Dump(); |
| 128 | |
| 129 | return NULL; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | int main(int argc, char *argv[]) { |
| 134 | (void)argc; |
| 135 | (void)argv; |
| 136 | printf("Hello\n"); |
| 137 | |
| 138 | // what stack do we get with no stack limit set |
| 139 | printf("%d\n", Coroutine_Run(DEMO_STACK_SIZE, dotests, NULL, NULL)); |
| 140 | } |
| 141 | |