2019-03-12 16:39:46 +00:00
|
|
|
|
|
|
|
#include <machinarium.h>
|
|
|
|
#include <odyssey_test.h>
|
|
|
|
|
|
|
|
static machine_channel_t *channel;
|
|
|
|
|
2020-04-02 11:00:56 +00:00
|
|
|
static int count_written = 0;
|
2020-12-28 11:22:53 +00:00
|
|
|
static int count_read = 0;
|
2019-03-12 16:39:46 +00:00
|
|
|
static int pc;
|
|
|
|
|
2020-12-28 11:22:53 +00:00
|
|
|
static void test_consumer(void *arg)
|
2019-03-12 16:39:46 +00:00
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
|
2020-04-02 11:00:56 +00:00
|
|
|
while (count_read < count_written) {
|
2019-03-12 16:39:46 +00:00
|
|
|
machine_msg_t *msg;
|
|
|
|
msg = machine_channel_read(channel, 0);
|
|
|
|
if (msg == NULL)
|
|
|
|
break;
|
|
|
|
machine_msg_free(msg);
|
|
|
|
count_read++;
|
|
|
|
machine_sleep(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 11:22:53 +00:00
|
|
|
static void test_pc(void *arg)
|
2019-03-12 16:39:46 +00:00
|
|
|
{
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (; i < 1000; i++) {
|
|
|
|
machine_msg_t *msg;
|
|
|
|
msg = machine_msg_create(0);
|
|
|
|
test(msg != NULL);
|
|
|
|
machine_msg_set_type(msg, i);
|
|
|
|
count_written++;
|
|
|
|
machine_channel_write(channel, msg);
|
|
|
|
}
|
|
|
|
|
2020-02-18 13:05:02 +00:00
|
|
|
for (i = 0; i < 100; i++) {
|
2020-04-02 11:00:56 +00:00
|
|
|
int rc = machine_coroutine_create(test_consumer, NULL);
|
|
|
|
test(rc != -1);
|
|
|
|
}
|
2019-03-12 16:39:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 11:22:53 +00:00
|
|
|
void machinarium_test_producer_consumer2(void)
|
2019-03-12 16:39:46 +00:00
|
|
|
{
|
|
|
|
machinarium_init();
|
|
|
|
|
|
|
|
channel = machine_channel_create(0);
|
|
|
|
test(channel != NULL);
|
|
|
|
|
|
|
|
pc = machine_create("producer-consumer", test_pc, NULL);
|
|
|
|
test(pc != -1);
|
|
|
|
|
|
|
|
int rc;
|
|
|
|
rc = machine_wait(pc);
|
|
|
|
test(rc != -1);
|
|
|
|
|
|
|
|
test(count_read == 1000);
|
|
|
|
test(count_written == count_read);
|
|
|
|
|
|
|
|
machine_channel_free(channel);
|
|
|
|
|
|
|
|
machinarium_free();
|
|
|
|
}
|