| 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 *stacktest( |
| 24 | void *param |
| 25 | ){ |
| 26 | TestResult *testresult = (TestResult *)param; |
| 27 | if (Coroutine_CanStartCoroutine(DEMO_STACK_SIZE)){ |
| 28 | Coroutine *cor = Coroutine_New(DEMO_STACK_SIZE, stacktest); |
| 29 | Coroutine_Delete(cor); |
| 30 | } |
| 31 | testresult->headroom = Coroutine_GetStackHeadroom(); |
| 32 | testresult->canstartcoroutine = Coroutine_CanStartCoroutine(DEMO_STACK_SIZE); |
| 33 | |
| 34 | return NULL; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | int main(int argc, char *argv[]) { |
| 39 | (void)argc; |
| 40 | (void)argv; |
| 41 | |
| 42 | unsigned char *stack_now = (unsigned char *)&argc; |
| 43 | |
| 44 | printf("Various stack headrooms:\n"); |
| 45 | intptr_t limitnocoroutine; |
| 46 | TestResult testresult; |
| 47 | |
| 48 | // what stack do we get with no stack limit set |
| 49 | limitnocoroutine = Coroutine_GetStackHeadroom(); |
| 50 | Coroutine_Run(DEMO_STACK_SIZE, stacktest, &testresult, NULL); |
| 51 | printf("No stack limit: %ld %ld %d\n", limitnocoroutine, testresult.headroom, testresult.canstartcoroutine); |
| 52 | |
| 53 | // what stack do we get with an undersize stack limit |
| 54 | for (size_t stacksize = DEMO_STACK_SIZE/2; stacksize < DEMO_STACK_SIZE*4; stacksize += DEMO_STACK_SIZE/2){ |
| 55 | Coroutine_SetStackLimit(stack_now - stacksize); |
| 56 | limitnocoroutine = Coroutine_GetStackHeadroom(); |
| 57 | Coroutine_Run(DEMO_STACK_SIZE, stacktest, &testresult, NULL); |
| 58 | printf("Stack=%ld: %ld, %ld %d\n", stacksize, limitnocoroutine, testresult.headroom, testresult.canstartcoroutine); |
| 59 | } |
| 60 | } |
| 61 | |