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");
|
2017-05-18 09:50:55 +00:00
|
|
|
while (machine_active()) {
|
2017-05-16 10:10:00 +00:00
|
|
|
csw++;
|
2017-05-18 09:50:55 +00:00
|
|
|
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");
|
2017-05-18 09:50:55 +00:00
|
|
|
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);
|
2017-05-18 09:50:55 +00:00
|
|
|
machine_stop();
|
2017-05-16 10:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2017-05-18 09:50:55 +00:00
|
|
|
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;
|
|
|
|
}
|