Skip to content

Repository files navigation

Introduction

A stackful task/coroutine library

  • Cross platform
  • Zero configuration
  • Zero setup
  • Blazing fast
  • Memory efficient (with autogrow stacks on linux)

CLI

# build> make
# build debug> make build=Debug
# install> sudo make install
# uninstall> sudo make uninstall

API Reference

Basically the whole include file :)

// All calls return 0 on success and errno on failuretypedefstructtask_ttask_t;
typedefvoid (*task_fn)(task_t*task, void*arg);
// Create a new task with entry point and argumentinttask_create(task_t**task, task_fnentry, void*arg);
// Delete the taskinttask_delete(task_t*task);
// Yield a value to caller inttask_yield(void*value);
// Run task as a generatorinttask_next(task_t*task, void**yield);
// Post a task to current threadinttask_post(task_fnentry, void*arg);
// Suspend the current task, and resume main taskinttask_suspend();
// Resume the supended taskinttask_resume(task_t*task);

Examples

// Print fibonacci numbers#include<stdio.h>#include"task/task.h"voidfibonacci(task_t*task, void*arg)
{
intseries, first=0, second=1, next, c;
series=*(int*)arg;
for (c=0; c<series; ++c)
{
if (c <= 1)
next=c;
else
{
next=first+second;
first=second;
second=next;
}
task_yield(&next);
}
}
voidmain()
{
task_t*task;
int*next;
intseries=30;
task_create(&task, fibonacci, &series);
while (!task_next(task, (void**)&next))
{
printf("%d\r\n", *next);
}
task_delete(task);
}
// Print permutations#include<stdio.h>#include"task/task.h"staticcharseries[] = { 'a', 'b', 'c', 'd', 0 };
voidswap(char*x, char*y)
{
chartemp;
temp=*x;
*x=*y;
*y=temp;
}
voidpermute(task_t*task, void*arg)
{
task_t*permuter;
intindex=arg ? *(int*)arg : 0;
intnext=index+1;
inti;
if (series[next] ==0)
{
task_yield(0);
return;
}
for (i=index; series[i]; ++i)
{
swap(series+index, series+i);
task_create(&permuter, permute, &next);
while (!task_next(permuter, 0))
{
task_yield(0);
}
task_delete(permuter);
swap(series+index, series+i);
}
}
voidmain()
{
task_t*permuter;
task_create(&permuter, permute, 0);
while (!task_next(permuter, 0))
{
printf("%s\r\n", series);
}
task_delete(permuter);
}
// Ping pong#include<stdio.h>#include"task/task.h"voidpong(task_t*task, void*arg)
{
*((task_t**)arg) =task;
intiteration=1;
while (1)
{
task_suspend();
printf("pong %d\r\n", iteration++);
}
}
voidmain()
{
intiteration=0;
task_t*ponger;
task_post(pong, &ponger);
while (iteration++<30)
{
printf("ping %d\r\n", iteration);
task_resume(ponger);
}
}

Platforms

Tested on

  • Win64 (32/64 bit build)
  • Ubuntu64 (64 bit build)

ToDos

  • Test on all platforms with different architectures
  • Compare performance with other libraries

Contacts

Email:artak.khnkoyan@gmail.com

Licence

MIT

About

Cross platform stackful task/coroutine library

Topics

Resources

Stars

11 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages