Shared_Memory_Channel module

reader_poll_for_message

procedure · 1 overloadSource

Looks for the next message without waiting and returns (found, fatal_error, message).

Example
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).

  • found is true with the message when one is waiting. Read the payload at message.user_read_pointer for message.user_read_bytes bytes, then call reader_done_with_message. Until then the same message is returned again.
  • found and fatal_error both false mean the ring is empty; poll again later (sleep, or use whatever wake-up the program has).
  • fatal_error true means 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
Example
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).

See also