DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

LibPortableCoroutine Test

09.08.2006
| 1342 views |
  • submit to reddit
        // A program to test libCoroutine at http://www.dekorte.com/projects/opensource/libCoroutine/

#include "Base.h"
#include "Coro.h"
#include <stdio.h>

int g_which = 0;
Coro *firstCoro, *secondCoro, *thirdCoro, *fourthCoro, *fifthCoro;

void fifthTask(void *context)
{
	int num = 0;

	printf("5th Task created with value %d\n", *(int *)context);
	while (1) {
		if (g_which != 5) {
			printf("5 != g_which %d\n", g_which); fflush(stdout);
			exit(1);
		} else printf("OK");
		printf("5th Task: %d %d\n", (int)Coro_bytesLeftOnStack(fifthCoro),  num++);
		Coro_switchTo_(fifthCoro, firstCoro);
	}
}


void fourthTask(void *context)
{
	int num = 0;

	printf("4th Task created with value %d\n", *(int *)context);
	while (1) {
		if (g_which != 4) {
			printf("4 != g_which %d\n", g_which); fflush(stdout);
			exit(1);
		} else printf("OK");
		printf("4th Task: %d %d\n", (int)Coro_bytesLeftOnStack(fourthCoro),  num++);
		Coro_switchTo_(fourthCoro, firstCoro);
	}
}



void thirdTask(void *context)
{
	int num = 0;

	printf("Third Task created with value %d\n", *(int *)context);
	while (1) {
		if (g_which != 3) {
			printf("3 != g_which %d\n", g_which); fflush(stdout);
			exit(1);
		} else printf("OK");
		printf("Third Task: %d %d\n", (int)Coro_bytesLeftOnStack(thirdCoro),  num++);
		Coro_switchTo_(thirdCoro, firstCoro);
	}
}

void secondTask(void *context)
{
	int num = 0;
	
	printf("secondTask created with value %d\n", *(int *)context);
	while (1) {
		if (g_which != 2) {
			printf("2 != g_which %d\n", g_which); fflush(stdout);
			exit(1);
		} else printf("OK");
		printf("secondTask: %d %d\n", (int)Coro_bytesLeftOnStack(secondCoro),  num++);
		Coro_switchTo_(secondCoro, firstCoro);
	}
}

void firstTask(void *context)
{
	int value = 2;
	int num  = 0;
	
	secondCoro = Coro_new();
	g_which = 2;
	Coro_startCoro_(firstCoro, secondCoro, (void *)&value, secondTask);

	thirdCoro = Coro_new();
	g_which = 3;
	Coro_startCoro_(firstCoro, thirdCoro, (void *)&value, thirdTask);

	fourthCoro = Coro_new();
	g_which = 4;
	Coro_startCoro_(firstCoro, fourthCoro, (void *)&value, fourthTask);

	fifthCoro = Coro_new();
	g_which = 5;
	Coro_startCoro_(firstCoro, fifthCoro, (void *)&value, fifthTask);

	printf("firstTask created with value %d\n", *(int *)context);

	while (1) {
		printf("firstTask: %d %d\n", (int)Coro_bytesLeftOnStack(firstCoro), num++);
		switch (num % 4) {
		case 0:
			g_which = 2;
			Coro_switchTo_(firstCoro, secondCoro);
			break;
		case 1:
			g_which = 3;
			Coro_switchTo_(firstCoro, thirdCoro);
			break;
		case 2:
			g_which = 4;
			Coro_switchTo_(firstCoro, fourthCoro);
			break;
		case 3:
			g_which = 5;
			Coro_switchTo_(firstCoro, fifthCoro);
			break;
		}
	}
}

int main()
{
	Coro *mainCoro = Coro_new();
	int value = 1;
	
	Coro_initializeMainCoro(mainCoro);
	
	firstCoro = Coro_new();
	g_which = 1;
	Coro_startCoro_(mainCoro, firstCoro, (void *)&value, firstTask);

}