This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch tymux-12-session-parity
in repository terminology.
View the commit online.
commit 3ad799fcdc0b84901cb1d075fcd101fb387055d8
Author: Cedric BAIL <[email protected]>
AuthorDate: Sun Nov 22 19:46:55 2026 -0600
termsrv: add a table of session records in shared memory
Clients of a session need state that is not in its cells: the targets
of hyperlinks and the definitions of media blocks. Like the screen and
the scrollback, it belongs in memory they map rather than in a stream
every client has to be sent, and a client attaching late then needs no
snapshot.
The table follows the cells in the screen memfd, which stays sparse.
Each id has a slot guarded by its own seqlock, and readers treat what
they map as untrusted. Space comes from a buddy allocator whose state
is private to the daemon, so that a burst of long records cannot leave
the arena unusable for shorter ones. The layout changes, so the
protocol version goes to 2.
---
src/bin/meson.build | 5 +-
src/bin/termsrv.h | 12 +-
src/bin/termsrv_table.c | 702 ++++++++++++++++++++++++++++++++++++++++++++++++
src/bin/termsrv_table.h | 59 ++++
src/bin/tytest.c | 2 +
src/bin/unit_tests.h | 2 +
6 files changed, 779 insertions(+), 3 deletions(-)
diff --git a/src/bin/meson.build b/src/bin/meson.build
index 653c1ff8..3bc551fd 100644
--- a/src/bin/meson.build
+++ b/src/bin/meson.build
@@ -79,6 +79,7 @@ terminology_sources = ['private.h',
if host_os == 'linux'
terminology_sources += ['termsrv.c', 'termsrv.h',
+ 'termsrv_table.c', 'termsrv_table.h',
'termtymux.c', 'termtymux.h']
endif
@@ -182,7 +183,8 @@ tybench_sources = [color_names,
'tybench.c']
if host_os == 'linux'
- tytest_sources += ['termsrv.c', 'termsrv.h', 'termtymux.c', 'termtymux.h',
+ tytest_sources += ['termsrv.c', 'termsrv.h',
+ 'termsrv_table.c', 'termsrv_table.h', 'termtymux.c', 'termtymux.h',
'tytest_tymux.c', 'tytest_tymux.h']
endif
@@ -237,6 +239,7 @@ if host_os == 'linux'
'backlog_compress.c', 'backlog_compress.h',
'backlog_view.c', 'backlog_view.h',
'termsrv.c', 'termsrv.h',
+ 'termsrv_table.c', 'termsrv_table.h',
'termpty.c', 'termpty.h',
'termptyblock.c', 'termptyblock.h',
'termptydbl.c', 'termptydbl.h',
diff --git a/src/bin/termsrv.h b/src/bin/termsrv.h
index d1ab3514..49c1ec10 100644
--- a/src/bin/termsrv.h
+++ b/src/bin/termsrv.h
@@ -15,9 +15,10 @@
#include <sys/un.h>
#include <stdatomic.h>
#include <Eina.h>
+#include "termsrv_table.h"
#define TERMSRV_SHM_MAGIC 0x544D5348u /* "TMSH" */
-#define TERMSRV_SHM_VERSION 1u
+#define TERMSRV_SHM_VERSION 2u
/* An 8K panel (7680x4320) with the smallest bundled font, 4x6, holds 1920
* columns in landscape and 7680 / 6 = 1280 rows in portrait. Only the cells
* of the current size are ever touched, so the rest of the screen memory
@@ -89,11 +90,18 @@ typedef struct
} TermSrvCell;
#define TERMSRV_SHM_HEADER_SIZE 4096u
-#define TERMSRV_SHM_TOTAL_SIZE \
+#define TERMSRV_SHM_CELLS_END \
(TERMSRV_SHM_HEADER_SIZE + \
(size_t)TERMSRV_MAX_COLS * TERMSRV_MAX_ROWS * sizeof(TermSrvCell))
+/* The session table follows the cells, on a page boundary. */
+#define TERMSRV_SHM_TABLE_OFFSET \
+ ((TERMSRV_SHM_CELLS_END + 4095u) & ~(size_t)4095u)
+#define TERMSRV_SHM_TOTAL_SIZE \
+ (TERMSRV_SHM_TABLE_OFFSET + TERMSRV_TABLE_SIZE)
#define TERMSRV_SHM_CELLS(shm) \
((TermSrvCell *)((uint8_t *)(shm) + TERMSRV_SHM_HEADER_SIZE))
+#define TERMSRV_SHM_TABLE(shm) \
+ ((void *)((uint8_t *)(shm) + TERMSRV_SHM_TABLE_OFFSET))
/* A backlog page is a memfd: a header_size header, then the cells of its
* entries packed with no gap, entry_off[] in cells. The header is the
diff --git a/src/bin/termsrv_table.c b/src/bin/termsrv_table.c
new file mode 100644
index 00000000..089b6cef
--- /dev/null
+++ b/src/bin/termsrv_table.c
@@ -0,0 +1,702 @@
+#include "private.h"
+#include <sched.h>
+#include <stdatomic.h>
+#include <stdlib.h>
+#include <string.h>
+#include <Eina.h>
+#include "termsrv_table.h"
+
+#define SLOT_COUNT (TERMSRV_TABLE_LINKS + TERMSRV_TABLE_BLOCKS)
+#define BLOCK_SHIFT_MIN 6u
+#define BLOCK_SHIFT_MAX 20u
+#define BLOCK_CLASSES (BLOCK_SHIFT_MAX - BLOCK_SHIFT_MIN + 1u)
+#define READ_ATTEMPTS 100
+
+/* generation 0 is a slot with no entry */
+typedef struct
+{
+ _Atomic uint32_t seq;
+ _Atomic uint32_t generation;
+ _Atomic uint32_t offset;
+ _Atomic uint32_t length;
+ _Atomic uint64_t valid_from;
+} TermSrvTableSlot;
+
+_Static_assert(sizeof(TermSrvTableSlot) == TERMSRV_TABLE_SLOT_SIZE,
+ "the slot is part of the layout shared with clients");
+_Static_assert(((size_t)SLOT_COUNT * sizeof(TermSrvTableSlot)) % 4096 == 0,
+ "the arena starts on a page boundary");
+_Static_assert(TERMSRV_TABLE_RECORD_MAX == 1u << BLOCK_SHIFT_MAX,
+ "the largest size class holds the largest record");
+_Static_assert(TERMSRV_TABLE_ARENA_SIZE <= UINT32_MAX,
+ "arena offsets are 32 bits");
+
+/* The arena is a buddy allocator over aligned chunks of the largest size
+ * class. Per class, a bitmap holds one bit per block, set when the block is
+ * free at exactly this class; below the largest class, two buddies are never
+ * both set. The bitmaps are one calloc'd allocation: pages of small classes stay untouched until the
+ * arena is split that far.
+ *
+ * The allocator state is deliberately not in the shared memory: a client
+ * must not be able to steer what the writer reuses. A slot's current block
+ * is remembered here too, rather than read back from the table. */
+struct tag_TermSrvTableWriter
+{
+ TermSrvTableSlot *slots;
+ uint8_t *arena;
+ uint32_t *generations;
+ uint32_t *offsets;
+ uint8_t *classes; /* size class + 1 of the slot's block, 0: none */
+ uint64_t *free_bits[BLOCK_CLASSES];
+ uint32_t free_words[BLOCK_CLASSES];
+ uint32_t free_hint[BLOCK_CLASSES]; /* no free bit in words before */
+ uint32_t free_count[BLOCK_CLASSES];
+ Eina_Bool failure_logged;
+};
+
+static void
+_free_mark(TermSrvTableWriter *writer, uint32_t class, uint32_t offset)
+{
+ uint32_t block = offset >> (class + BLOCK_SHIFT_MIN);
+
+ writer->free_bits[class][block / 64] |= (uint64_t)1 << (block % 64);
+ if (block / 64 < writer->free_hint[class])
+ writer->free_hint[class] = block / 64;
+ writer->free_count[class]++;
+}
+
+size_t
+termsrv_table_size(void)
+{
+ return TERMSRV_TABLE_SIZE;
+}
+
+TermSrvTableWriter *
+termsrv_table_writer_new(void *table)
+{
+ TermSrvTableWriter *writer;
+ uint64_t *bits;
+ size_t words = 0;
+ uint32_t i, offset;
+
+ EINA_SAFETY_ON_NULL_RETURN_VAL(table, NULL);
+ writer = calloc(1, sizeof(*writer));
+ if (!writer) return NULL;
+ writer->generations = calloc(SLOT_COUNT, sizeof(uint32_t));
+ writer->offsets = calloc(SLOT_COUNT, sizeof(uint32_t));
+ writer->classes = calloc(SLOT_COUNT, sizeof(uint8_t));
+ for (i = 0; i < BLOCK_CLASSES; i++)
+ {
+ uint32_t blocks = TERMSRV_TABLE_ARENA_SIZE >> (i + BLOCK_SHIFT_MIN);
+
+ writer->free_words[i] = (blocks + 63) / 64;
+ words += writer->free_words[i];
+ }
+ bits = calloc(words, sizeof(uint64_t));
+ if (!writer->generations || !writer->offsets || !writer->classes || !bits)
+ {
+ free(bits);
+ termsrv_table_writer_free(writer);
+ return NULL;
+ }
+ for (i = 0; i < BLOCK_CLASSES; i++)
+ {
+ writer->free_bits[i] = bits;
+ bits += writer->free_words[i];
+ }
+ for (offset = 0; offset < TERMSRV_TABLE_ARENA_SIZE;
+ offset += TERMSRV_TABLE_RECORD_MAX)
+ _free_mark(writer, BLOCK_CLASSES - 1, offset);
+ writer->slots = table;
+ writer->arena = (uint8_t *)(writer->slots + SLOT_COUNT);
+ return writer;
+}
+
+void
+termsrv_table_writer_free(TermSrvTableWriter *writer)
+{
+ if (!writer) return;
+ free(writer->free_bits[0]);
+ free(writer->generations);
+ free(writer->offsets);
+ free(writer->classes);
+ free(writer);
+}
+
+static uint32_t
+_class_of(uint32_t length)
+{
+ if (length <= (1u << BLOCK_SHIFT_MIN)) return 0;
+ return (32u - (uint32_t)__builtin_clz(length - 1)) - BLOCK_SHIFT_MIN;
+}
+
+/* Takes the lowest free block of class, splitting a larger one if need be. */
+static Eina_Bool
+_block_alloc(TermSrvTableWriter *writer, uint32_t class, uint32_t *offset)
+{
+ uint32_t found = class, word, block;
+
+ while (found < BLOCK_CLASSES && !writer->free_count[found]) found++;
+ if (found == BLOCK_CLASSES) return EINA_FALSE;
+
+ word = writer->free_hint[found];
+ while (!writer->free_bits[found][word]) word++;
+ writer->free_hint[found] = word;
+ block = word * 64 + (uint32_t)__builtin_ctzll(writer->free_bits[found][word]);
+ writer->free_bits[found][word] &= ~((uint64_t)1 << (block % 64));
+ writer->free_count[found]--;
+
+ *offset = block << (found + BLOCK_SHIFT_MIN);
+ while (found > class)
+ {
+ found--;
+ _free_mark(writer, found, *offset + (1u << (found + BLOCK_SHIFT_MIN)));
+ }
+ return EINA_TRUE;
+}
+
+/* The slot no longer names the block, but a reader that read the slot before
+ * may still be copying it. The fence puts the slot update before whatever
+ * is written to the block next, so such a reader fails its final check of
+ * the slot's seq rather than return mixed bytes. */
+static void
+_block_free(TermSrvTableWriter *writer, uint32_t class, uint32_t offset)
+{
+ atomic_thread_fence(memory_order_release);
+ while (class < BLOCK_CLASSES - 1)
+ {
+ uint32_t block = offset >> (class + BLOCK_SHIFT_MIN);
+ uint32_t buddy = block ^ 1u;
+ uint64_t bit = (uint64_t)1 << (buddy % 64);
+
+ if (!(writer->free_bits[class][buddy / 64] & bit)) break;
+ writer->free_bits[class][buddy / 64] &= ~bit;
+ writer->free_count[class]--;
+ offset &= ~(1u << (class + BLOCK_SHIFT_MIN));
+ class++;
+ }
+ _free_mark(writer, class, offset);
+}
+
+static void
+_slot_write(TermSrvTableSlot *slot, uint32_t generation, uint32_t offset,
+ uint32_t length, uint64_t valid_from)
+{
+ uint32_t seq = atomic_load_explicit(&slot->seq, memory_order_relaxed);
+
+ atomic_store_explicit(&slot->seq, seq + 1, memory_order_relaxed);
+ /* readers seeing any of the new values see seq odd */
+ atomic_thread_fence(memory_order_release);
+ atomic_store_explicit(&slot->generation, generation, memory_order_relaxed);
+ atomic_store_explicit(&slot->offset, offset, memory_order_relaxed);
+ atomic_store_explicit(&slot->length, length, memory_order_relaxed);
+ atomic_store_explicit(&slot->valid_from, valid_from, memory_order_relaxed);
+ /* readers seeing the even value see all the new values */
+ atomic_store_explicit(&slot->seq, seq + 2, memory_order_release);
+}
+
+static Eina_Bool
+_slot_index(TermSrvTableKind kind, uint32_t id, size_t *index)
+{
+ if (kind == TERMSRV_TABLE_LINK && id < TERMSRV_TABLE_LINKS)
+ *index = id;
+ else if (kind == TERMSRV_TABLE_BLOCK && id < TERMSRV_TABLE_BLOCKS)
+ *index = (size_t)TERMSRV_TABLE_LINKS + id;
+ else
+ return EINA_FALSE;
+ return EINA_TRUE;
+}
+
+static void
+_unpublish_index(TermSrvTableWriter *writer, size_t index)
+{
+ uint8_t class_plus_one = writer->classes[index];
+
+ if (!class_plus_one) return;
+ _slot_write(&writer->slots[index], 0, 0, 0, 0);
+ writer->classes[index] = 0;
+ _block_free(writer, class_plus_one - 1u, writer->offsets[index]);
+}
+
+void
+termsrv_table_unpublish(TermSrvTableWriter *writer, TermSrvTableKind kind,
+ uint32_t id)
+{
+ size_t index;
+
+ EINA_SAFETY_ON_NULL_RETURN(writer);
+ if (_slot_index(kind, id, &index))
+ _unpublish_index(writer, index);
+}
+
+Eina_Bool
+termsrv_table_publish(TermSrvTableWriter *writer, TermSrvTableKind kind,
+ uint32_t id, const void *record, uint32_t length,
+ uint64_t valid_from)
+{
+ uint32_t class, offset, generation;
+ uint8_t old_class_plus_one;
+ uint32_t old_offset;
+ size_t index;
+
+ EINA_SAFETY_ON_NULL_RETURN_VAL(writer, EINA_FALSE);
+ if (!_slot_index(kind, id, &index))
+ {
+ if (!writer->failure_logged)
+ ERR("session table: id %u of kind %d is out of range", id, kind);
+ writer->failure_logged = EINA_TRUE;
+ return EINA_FALSE;
+ }
+ if (length > TERMSRV_TABLE_RECORD_MAX ||
+ !_block_alloc(writer, class = _class_of(length), &offset))
+ {
+ _unpublish_index(writer, index);
+ if (!writer->failure_logged)
+ ERR("session table: no room for a record of %u bytes", length);
+ writer->failure_logged = EINA_TRUE;
+ return EINA_FALSE;
+ }
+ if (length) memcpy(writer->arena + offset, record, length);
+
+ generation = writer->generations[index] + 1;
+ if (!generation) generation = 1;
+ old_class_plus_one = writer->classes[index];
+ old_offset = writer->offsets[index];
+ _slot_write(&writer->slots[index], generation, offset, length, valid_from);
+ writer->generations[index] = generation;
+ writer->offsets[index] = offset;
+ writer->classes[index] = (uint8_t)(class + 1);
+ if (old_class_plus_one)
+ _block_free(writer, old_class_plus_one - 1u, old_offset);
+ return EINA_TRUE;
+}
+
+int
+termsrv_table_read(const void *table, TermSrvTableKind kind, uint32_t id,
+ uint64_t shown_seq, uint32_t *generation,
+ void **record, uint32_t *capacity, uint32_t *length)
+{
+ const TermSrvTableSlot *slots = table;
+ const uint8_t *arena = (const uint8_t *)(slots + SLOT_COUNT);
+ const TermSrvTableSlot *slot;
+ size_t index;
+ int attempt;
+
+ *length = 0;
+ *generation = 0;
+ if (!_slot_index(kind, id, &index)) return 0;
+ slot = &slots[index];
+
+ for (attempt = 0; attempt < READ_ATTEMPTS; attempt++)
+ {
+ uint32_t seq, gen, offset, len;
+ uint64_t valid_from;
+ int result = 1;
+
+ seq = atomic_load_explicit(&slot->seq, memory_order_acquire);
+ if (seq & 1)
+ {
+ sched_yield();
+ continue;
+ }
+ gen = atomic_load_explicit(&slot->generation, memory_order_relaxed);
+ offset = atomic_load_explicit(&slot->offset, memory_order_relaxed);
+ len = atomic_load_explicit(&slot->length, memory_order_relaxed);
+ valid_from = atomic_load_explicit(&slot->valid_from,
+ memory_order_relaxed);
+ if (!gen || valid_from > shown_seq)
+ result = 0;
+ else if (len > TERMSRV_TABLE_RECORD_MAX ||
+ offset > TERMSRV_TABLE_ARENA_SIZE ||
+ len > TERMSRV_TABLE_ARENA_SIZE - offset)
+ result = -1;
+ else
+ {
+ if (*capacity < len)
+ {
+ void *grown = realloc(*record, len);
+
+ if (!grown) return -1;
+ *record = grown;
+ *capacity = len;
+ }
+ if (len) memcpy(*record, arena + offset, len);
+ }
+ /* the reads above are done before seq is checked again */
+ atomic_thread_fence(memory_order_acquire);
+ if (atomic_load_explicit(&slot->seq, memory_order_relaxed) != seq)
+ continue;
+ if (result == 1)
+ {
+ *generation = gen;
+ *length = len;
+ }
+ return result;
+ }
+ return -1;
+}
+
+#if defined(BINARY_TYTEST)
+#include <assert.h>
+#include <pthread.h>
+
+typedef struct
+{
+ void *table;
+ TermSrvTableWriter *writer;
+} Table_Fixture;
+
+static void
+_fixture_init(Table_Fixture *fx)
+{
+ fx->table = calloc(1, TERMSRV_TABLE_SIZE);
+ assert(fx->table);
+ fx->writer = termsrv_table_writer_new(fx->table);
+ assert(fx->writer);
+}
+
+static void
+_fixture_shutdown(Table_Fixture *fx)
+{
+ termsrv_table_writer_free(fx->writer);
+ free(fx->table);
+}
+
+static void
+_corrupt_link9(void *table, uint32_t offset, uint32_t length)
+{
+ TermSrvTableSlot *slot = (TermSrvTableSlot *)table + 9;
+
+ atomic_store(&slot->offset, offset);
+ atomic_store(&slot->length, length);
+}
+
+/* Every 1 MiB chunk of an empty arena must be whole again. */
+static void
+_assert_arena_whole(Table_Fixture *fx)
+{
+ static uint8_t big[TERMSRV_TABLE_RECORD_MAX];
+ uint32_t i, chunks = TERMSRV_TABLE_ARENA_SIZE / TERMSRV_TABLE_RECORD_MAX;
+
+ for (i = 0; i < chunks; i++)
+ assert(termsrv_table_publish(fx->writer, TERMSRV_TABLE_BLOCK, i, big,
+ sizeof(big), 0));
+ assert(!termsrv_table_publish(fx->writer, TERMSRV_TABLE_BLOCK, chunks, big,
+ 64, 0));
+ for (i = 0; i < chunks; i++)
+ termsrv_table_unpublish(fx->writer, TERMSRV_TABLE_BLOCK, i);
+}
+
+static void
+_assert_small_after_large_burst(void)
+{
+ Table_Fixture fx;
+ static uint8_t rec[3000];
+ uint32_t i, n = TERMSRV_TABLE_ARENA_SIZE / 4096;
+
+ _fixture_init(&fx);
+ for (i = 0; i < n; i++)
+ assert(termsrv_table_publish(fx.writer, TERMSRV_TABLE_LINK, i, rec,
+ sizeof(rec), 0));
+ for (i = 0; i < n; i++)
+ termsrv_table_unpublish(fx.writer, TERMSRV_TABLE_LINK, i);
+ assert(termsrv_table_publish(fx.writer, TERMSRV_TABLE_LINK, 0, rec, 40, 0));
+ termsrv_table_unpublish(fx.writer, TERMSRV_TABLE_LINK, 0);
+ _assert_arena_whole(&fx);
+ _fixture_shutdown(&fx);
+}
+
+static void
+_assert_large_after_small_burst(void)
+{
+ Table_Fixture fx;
+ static uint8_t rec[512];
+ uint32_t i;
+
+ _fixture_init(&fx);
+ for (i = 0; i < TERMSRV_TABLE_LINKS; i++)
+ assert(termsrv_table_publish(fx.writer, TERMSRV_TABLE_LINK, i, rec,
+ sizeof(rec), 0));
+ for (i = 0; i < TERMSRV_TABLE_BLOCKS; i++)
+ assert(termsrv_table_publish(fx.writer, TERMSRV_TABLE_BLOCK, i, rec,
+ sizeof(rec), 0));
+ for (i = 0; i < TERMSRV_TABLE_LINKS; i++)
+ termsrv_table_unpublish(fx.writer, TERMSRV_TABLE_LINK, i);
+ for (i = 0; i < TERMSRV_TABLE_BLOCKS; i++)
+ termsrv_table_unpublish(fx.writer, TERMSRV_TABLE_BLOCK, i);
+ _assert_arena_whole(&fx);
+ _fixture_shutdown(&fx);
+}
+
+#define RANDOM_IDS 400
+
+static uint32_t
+_xorshift(uint32_t *state)
+{
+ *state ^= *state << 13;
+ *state ^= *state >> 17;
+ *state ^= *state << 5;
+ return *state;
+}
+
+static void
+_assert_random_publish_unpublish(void)
+{
+ Table_Fixture fx;
+ uint32_t state = 0x2545F491u, length_of[RANDOM_IDS] = { 0 };
+ uint8_t byte_of[RANDOM_IDS] = { 0 };
+ uint8_t *rec = malloc(TERMSRV_TABLE_RECORD_MAX), *out = NULL;
+ uint32_t cap = 0, len, gen, op, id;
+
+ assert(rec);
+ _fixture_init(&fx);
+ for (op = 0; op < 200000; op++)
+ {
+ uint32_t r = _xorshift(&state);
+
+ id = r % RANDOM_IDS;
+ if ((r >> 12) % 3 == 0)
+ {
+ termsrv_table_unpublish(fx.writer, TERMSRV_TABLE_LINK, id);
+ length_of[id] = 0;
+ }
+ else
+ {
+ uint32_t shift = (r >> 16) % 21; /* 1 B to 1 MiB */
+ uint32_t length = 1 + (_xorshift(&state) % (1u << shift));
+
+ memset(rec, (int)op | 1, length);
+ if (termsrv_table_publish(fx.writer, TERMSRV_TABLE_LINK, id, rec,
+ length, 0))
+ {
+ length_of[id] = length;
+ byte_of[id] = (uint8_t)(op | 1);
+ }
+ else
+ length_of[id] = 0;
+ }
+ id = _xorshift(&state) % RANDOM_IDS;
+ if (length_of[id])
+ {
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_LINK, id, 0,
+ &gen, (void **)&out, &cap, &len) == 1);
+ assert(len == length_of[id] && out[0] == byte_of[id] &&
+ out[len - 1] == byte_of[id]);
+ }
+ }
+ for (id = 0; id < RANDOM_IDS; id++)
+ {
+ uint32_t i;
+
+ if (!length_of[id])
+ {
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_LINK, id, 0,
+ &gen, (void **)&out, &cap, &len) == 0);
+ continue;
+ }
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_LINK, id, 0, &gen,
+ (void **)&out, &cap, &len) == 1);
+ assert(len == length_of[id]);
+ for (i = 0; i < len; i++) assert(out[i] == byte_of[id]);
+ termsrv_table_unpublish(fx.writer, TERMSRV_TABLE_LINK, id);
+ }
+ _assert_arena_whole(&fx);
+ free(out);
+ free(rec);
+ _fixture_shutdown(&fx);
+}
+
+int
+tytest_termsrv_table(void)
+{
+ Table_Fixture fx;
+ uint8_t rec[300], *out = NULL;
+ uint32_t cap = 0, len = 0, gen = 0, gen2 = 0, i;
+ int count;
+
+ _fixture_init(&fx);
+ assert(termsrv_table_size() == TERMSRV_TABLE_SIZE);
+ for (i = 0; i < sizeof(rec); i++) rec[i] = (uint8_t)i;
+
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_LINK, 5, 0, &gen,
+ (void **)&out, &cap, &len) == 0);
+
+ assert(termsrv_table_publish(fx.writer, TERMSRV_TABLE_LINK, 5, rec,
+ sizeof(rec), 0));
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_LINK, 5, 0, &gen,
+ (void **)&out, &cap, &len) == 1);
+ assert(gen != 0);
+ assert(len == sizeof(rec) && cap >= len && !memcmp(out, rec, len));
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_BLOCK, 5, 0, &gen2,
+ (void **)&out, &cap, &len) == 0);
+
+ assert(termsrv_table_publish(fx.writer, TERMSRV_TABLE_BLOCK, 7, rec, 10, 10));
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_BLOCK, 7, 8, &gen2,
+ (void **)&out, &cap, &len) == 0);
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_BLOCK, 7, 10, &gen2,
+ (void **)&out, &cap, &len) == 1);
+ assert(len == 10 && gen2 != 0);
+
+ assert(termsrv_table_publish(fx.writer, TERMSRV_TABLE_LINK, 5, rec, 20, 0));
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_LINK, 5, 0, &gen2,
+ (void **)&out, &cap, &len) == 1);
+ assert(gen2 != gen && gen2 != 0 && len == 20);
+
+ termsrv_table_unpublish(fx.writer, TERMSRV_TABLE_LINK, 5);
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_LINK, 5, 0, &gen,
+ (void **)&out, &cap, &len) == 0);
+ assert(termsrv_table_publish(fx.writer, TERMSRV_TABLE_LINK, 5, rec, 20, 0));
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_LINK, 5, 0, &gen,
+ (void **)&out, &cap, &len) == 1);
+ assert(gen != gen2);
+ termsrv_table_unpublish(fx.writer, TERMSRV_TABLE_LINK, 5);
+
+ for (i = 0; i < 100000; i++)
+ {
+ assert(termsrv_table_publish(fx.writer, TERMSRV_TABLE_LINK, 1, rec,
+ sizeof(rec), 0));
+ termsrv_table_unpublish(fx.writer, TERMSRV_TABLE_LINK, 1);
+ }
+
+ assert(termsrv_table_publish(fx.writer, TERMSRV_TABLE_LINK, 2, rec, 8, 0));
+ {
+ void *big = calloc(1, TERMSRV_TABLE_RECORD_MAX + 1);
+
+ assert(big);
+ assert(!termsrv_table_publish(fx.writer, TERMSRV_TABLE_LINK, 2, big,
+ TERMSRV_TABLE_RECORD_MAX + 1, 0));
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_LINK, 2, 0, &gen,
+ (void **)&out, &cap, &len) == 0);
+ assert(termsrv_table_publish(fx.writer, TERMSRV_TABLE_LINK, 2, big,
+ TERMSRV_TABLE_RECORD_MAX, 0));
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_LINK, 2, 0, &gen,
+ (void **)&out, &cap, &len) == 1);
+ assert(len == TERMSRV_TABLE_RECORD_MAX);
+ termsrv_table_unpublish(fx.writer, TERMSRV_TABLE_LINK, 2);
+
+ for (count = 0; count < 1000; count++)
+ if (!termsrv_table_publish(fx.writer, TERMSRV_TABLE_BLOCK, count, big,
+ TERMSRV_TABLE_RECORD_MAX, 0))
+ break;
+ assert(count > 0 && count < 1000);
+ for (i = 0; i < (uint32_t)count; i++)
+ {
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_BLOCK, i, 0, &gen,
+ (void **)&out, &cap, &len) == 1);
+ assert(len == TERMSRV_TABLE_RECORD_MAX);
+ }
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_BLOCK, count, 0, &gen,
+ (void **)&out, &cap, &len) == 0);
+ for (i = 0; i < (uint32_t)count; i++)
+ termsrv_table_unpublish(fx.writer, TERMSRV_TABLE_BLOCK, i);
+ free(big);
+ }
+
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_LINK,
+ TERMSRV_TABLE_LINKS, 0, &gen, (void **)&out, &cap,
+ &len) == 0);
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_BLOCK,
+ TERMSRV_TABLE_BLOCKS, 0, &gen, (void **)&out, &cap,
+ &len) == 0);
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_LINK, UINT32_MAX, 0, &gen,
+ (void **)&out, &cap, &len) == 0);
+ assert(!termsrv_table_publish(fx.writer, TERMSRV_TABLE_LINK,
+ TERMSRV_TABLE_LINKS, rec, 4, 0));
+
+ assert(termsrv_table_publish(fx.writer, TERMSRV_TABLE_LINK, 9, rec, 100, 0));
+ {
+ uint32_t bad[][2] = {
+ { TERMSRV_TABLE_ARENA_SIZE, 1 },
+ { TERMSRV_TABLE_ARENA_SIZE - 10, 100 },
+ { UINT32_MAX, 100 },
+ { 64, TERMSRV_TABLE_RECORD_MAX + 1 },
+ { 64, UINT32_MAX },
+ { UINT32_MAX - 3, 8 },
+ };
+
+ for (i = 0; i < sizeof(bad) / sizeof(bad[0]); i++)
+ {
+ _corrupt_link9(fx.table, bad[i][0], bad[i][1]);
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_LINK, 9, 0, &gen,
+ (void **)&out, &cap, &len) == -1);
+ }
+ }
+ free(out);
+ _fixture_shutdown(&fx);
+ _assert_small_after_large_burst();
+ _assert_large_after_small_burst();
+ _assert_random_publish_unpublish();
+ return 0;
+}
+
+typedef struct
+{
+ TermSrvTableWriter *writer;
+ _Atomic int stop;
+ _Atomic int both_published;
+} Table_Writer_Thread;
+
+/* Two ids take turns, so that a block freed by one is soon reused by the
+ * other while a reader may still be copying it. */
+static void *
+_republish_thread(void *data)
+{
+ Table_Writer_Thread *thread = data;
+ uint8_t *rec = malloc(5000);
+ uint32_t i;
+
+ assert(rec);
+ for (i = 1; !atomic_load(&thread->stop); i++)
+ {
+ uint32_t length = 1 + (i * 37) % 5000;
+
+ memset(rec, (int)(i & 0xff) | 1, length);
+ assert(termsrv_table_publish(thread->writer, TERMSRV_TABLE_LINK,
+ i & 1, rec, length, i));
+ if (i == 2) atomic_store(&thread->both_published, 1);
+ }
+ free(rec);
+ return NULL;
+}
+
+int
+tytest_termsrv_table_concurrent(void)
+{
+ Table_Fixture fx;
+ Table_Writer_Thread thread = { .stop = 0 };
+ pthread_t tid;
+ void *out = NULL;
+ uint32_t cap = 0, len, gen, last_gen[2] = { 0, 0 };
+ unsigned int n;
+
+ _fixture_init(&fx);
+ thread.writer = fx.writer;
+ assert(!pthread_create(&tid, NULL, _republish_thread, &thread));
+ while (!atomic_load(&thread.both_published)) sched_yield();
+ for (n = 0; n < 500000; n++)
+ {
+ int res = termsrv_table_read(fx.table, TERMSRV_TABLE_LINK, n & 1,
+ UINT64_MAX, &gen, &out, &cap, &len);
+
+ assert(res != 0);
+ if (res == 1)
+ {
+ const uint8_t *bytes = out;
+ uint32_t i;
+
+ assert(len >= 1 && len <= 5000 && gen != 0);
+ for (i = 1; i < len; i++) assert(bytes[i] == bytes[0]);
+ assert(gen >= last_gen[n & 1]);
+ last_gen[n & 1] = gen;
+ }
+ }
+ atomic_store(&thread.stop, 1);
+ pthread_join(tid, NULL);
+ for (n = 0; n < 2; n++)
+ assert(termsrv_table_read(fx.table, TERMSRV_TABLE_LINK, n, UINT64_MAX,
+ &gen, &out, &cap, &len) == 1);
+ free(out);
+ _fixture_shutdown(&fx);
+ return 0;
+}
+#endif
diff --git a/src/bin/termsrv_table.h b/src/bin/termsrv_table.h
new file mode 100644
index 00000000..d424a610
--- /dev/null
+++ b/src/bin/termsrv_table.h
@@ -0,0 +1,59 @@
+#ifndef TERMINOLOGY_TERMSRV_TABLE_H_
+#define TERMINOLOGY_TERMSRV_TABLE_H_ 1
+
+/* A table of variable-length records in shared memory, written by tymuxd and
+ * read by its clients: see the session table in the tymux design. Each id of
+ * a kind owns a slot guarded by its own sequence lock, and its record lives
+ * in an arena. Only the writer knows which arena space is free.
+ *
+ * The reader treats the table as untrusted input from another process. */
+
+#include <stdint.h>
+#include <stddef.h>
+#include <Eina.h>
+
+#define TERMSRV_TABLE_LINKS 65536u
+#define TERMSRV_TABLE_BLOCKS 8192u
+#define TERMSRV_TABLE_ARENA_SIZE (64u << 20)
+#define TERMSRV_TABLE_RECORD_MAX (1u << 20)
+
+/* sizeof of the slot in termsrv_table.c, checked there */
+#define TERMSRV_TABLE_SLOT_SIZE 24u
+/* the slots are a multiple of the page size, so the arena is page aligned */
+#define TERMSRV_TABLE_SIZE \
+ (((size_t)(TERMSRV_TABLE_LINKS + TERMSRV_TABLE_BLOCKS) * \
+ TERMSRV_TABLE_SLOT_SIZE) + TERMSRV_TABLE_ARENA_SIZE)
+
+typedef enum
+{
+ TERMSRV_TABLE_LINK,
+ TERMSRV_TABLE_BLOCK
+} TermSrvTableKind;
+
+typedef struct tag_TermSrvTableWriter TermSrvTableWriter;
+
+size_t termsrv_table_size(void);
+
+/* table is TERMSRV_TABLE_SIZE bytes of zeroed, writable memory. */
+TermSrvTableWriter *termsrv_table_writer_new(void *table);
+void termsrv_table_writer_free(TermSrvTableWriter *writer);
+
+/* Gives id a new meaning, valid for the frames from valid_from on. The
+ * previous record of the id stays readable until the slot is updated.
+ * EINA_FALSE when over the limit or the arena is full: the id is then
+ * unpublished and the failure logged once. */
+Eina_Bool termsrv_table_publish(TermSrvTableWriter *writer, TermSrvTableKind kind,
+ uint32_t id, const void *record, uint32_t length,
+ uint64_t valid_from);
+void termsrv_table_unpublish(TermSrvTableWriter *writer, TermSrvTableKind kind,
+ uint32_t id);
+
+/* 1: *record holds *length bytes, grown as needed. 0: no entry for a frame
+ * of shown_seq. -1: corrupt, or kept changing. *generation is never 0 for an
+ * entry. *record and *capacity are a malloc'd buffer the caller owns; the
+ * reader may realloc it. */
+int termsrv_table_read(const void *table, TermSrvTableKind kind, uint32_t id,
+ uint64_t shown_seq, uint32_t *generation,
+ void **record, uint32_t *capacity, uint32_t *length);
+
+#endif
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index cdfb7c84..556262fc 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -275,6 +275,8 @@ static struct {
{ "termsrv_msg_split", tytest_termsrv_msg_split},
{ "termsrv_msg_send_full", tytest_termsrv_msg_send_full},
{ "termsrv_shm_seqlock", tytest_termsrv_shm_seqlock},
+ { "termsrv_table", tytest_termsrv_table},
+ { "termsrv_table_concurrent", tytest_termsrv_table_concurrent},
{ "termsrv_attached", tytest_termsrv_attached},
{ "termsrv_cell_layout", tytest_termsrv_cell_layout},
{ "termsrv_backlog_page_fd", tytest_termsrv_backlog_page_fd},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 178ca770..d1b5af9d 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -215,6 +215,8 @@ int tytest_termsrv_msg_invalid(void);
int tytest_termsrv_msg_split(void);
int tytest_termsrv_msg_send_full(void);
int tytest_termsrv_shm_seqlock(void);
+int tytest_termsrv_table(void);
+int tytest_termsrv_table_concurrent(void);
int tytest_termsrv_attached(void);
int tytest_termsrv_cell_layout(void);
int tytest_termsrv_backlog_page_fd(void);
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.