Shared_Memory_Channel module

reader_create_channel

procedure · 1 overloadSource

Creates the channel called name with a region of bytes bytes and returns it as (success, channel). The caller becomes its reader and owns the name: nobody can connect until it exists, and the name goes away with reader_release_channel.

It fails if the name is taken (only one reader per name), the size is too small, or the system refuses; nothing is left behind then. Uses context.allocator for a small record that reader_release_channel frees.

Example
name := tprint("jai-doc-%", to_nanoseconds(current_time_monotonic()));

ok, reader := reader_create_channel(name, 1024);
assert(ok);
assert(reader.header.data_size_in_bytes == 768);

taken, again := reader_create_channel(name, 1024);
assert(!taken); // the name belongs to the first reader

assert(reader_release_channel(*reader));

Doesn't run in the browser (skipped on wasm).

reader_create_channel :: (
    name: string,
    bytes: s64
) -> (success: bool, channel: Read_Channel)

Creates the channel called name with a region of bytes bytes and returns it as (success, channel). The caller becomes its reader and owns the name: nobody can connect until it exists, and the name goes away with reader_release_channel.

  • bytes is the size of the whole region, including a 256-byte header; the message ring gets bytes - 256 rounded down to a multiple of 8. The least allowed is 288, which leaves room for a 16-byte message. Pick a size for the biggest message plus some room for the writer to run ahead: a message needs its payload plus 16 bytes, rounded up to 8, and never wraps.
  • name is a short non-empty text. On Unix targets it names a POSIX shared-memory object: one leading / is allowed and added if missing, no other /, and the whole name with the slash is at most 31 bytes on macOS. On Windows it names a mapping object. No NUL bytes.

It fails if the name is taken (only one reader per name), the size is too small, or the system refuses; nothing is left behind then. Uses context.allocator for a small record that reader_release_channel frees.

Parameters

name: string
bytes: s64

Returns

success: bool
channel: Read_Channel
Example
name := tprint("jai-doc-%", to_nanoseconds(current_time_monotonic()));

ok, reader := reader_create_channel(name, 1024);
assert(ok);
assert(reader.header.data_size_in_bytes == 768);

taken, again := reader_create_channel(name, 1024);
assert(!taken); // the name belongs to the first reader

assert(reader_release_channel(*reader));

Doesn't run in the browser (skipped on wasm).

See also