diff --git a/src/machinarium_private.h b/src/machinarium_private.h index 4c2fe7a9..f94e8488 100644 --- a/src/machinarium_private.h +++ b/src/machinarium_private.h @@ -29,6 +29,8 @@ #include #include +#include +#include #include #include diff --git a/src/mm_call.c b/src/mm_call.c index c2327aac..77705f38 100644 --- a/src/mm_call.c +++ b/src/mm_call.c @@ -37,8 +37,6 @@ void mm_call(mm_call_t *call, int time_ms) mm_fiber_t *fiber; fiber = mm_scheduler_current(scheduler); - if (mm_fiber_is_cancelled(fiber)) - return; fiber->call_ptr = call; call->fiber = fiber; @@ -47,6 +45,15 @@ void mm_call(mm_call_t *call, int time_ms) call->arg = call; call->timedout = 0; call->status = 0; + if (mm_fiber_is_cancelled(fiber)) { + call->status = ECANCELED; + call->timedout = 0; + call->active = 0; + call->cancel_function = NULL; + call->arg = NULL; + fiber->call_ptr = NULL; + return; + } mm_timer_start(clock, &call->timer, mm_call_timer_cb, call, time_ms); diff --git a/src/mm_channel.h b/src/mm_channel.h index 0719a49b..59181cf0 100644 --- a/src/mm_channel.h +++ b/src/mm_channel.h @@ -78,7 +78,7 @@ mm_channel_read(mm_channel_t *channel, int time_ms) fetch: if (channel->incoming_count > 0) { mm_list_t *first; - first = mm_list_pop(&channel->readers); + first = mm_list_pop(&channel->incoming); channel->incoming_count--; mm_msg_t *msg; msg = mm_container_of(first, mm_msg_t, link); diff --git a/tests/machinarium_test.c b/tests/machinarium_test.c index 6138a216..c2678955 100644 --- a/tests/machinarium_test.c +++ b/tests/machinarium_test.c @@ -21,6 +21,13 @@ extern void test_join(void); extern void test_condition0(void); extern void test_condition1(void); +extern void test_channel_create(void); +extern void test_channel_rw0(void); +extern void test_channel_rw1(void); +extern void test_channel_rw2(void); +extern void test_channel_timeout(void); +extern void test_channel_cancel(void); + extern void test_io_new(void); extern void test_connect(void); extern void test_connect_timeout(void); @@ -50,6 +57,12 @@ main(int argc, char *argv[]) machinarium_test(test_join); machinarium_test(test_condition0); machinarium_test(test_condition1); + machinarium_test(test_channel_create); + machinarium_test(test_channel_rw0); + machinarium_test(test_channel_rw1); + machinarium_test(test_channel_rw2); + machinarium_test(test_channel_timeout); + machinarium_test(test_channel_cancel); machinarium_test(test_io_new); machinarium_test(test_connect); machinarium_test(test_connect_timeout); diff --git a/tests/makefile b/tests/makefile index 64943289..905223c8 100644 --- a/tests/makefile +++ b/tests/makefile @@ -15,6 +15,12 @@ OBJECTS = machinarium_test.o \ test_join.o \ test_condition0.o \ test_condition1.o \ + test_channel_create.o \ + test_channel_rw0.o \ + test_channel_rw1.o \ + test_channel_rw2.o \ + test_channel_timeout.o \ + test_channel_cancel.o \ test_io_new.o \ test_connect.o \ test_connect_timeout.o \ diff --git a/tests/test_channel_cancel.c b/tests/test_channel_cancel.c new file mode 100644 index 00000000..1937dd9b --- /dev/null +++ b/tests/test_channel_cancel.c @@ -0,0 +1,51 @@ + +/* + * machinarium. + * + * Cooperative multitasking engine. +*/ + +#include +#include + +static machine_channel_t channel; + +static void +test_fiber2(void *arg) +{ + machine_msg_t msg; + msg = machine_channel_read(channel, UINT_MAX); + test(msg == NULL); + test(machine_cancelled()); +} + +static void +test_fiber(void *arg) +{ + channel = machine_channel_create(); + test(channel != NULL); + + int id; + id = machine_fiber_create(test_fiber2, NULL); + machine_sleep(0); + machine_cancel(id); + machine_join(id); + + machine_channel_free(channel); +} + +void +test_channel_cancel(void) +{ + machinarium_init(); + + int id; + id = machine_create("test", test_fiber, NULL); + test(id != -1); + + int rc; + rc = machine_wait(id); + test(rc != -1); + + machinarium_free(); +} diff --git a/tests/test_channel_create.c b/tests/test_channel_create.c new file mode 100644 index 00000000..59e25b49 --- /dev/null +++ b/tests/test_channel_create.c @@ -0,0 +1,34 @@ + +/* + * machinarium. + * + * Cooperative multitasking engine. +*/ + +#include +#include + +static void +test_fiber(void *arg) +{ + machine_channel_t channel; + channel = machine_channel_create(); + test(channel != NULL); + machine_channel_free(channel); +} + +void +test_channel_create(void) +{ + machinarium_init(); + + int id; + id = machine_create("test", test_fiber, NULL); + test(id != -1); + + int rc; + rc = machine_wait(id); + test(rc != -1); + + machinarium_free(); +} diff --git a/tests/test_channel_rw0.c b/tests/test_channel_rw0.c new file mode 100644 index 00000000..c9bc7d3d --- /dev/null +++ b/tests/test_channel_rw0.c @@ -0,0 +1,48 @@ + +/* + * machinarium. + * + * Cooperative multitasking engine. +*/ + +#include +#include + +static void +test_fiber(void *arg) +{ + machine_channel_t channel; + channel = machine_channel_create(); + + machine_msg_t msg; + msg = machine_msg_create(123); + test(msg != NULL); + + machine_channel_write(channel, msg); + + machine_msg_t msg_in; + msg_in = machine_channel_read(channel, 0); + test(msg_in != NULL); + test(msg_in == msg); + + machine_msg_free(msg); + + test(channel != NULL); + machine_channel_free(channel); +} + +void +test_channel_rw0(void) +{ + machinarium_init(); + + int id; + id = machine_create("test", test_fiber, NULL); + test(id != -1); + + int rc; + rc = machine_wait(id); + test(rc != -1); + + machinarium_free(); +} diff --git a/tests/test_channel_rw1.c b/tests/test_channel_rw1.c new file mode 100644 index 00000000..bf7635e7 --- /dev/null +++ b/tests/test_channel_rw1.c @@ -0,0 +1,66 @@ + +/* + * machinarium. + * + * Cooperative multitasking engine. +*/ + +#include +#include + +static machine_channel_t channel; + +static void +test_fiber2(void *arg) +{ + machine_msg_t msg; + msg = machine_channel_read(channel, UINT_MAX); + test(msg != NULL); + test(machine_msg_get_type(msg) == 123); + machine_msg_free(msg); + + msg = machine_msg_create(321); + machine_channel_write(channel, msg); +} + +static void +test_fiber(void *arg) +{ + channel = machine_channel_create(); + test(channel != NULL); + + int id; + id = machine_fiber_create(test_fiber2, NULL); + + machine_msg_t msg; + msg = machine_msg_create(123); + test(msg != NULL); + machine_channel_write(channel, msg); + + machine_sleep(0); + + msg = machine_channel_read(channel, UINT_MAX); + test(msg != NULL); + test(machine_msg_get_type(msg) == 321); + machine_msg_free(msg); + + machine_join(id); + + machine_channel_free(channel); +} + +void +test_channel_rw1(void) +{ + machinarium_init(); + + int id; + id = machine_create("test", test_fiber, NULL); + test(id != -1); + + int rc; + rc = machine_wait(id); + test(rc != -1); + + machinarium_free(); +} diff --git a/tests/test_channel_rw2.c b/tests/test_channel_rw2.c new file mode 100644 index 00000000..d40bca52 --- /dev/null +++ b/tests/test_channel_rw2.c @@ -0,0 +1,67 @@ + +/* + * machinarium. + * + * Cooperative multitasking engine. +*/ + +#include +#include + +static machine_channel_t channel; + +static void +test_fiber2(void *arg) +{ + machine_msg_t msg; + msg = machine_channel_read(channel, UINT_MAX); + test(msg != NULL); + test(machine_msg_get_type(msg) == 123); + machine_msg_free(msg); + + msg = machine_msg_create(321); + machine_channel_write(channel, msg); +} + +static void +test_fiber(void *arg) +{ + channel = machine_channel_create(); + test(channel != NULL); + + int id; + id = machine_fiber_create(test_fiber2, NULL); + machine_sleep(0); + + machine_msg_t msg; + msg = machine_msg_create(123); + test(msg != NULL); + machine_channel_write(channel, msg); + + machine_sleep(0); + + msg = machine_channel_read(channel, UINT_MAX); + test(msg != NULL); + test(machine_msg_get_type(msg) == 321); + machine_msg_free(msg); + + machine_join(id); + + machine_channel_free(channel); +} + +void +test_channel_rw2(void) +{ + machinarium_init(); + + int id; + id = machine_create("test", test_fiber, NULL); + test(id != -1); + + int rc; + rc = machine_wait(id); + test(rc != -1); + + machinarium_free(); +} diff --git a/tests/test_channel_timeout.c b/tests/test_channel_timeout.c new file mode 100644 index 00000000..47747145 --- /dev/null +++ b/tests/test_channel_timeout.c @@ -0,0 +1,39 @@ + +/* + * machinarium. + * + * Cooperative multitasking engine. +*/ + +#include +#include + +static void +test_fiber(void *arg) +{ + machine_channel_t channel; + channel = machine_channel_create(); + + machine_msg_t msg; + msg = machine_channel_read(channel, 100); + test(msg == NULL); + + test(channel != NULL); + machine_channel_free(channel); +} + +void +test_channel_timeout(void) +{ + machinarium_init(); + + int id; + id = machine_create("test", test_fiber, NULL); + test(id != -1); + + int rc; + rc = machine_wait(id); + test(rc != -1); + + machinarium_free(); +}