odyssey/examples/benchmark.c

48 lines
770 B
C
Raw Normal View History

2017-05-16 10:10:00 +00:00
/*
* machinarium.
*
* Cooperative multitasking engine.
*/
/*
* This example shows fiber context switch (yield)
* performance done in one second.
*/
#include <machinarium.h>
static int csw = 0;
static void
benchmark_worker(void *arg)
{
printf("worker started.\n");
while (machine_active()) {
2017-05-16 10:10:00 +00:00
csw++;
machine_sleep(0);
2017-05-16 10:10:00 +00:00
}
printf("worker done.\n");
}
static void
benchmark_runner(void *arg)
{
printf("benchmark started.\n");
machine_create_fiber(benchmark_worker, NULL);
machine_sleep(1000);
2017-05-16 10:10:00 +00:00
printf("done.\n");
printf("context switches %d in 1 sec.\n", csw);
machine_stop();
2017-05-16 10:10:00 +00:00
}
int
main(int argc, char *argv[])
{
machinarium_init();
int id = machine_create("benchmark", benchmark_runner, NULL);
machine_join(id);
machinarium_free();
2017-05-16 10:10:00 +00:00
return 0;
}