Coder Social home page Coder Social logo

c_coroutines's Introduction

Description

The algorithm is a do-while loop until no more coroutines are not done yet it is a round-robin scheduler. Duff's device is used to inline the do-while and the for loop within the switch cases, so if longjmp 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						
*/
struct Coroutine {
	jmp_buf thread;
	void (*ptrFun)();
	int started;
	int done;
	int num_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

void coroutine_function() {
	int i = 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)

void coroutine_function() {
	static int i = 0;

	COROUTINE_START
	for (; 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.c
void callbackFib(long *f) {
	printf("callbackFib:\t\t%ld\n", *f);
}

int main() {
	initializeCoroutines();
	.
	.
	.
	
	void* args4[] = {
		COROUTINE_ARG 0,			// i
		COROUTINE_ARG 0,			// a
		COROUTINE_ARG 1,			// b
		COROUTINE_ARG 20, 			// size
		COROUTINE_ARG &callbackFib,		// callback function
	};

	addCoroutine(&fib, args4);
	startCoroutines();
	.
	.
	.
// in functions.c
// fib() is the coroutine function
void fib() {
	long *i;
	long *a;
	long *b;
	long c;
	long size;
	void (*ptrCallBack)(long*);

	COROUTINE_LOAD_ARGS
	i = (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_ARGS
	if (*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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.