Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

simple library to handle coroutines in C, using setjmp and longjmp

NotificationsYou must be signed in to change notification settings

hamadmarri/C_Coroutines

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 

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;};

Features

  • Concurrency
  • Passing Arguments
  • Callback Function
  • Multiple calls (or copies) to the same function

How to compile

gcc -std=c11 main.c

Basic usage

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}

Some values to adjust

// 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

Credits

https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

by Simon Tatham

About

simple library to handle coroutines in C, using setjmp and longjmp

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp