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)
|
|
|
|
{
|
2018-02-05 12:49:23 +00:00
|
|
|
machine_channel_t *q = arg;
|
2017-05-19 12:59:05 +00:00
|
|
|
while (machine_active()) {
|
2018-01-22 11:39:16 +00:00
|
|
|
machine_msg_t *msg;
|
2018-02-05 12:49:23 +00:00
|
|
|
msg = machine_channel_read(q, UINT32_MAX);
|
2017-05-19 12:59:05 +00:00
|
|
|
if (msg)
|
|
|
|
machine_msg_free(msg);
|
|
|
|
ops++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
benchmark_writer(void *arg)
|
|
|
|
{
|
2018-02-05 12:49:23 +00:00
|
|
|
machine_channel_t *q = arg;
|
2017-05-19 12:59:05 +00:00
|
|
|
while (machine_active()) {
|
2018-01-22 11:39:16 +00:00
|
|
|
machine_msg_t *msg;
|
2017-05-23 11:54:19 +00:00
|
|
|
msg = machine_msg_create(0, 0);
|
2018-02-05 12:49:23 +00:00
|
|
|
machine_channel_write(q, msg);
|
2017-05-19 12:59:05 +00:00
|
|
|
ops++;
|
|
|
|
machine_sleep(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
benchmark_runner(void *arg)
|
|
|
|
{
|
|
|
|
printf("benchmark started.\n");
|
|
|
|
|
2018-02-05 12:49:23 +00:00
|
|
|
machine_channel_t *q;
|
|
|
|
q = machine_channel_create(1);
|
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);
|
|
|
|
|
2018-02-05 12:49:23 +00:00
|
|
|
machine_channel_free(q);
|
2017-05-19 12:59:05 +00:00
|
|
|
|
|
|
|
printf("done.\n");
|
2018-02-05 12:49:23 +00:00
|
|
|
printf("channel operations %d in 1 sec.\n", ops);
|
2017-05-19 12:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
machinarium_init();
|
2018-02-05 12:49:23 +00:00
|
|
|
int id = machine_create("benchmark_channel", benchmark_runner, NULL);
|
2017-05-19 12:59:05 +00:00
|
|
|
machine_wait(id);
|
|
|
|
machinarium_free();
|
|
|
|
return 0;
|
|
|
|
}
|