odyssey/third_party/machinarium/benchmark/benchmark_csw.c

48 lines
782 B
C
Raw Normal View History

2017-05-16 10:10:00 +00:00
/*
* machinarium.
*
* Cooperative multitasking engine.
*/
/*
* This example shows coroutine context switch (yield)
2017-05-16 10:10:00 +00:00
* 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_coroutine_create(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();
2017-05-19 12:59:05 +00:00
int id = machine_create("benchmark_csw", benchmark_runner, NULL);
2017-05-18 10:20:58 +00:00
machine_wait(id);
machinarium_free();
2017-05-16 10:10:00 +00:00
return 0;
}