odyssey/benchmark/benchmark_queue.c

70 lines
1.1 KiB
C
Raw Normal View History

2017-05-19 12:59:05 +00:00
/*
* machinarium.
*
* Cooperative multitasking engine.
*/
#include <machinarium.h>
static int ops = 0;
static void
benchmark_reader(void *arg)
{
2017-06-06 14:32:09 +00:00
machine_queue_t q = arg;
2017-05-19 12:59:05 +00:00
while (machine_active()) {
machine_msg_t msg;
2017-06-06 14:32:09 +00:00
msg = machine_queue_get(q, UINT32_MAX);
2017-05-19 12:59:05 +00:00
if (msg)
machine_msg_free(msg);
ops++;
}
}
static void
benchmark_writer(void *arg)
{
2017-06-06 14:32:09 +00:00
machine_queue_t q = arg;
2017-05-19 12:59:05 +00:00
while (machine_active()) {
machine_msg_t msg;
msg = machine_msg_create(0, 0);
2017-06-06 14:32:09 +00:00
machine_queue_put(q, msg);
2017-05-19 12:59:05 +00:00
ops++;
machine_sleep(0);
}
}
static void
benchmark_runner(void *arg)
{
printf("benchmark started.\n");
2017-06-06 14:32:09 +00:00
machine_queue_t q;
q = machine_queue_create();
2017-05-19 12:59:05 +00:00
2017-06-06 14:32:09 +00:00
int r = machine_coroutine_create(benchmark_reader, q);
int w = machine_coroutine_create(benchmark_writer, q);
2017-05-19 12:59:05 +00:00
machine_sleep(1000);
machine_stop();
machine_cancel(r);
machine_join(r);
machine_join(w);
2017-06-06 14:32:09 +00:00
machine_queue_free(q);
2017-05-19 12:59:05 +00:00
printf("done.\n");
2017-06-06 14:32:09 +00:00
printf("queue operations %d in 1 sec.\n", ops);
2017-05-19 12:59:05 +00:00
}
int
main(int argc, char *argv[])
{
machinarium_init();
2017-06-06 14:32:09 +00:00
int id = machine_create("benchmark_queue", benchmark_runner, NULL);
2017-05-19 12:59:05 +00:00
machine_wait(id);
machinarium_free();
return 0;
}