Definition
Immutable publication is a way to share data between threads, instruction sequences that run concurrently over one memory. A writer builds a complete new object where no other thread can reach it, then makes the object visible with one atomic store of its address into a shared pointer, ordered so that every write that built the object becomes visible no later than the address. Nobody changes the object after that store. A reader loads the pointer once and reads a complete version that cannot change under it.
The idea
A writer that edits a shared record in place, while other threads read it, causes two problems. A reader can read some fields before the edit and others after it, a torn read. And a reader can see a new pointer before the contents it points to: even when the writer stores the pointer last in its program, the compiler or the processor may make that store visible to another processor before the stores that filled the record. A lock around every read fixes both problems, but then every reader pays for the lock on every read.
Immutable publication never edits anything a reader can see. The writer prepares a new version completely, in private, and then switches one pointer. A reader that loads the pointer before the switch gets the old version, whole; a reader that loads it after the switch gets the new version, whole. Because no version changes once published, a reader needs no lock to read the fields. A change is made by publishing another new version.
A printed timetable in a display case is a fair picture. The transit company prints the new timetable and then swaps it into the case; nobody corrects the displayed sheet with a pen. The picture stops in two places. In a program, printing before swapping is not automatic: the release ordering of the pointer store is what guarantees it. And the old sheet must not be shredded while someone is still reading it. Publication says nothing about that, so a separate reclamation protocol must decide when the old version may be freed.
How it works
The writer follows five steps.
Allocate the new object B in memory that no other thread can reach.
Initialize every field of B, including any buffer that B points to, and check that B is internally consistent, for example that a length field matches the size of its buffer.
Publish B: store B’s address into the shared pointer with one atomic store that has release ordering. Release ordering means that every write the thread made before the store becomes visible to any thread whose acquire load reads the stored address (see the acquire-release ordering §9.11 entry). In C11, the 2011 edition of the C standard, the store is
atomic_store_explicit(¤t, B,
memory_order_release);and in the Linux kernel it is
rcu_assign_pointer.From now on, never write to B or to anything that B’s readers rely on.
Retire the old object A, meaning record that A must be freed once no reader can still use it, and free A at that time.
The reader follows three steps.
Load the shared pointer once, with acquire ordering:
atomic_load_explicit(¤t, memory_order_acquire)in C11, orrcu_dereferencein the Linux kernel’s read-copy update §9.20 (RCU) interface.Keep the loaded address in a local variable and read every field through it. Loading the shared pointer a second time during the same operation can return a different version.
Keep the object alive until the last field read, with the lifetime protection that the program uses: an RCU read-side critical section §9.4 (the stretch of reader code during which RCU delays every free), a counted reference, or a hazard pointer (a shared slot in which a reader announces the address it is using, so that no writer frees that object).
Reclamation is a separate decision. Publishing B stops new readers from finding A, but readers that loaded A earlier may still be reading it. A is freed only when a lifetime mechanism shows that no reader can still hold it:
an RCU grace period §9.21, a wait that ends once every reader that started before the switch has finished;
reference counting §9.22, in which the shared pointer and every reader hold counted references and the last release frees A;
hazard pointers, where the writer frees A only when no reader’s slot holds A’s address;
no freeing at all, which is acceptable only when the program publishes a small, fixed number of versions over its whole run.
Reusing A’s memory for a later version is a free followed by an allocation, and it needs the same wait.
Several writers must also be ordered among themselves. Either a lock lets one writer at a time copy, change and publish, or each writer publishes with compare-and-swap §9.9, an atomic instruction that replaces the pointer only if it still holds the address the writer started from, and retries if it does not. Without either, two writers that both start from A each publish their own version, and the second publication silently discards the first writer’s change. A compare-and-swap loop can meet the ABA problem §9.25, in which a location changes and then returns to the value a writer read earlier, so that the comparison misses the change. That happens if A’s memory is freed and reused for a newer version while a writer still holds A’s address.
Worked example
Take 4-byte lengths and 4-byte pointers, as on a 32-bit processor. A record holds a length and a pointer to a byte buffer, so the record itself takes bytes. The shared pointer current points to version A: length 4, pointing to the 4 bytes 01 02 03 04. The writer creates version B in three steps.
Allocate an 8-byte buffer and fill it with the bytes
01to08.Allocate an 8-byte record B; set its length to 8 and its pointer to the new buffer.
Store B’s address into
currentwith release ordering.
A reader that loads current with acquire ordering and finds B’s address is guaranteed to read the length 8 and the eight filled bytes.
Suppose instead that the writer used a plain store in step 3. The compiler, or a processor that may reorder stores, such as many Arm processors, may then make the store to current visible before the store of B’s length. A reader on another processor can load B’s address and read the length field before the value 8 arrives there. The reader gets whatever the memory held before, for example 4000, left over from an earlier use of that memory. The reader copies 4000 bytes from an 8-byte buffer and runs bytes past its end, an out-of-bounds read §4.5.
Until A is freed, both versions exist. A takes bytes and B takes bytes, so together they take bytes, compared with 16 bytes once A is freed, not counting the memory allocator’s own overhead. If the writer publishes new versions faster than readers finish, several old versions wait at once.
Stated precisely
Let be the shared atomic pointer, the writer’s release store of B’s address into , and a reader’s acquire load of that returns B’s address. Immutable publication rests on three obligations, in time order.
Initialization before publication. Every write to B, and to any memory reachable from B that readers use, comes before in the writer’s program order. Because reads the value that stored, synchronizes with , and by the happens-before relation §9.10 every such write happens before every read that the reader makes through B after .
No change after publication. No thread, interrupt handler §6.12 or hardware device writes to B, or to memory reachable from B that readers use, from until B is freed.
Reclamation after last use. B is freed only after every reader that obtained B’s address has finished using it.
If all three hold, every reader that obtains B reads exactly the values that the writer stored before , all from the same version. In C11 terms, only needs to be atomic: the reader’s plain reads of B’s fields are not data races §9.3, because every write to those fields happens before those reads and no write runs at the same time as them. Release and acquire ordering supplies obligation 1, immutability is obligation 2, and a reclamation protocol supplies obligation 3. None of the three supplies another.
Limits and common mistakes
A reader can use an old version after a new one is published. A reader that loaded A keeps reading A to the end of its operation, even if B appeared a moment after its load.
Every change copies the whole object, even to change one byte. For large structures, persistent data structures, which share unchanged parts between versions and copy only the parts on the path to a change, reduce the copying.
Several versions can be alive at once, and each one occupies memory until its readers finish, as the worked example counts.
Writers that are not ordered among themselves lose updates, as described above.
The C qualifier const does not make an object immutable. It forbids writes through one particular pointer; another pointer to the same memory can still write, a cast can remove the qualifier, and a const record says nothing about the buffer that its pointer field points to. Immutability is a rule that the whole program must keep for everything readers reach through the object.
Writers are not only threads. A direct memory access §4.36 (DMA) controller, hardware that copies data into memory without the processor, or an interrupt handler that refills a buffer breaks immutability just as a thread does.
Publishing machine code needs more than release ordering. Release and acquire order data reads and writes, not instruction fetch. On processors with instruction caches or instruction prefetch, such as Arm processors, the new code may run only after explicit cache maintenance and an instruction synchronization barrier.
Immutability without a reclamation protocol still allows a use-after-free §4.6. Reclamation without immutability still allows torn reads.
Questions answered
How are publication ordering and eventual reclamation handled?
Publication ordering is handled by building the new version completely before making it reachable, and by making it reachable with one atomic store that has release ordering, while readers load the pointer with acquire ordering or an equivalent such as rcu_dereference in Linux. Release ordering means that every write the writer made before the store becomes visible to any reader whose load returns the stored address, so a reader that finds the new version also finds all of its fields initialized. The reader loads the pointer once per operation and reads every field through that one value, and nobody writes the published version again.
Reclamation is handled separately, because publishing the new version only stops new readers from finding the old one; readers that loaded the old address earlier may still be using it. The writer therefore retires the old version and frees it only when a lifetime mechanism shows that no reader can still hold it. The mechanism can be a read-copy update grace period, a wait that ends once every reader that started before the switch has finished; a reference count that reaches zero; or hazard pointers, shared slots in which each reader announces the object it is using and which the writer checks before freeing. Freeing the old version immediately after publication is a use-after-free bug.
Related entries: Acquire-release ordering (§9.11), Read-copy update (RCU) (§9.20), Reference counting (§9.22), Object lifetime (§4.1), Coherent snapshot (§9.23), ABA problem (§9.25). Further reading: [23, 78].