- Notifications
You must be signed in to change notification settings - Fork1
hamadmarri/C_Coroutines
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The algorithm is a do-while loop until no more coroutines are not done yetit is a round-robin scheduler.Duff's device is used to inline the do-while and the for loop within the switch cases, so iflongjmp from children coroutines (i.e. print_numbers or print_letters) to this function, the switch will continue from case 1
I am using a struct that holds coroutines informations
/* Coroutine structure that is used to hold running coroutines jmp_buf thread: is jmp buffer that saves the jmp context of each coroutine void (*ptrFun)(): is a pointer to function, it is used at first time to call the function, after that longjmp is used int started: used to check if not started yet, call the function, if already started call longjmp int done: if the coroutine is done or not int num_of_iterations: number of iterations or "time" that is given to the coroutine before release to other coroutines (can be removed and be global if needed) the current approach is useful for varying each coroutine iterations separately void **args: generic array of (void*) type. Usage in main.c*/structCoroutine {jmp_bufthread;void (*ptrFun)();intstarted;intdone;intnum_of_iterations;void**args;};
- Concurrency
- Passing Arguments
- Callback Function
- Multiple calls (or copies) to the same function
gcc -std=c11 main.c
If you want to implement a for loop such as
voidcoroutine_function() {inti=0;for (;i<100;++i) {printf("%d ",i);}}
Then add these changes (COROUTINE_START, COROUTINE_PREEMPT, COROUTINE_END, and make sure to make variables needed to preserve their values to static variables)
voidcoroutine_function() {staticinti=0;COROUTINE_STARTfor (;i<100;++i) {printf("%d ",i);COROUTINE_PREEMPT}COROUTINE_END}
To pass arguments, you need to initialize (void*) array. The following is a fibonacci example where passing callback function along with other arguments.
// in main.cvoidcallbackFib(long*f) {printf("callbackFib:\t\t%ld\n",*f);}intmain() {initializeCoroutines();...void*args4[]= {COROUTINE_ARG0,// iCOROUTINE_ARG0,// aCOROUTINE_ARG1,// bCOROUTINE_ARG20,// sizeCOROUTINE_ARG&callbackFib,// callback function};addCoroutine(&fib,args4);startCoroutines();...
// in functions.c// fib() is the coroutine functionvoidfib() {long*i;long*a;long*b;longc;longsize;void (*ptrCallBack)(long*);COROUTINE_LOAD_ARGSi= (long*)&(here->args[0]);a= (long*)&(here->args[1]);b= (long*)&(here->args[2]);size= (long)here->args[3];ptrCallBack= (void (*)(long*)) (here->args[4]);COROUTINE_START_WITH_ARGSif (*i<size)ptrCallBack(i);if (++(*i)<size)ptrCallBack(i);for (*i=2;*i<size;++(*i)) {c=*b+*a;*a=*b;*b=c;ptrCallBack(&c);COROUTINE_PREEMPT}COROUTINE_END}
// maximum number of coroutines
#define MAX_COROUTINES 4
// number of iterations or "time" that is given to each coroutine before release to other coroutines
#define MAX_NUM_OF_ITERATIONS 5
https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
by Simon Tatham