diff --git a/src/GameCult.Caching/Contracts/cultcache-store-composition.md b/src/GameCult.Caching/Contracts/cultcache-store-composition.md index bd11662c..d2c3bbeb 100644 --- a/src/GameCult.Caching/Contracts/cultcache-store-composition.md +++ b/src/GameCult.Caching/Contracts/cultcache-store-composition.md @@ -127,14 +127,19 @@ which route wrote it. ## Locking - One lock order: the cache's gate, then the store's gate, then the file lock. A - thread holds at most one cache's gate and at most one store's gate. A store's - gate may be taken inside a cache's gate (the attach; a journal writing a - cacheless file), never the reverse: while a thread holds a store's gate it - enters no cache gate it does not already hold, reads included, and no other - store's gate. Breaking the order throws `InvalidOperationException`. This is - checked at run time, not by comment: user code a cacheless call or an attach - runs under the store's gate (a document's getter, a constructor while a copied - record is read) is the way in. + store's gate may be taken inside a cache's gate (the attach; a journal writing + a cacheless file), never the reverse. What the code enforces, at run time and + by `InvalidOperationException`: a thread holds at most one store's gate; + while it holds a store's gate it enters no cache gate it does not already + hold, reads included; and a cache's hold (a write, a flush, an attach) entered + inside another cache's hold is refused. What it does not enforce: a cache + read made inside another cache's hold takes that cache's gate without a + check, so two threads each holding one cache and reading the other can + deadlock. That is open as `variants:follow_up:cross-cache-read-lock-order`; + until it lands, a journal or a getter must not read a second cache. User code + a cacheless call or an attach runs under the store's gate (a document's + getter, a constructor while a copied record is read) is the way into the + refused cases. - One owner picks the gate a store call holds. A store attached to a cache takes that cache's gate as its own lock, so a store's load callback into the cache cannot take the two out of order; a cacheless store takes its own gate. The @@ -155,8 +160,6 @@ which route wrote it. CultNet journal, under the cache's gate: it must not call the cache or wait for a thread that does. A store that writes its own cache throws through the journal's reentry guard, and the admission stands. -- A thread holds one cache's gate at a time: entering a cache's hold inside - another cache's hold throws. - A store doing I/O on behalf of its cache (pull, flush, commit) holds the gate, so that cache's readers wait for the I/O. The file lock (`.lock` or the directory commit lease) is always taken inside the gate and released before a diff --git a/unity/org.gamecult.cultlib/CHANGELOG.md b/unity/org.gamecult.cultlib/CHANGELOG.md index 6972cced..0821d622 100644 --- a/unity/org.gamecult.cultlib/CHANGELOG.md +++ b/unity/org.gamecult.cultlib/CHANGELOG.md @@ -23,7 +23,7 @@ Releases as 3.0.0 (major). ### Added -- A cacheless `WriteRecord` or `WriteDocument`, and an attach, hold the store's gate while they run, and user code they run (a document's property getters, a constructor while a copied record is read) now throws `InvalidOperationException` if it enters a cache it does not hold or a second store; it used to wait forever against an attach of the same store. A store call that queued on an attach that then failed runs under the gate that owns the store, not beside a cacheless call or a second cache's attach. +- A cacheless `WriteRecord` or `WriteDocument`, and an attach, hold the store's gate while they run, and user code they run (a document's property getters, a constructor while a copied record is read) now throws `InvalidOperationException` if it enters a cache it does not hold or a second store; it used to wait forever against an attach of the same store. A store call that queued on an attach that then failed runs under the gate that owns the store, not beside a cacheless call or a second cache's attach. An attach that fails takes its registry back, so a `PullAll` on the store afterwards is refused as unattached instead of loading records the next cache never sees. - `SingleFileBackingStore.ReadStoreSnapshot()` reads a store file once, under the store's in-process gate, with the reader a cache's store opens with, and returns the durable snapshot or null when nothing is at the path. `SingleFileBackingStore.WriteRecord(record, catalogEntry, holdsIds, registry)` writes one record without a cache: under the store's lock every other record and catalog entry is copied as stored, the file is replaced atomically, and the header is decided as a flush decides it. It refuses on a store attached to a cache. - `SingleFileBackingStore.WriteDocument(key, document, storedAt, registry)` writes one typed document without a cache, as a cache write does: it plans element ids, encodes the document as `T` (the type its schema id and ids are taken from, also for a derived instance) and marks the store when it holds an id; a failed write gives the minted ids back. It refuses on a store attached to a cache. The `registry` of `WriteRecord` and `WriteDocument` serves that call only; a store keeps none. - `CultCodec.DecodeDocument(key, payload)` decodes a stored payload as a cache load does (`CultCacheMessagePack.CreateCodec(registry)` makes the codec). It reads no file and holds no state. `CultElementIds` stays internal.