Artifact 639833a9beb8e4ec82cdd139180db899a60f7dd8b925377e1d6bc8c8728f762a:
- File
src/platform.h
— part of check-in
[eae917e9e9]
at
2026-11-21 12:03:36
on branch shared-store
— Shared journal store: several pic processes on one workspace, one conversation each
Each conversation has a lease (an exclusive flock beside the journal) that its process holds while it advances it and that the kernel drops when the process and its forked children are gone. Taking a lease claims the conversation's request and settles what a dead owner left; open recovers only conversations whose lease is free. The journal lock is held shared and taken exclusive only to create or migrate the schema. SQLite runs in normal locking mode with the fair busy handler; dev builds stop on a write begun under an open read.
pic run takes only requests nobody is running. A second pic asking into a conversation in use is refused with its owner's pid. An edit that misses in a file another conversation changed says so and shows the file; a write to such a file is refused once.
New test/pic/shared.sh: two-process crash matrix, kill -9 beside a live pic, claim and admission races, stale-file results. No schema change. (user: dmestas, size: 3573) [annotate] [blame] [check-ins using]
/* What pic asks of the machine it runs on, apart from files a tool touches * and commands it runs (pic_executor, in pic.h): clocks, entropy, the * journal's single-writer lock, file inputs, and the SQLite VFS for pic's * databases. The host's is the only one today; a build where pic is the * whole guest of a microVM supplies its own with pic_platform_set(). */ #ifndef PIC_PLATFORM_H #define PIC_PLATFORM_H #include <stddef.h> #include <stdint.h> typedef struct pic_platform { /* Wall-clock time in ns since the Unix epoch, or -1 when unknown. */ int64_t (*wall_ns)(void); /* A clock that never steps back, in ns from an arbitrary origin. */ uint64_t (*mono_ns)(void); /* Fills buf with n (at most 256) random bytes: 0, or -1 with errno. */ int (*entropy)(void *buf, size_t n); /* Takes the lock named by path without waiting: shared with other * shared holders when SHARED, else exclusive. A handle (>= 0) on * success; -1 with errno when the lock cannot be opened; -2 with errno * (EWOULDBLOCK: someone else holds it) when it is not taken. The lock * lasts until unlock() or until this process and the children it forks * have all exited. The file may be removed by its exclusive holder; the * lock taken is always on the file now at the path. */ int (*lock)(const char *path, int shared); void (*unlock)(int handle); /* pic_input_read (input.h) dispatches here. */ char *(*input_read)(const char *path, size_t max, int flags, size_t *len, int *truncated); /* Calls fn(ctx, name) for each entry of the directory DIR (not . or ..) * until it returns nonzero: 0, or -1 with errno when DIR cannot be * opened. pic_input_read reads what it names. */ int (*input_list)(const char *dir, int (*fn)(void *ctx, const char *name), void *ctx); /* The VFS pic's journal and ~/.pic/pic.sqlite are opened with; NULL is * SQLite's default. */ const char *vfs; /* The credential store's master key source (cred.c), asked only when a * stored key is needed. Writes the secret into buf (at most cap bytes) * and its length into *len; *stretch 1 when it is a passphrase cred.c * must stretch with the store's KDF, 0 when it is a 32-byte key already. * FRESH: the store has no key yet (a passphrase source asks twice). 0, or * -1 with err. NULL: this build has no source, and stored keys cannot be * used. The host's asks on the controlling terminal; a no-OS build * supplies its boot key here; a Keychain/libsecret source would go here * too. */ int (*secret_key)(int fresh, unsigned char *buf, size_t cap, size_t *len, int *stretch, char *err, size_t errcap); } pic_platform; /* This machine's: POSIX clocks, getentropy, flock, the filesystem. */ const pic_platform *pic_platform_host(void); /* The one in use, the host's unless another was set. */ const pic_platform *pic_platform_get(void); void pic_platform_set(const pic_platform *p); /* Host only: reads a line from the controlling terminal with echo off, * after writing PROMPT to it; the newline is dropped. Its length, or -1 with * err (no terminal, too long, or interrupted). */ long pic_tty_secret(const char *prompt, char *buf, size_t cap, char *err, size_t errcap); /* input.c's reader and lister of host files, the host platform's input_read and input_list. */ char *pic_input_read_host(const char *path, size_t max, int flags, size_t *len, int *truncated); int pic_input_list_host(const char *dir, int (*fn)(void *ctx, const char *name), void *ctx); #endif