Looks for the next message without waiting and returns (found, fatal_error, message).
foundistruewith the message when one is waiting. Read the payload atmessage.user_read_pointerformessage.user_read_bytesbytes, then callreader_done_with_message. Until then the same message is returned again.foundandfatal_errorbothfalsemean the ring is empty; poll again later (sleep, or use whatever wake-up the program has).fatal_errortruemeans the shared region is corrupt (the other process wrote over the header or the cursors); the channel cannot be used further and should be released.
name := tprint("jai-doc-%", to_nanoseconds(current_time_monotonic()));
ok, reader := reader_create_channel(name, 1024);
assert(ok);
connected, writer := writer_connect(name);
assert(connected);
// The writer reserves space, fills it in and commits it.
reserved_ok, reserved := writer_reserve_message(*writer, 5);
assert(reserved_ok);
memcpy(reserved.user_write_pointer, "hello".data, 5);
assert(writer_commit_message(*writer, reserved));
// The reader finds it, reads it in place and gives the space back.
found, fatal, message := reader_poll_for_message(*reader);
assert(found && !fatal);
assert(to_string(message.user_read_pointer, message.user_read_bytes) == "hello");
assert(reader_done_with_message(*message));
found, fatal, message = reader_poll_for_message(*reader);
assert(!found && !fatal); // empty again
assert(writer_disconnect(*writer));
assert(reader_release_channel(*reader));Doesn't run in the browser (skipped on wasm).
reader_poll_for_message :: (
channel: *Read_Channel
) -> (found: bool, fatal_error: bool, message: Read_Message)
Looks for the next message without waiting and returns (found, fatal_error, message).
foundistruewith the message when one is waiting. Read the payload atmessage.user_read_pointerformessage.user_read_bytesbytes, then callreader_done_with_message. Until then the same message is returned again.foundandfatal_errorbothfalsemean the ring is empty; poll again later (sleep, or use whatever wake-up the program has).fatal_errortruemeans the shared region is corrupt (the other process wrote over the header or the cursors); the channel cannot be used further and should be released.
Parameters
- channel: *Read_Channel
Returns
- found: bool
- fatal_error: bool
- message: Read_Message
name := tprint("jai-doc-%", to_nanoseconds(current_time_monotonic()));
ok, reader := reader_create_channel(name, 1024);
assert(ok);
connected, writer := writer_connect(name);
assert(connected);
// The writer reserves space, fills it in and commits it.
reserved_ok, reserved := writer_reserve_message(*writer, 5);
assert(reserved_ok);
memcpy(reserved.user_write_pointer, "hello".data, 5);
assert(writer_commit_message(*writer, reserved));
// The reader finds it, reads it in place and gives the space back.
found, fatal, message := reader_poll_for_message(*reader);
assert(found && !fatal);
assert(to_string(message.user_read_pointer, message.user_read_bytes) == "hello");
assert(reader_done_with_message(*message));
found, fatal, message = reader_poll_for_message(*reader);
assert(!found && !fatal); // empty again
assert(writer_disconnect(*writer));
assert(reader_release_channel(*reader));Doesn't run in the browser (skipped on wasm).