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