A Middle-Period Engine: Reconstructing Concurrency Invariants Through East Asian Structural Logic
by
gg582 · 2026-09-11 04:18:16 · 46 views
Discussions on the theoretical foundations of computation typically draw from the Western formal lineage: Aristotle's categorical syllogisms, Leibniz's calculus ratiocinator, and the Boolean algebra that grounded digital circuitry. Non-Western systems, when brought up, are frequently reduced to cultural tropes or philosophical abstractions.
A more productive comparison lies in analyzing how structural frameworks from East Asia's Middle Period (11th to 14th centuries, spanning Song-Yuan China and late Goryeo Korea) approached deterministic state machines. During this era, Eurasian scientific exchange introduced Islamic astronomical computation and spherical trigonometry into East Asia, culminating in the Huihui Astronomical Bureau and Guo Shoujing's Shoushi calendar. Concurrently, regional mathematics and mechanical horology maintained an algorithmic, array-centric paradigm:
- Shao Yong's combinatorial ordering (Xiantian system), which mapped trigram structures as deterministic 2^n state transitions.
- The polynomial matrix algebra of Qin Jiushao, Li Zhi, and Zhu Shijie, which formalized algorithmic elimination through positional array operations: placement (置), subtraction (減), and variable cancellation (消) on counting boards.
- Mechanical interlock and escapement engineering, exemplified by Su Song's water-driven clock tower, where mechanical stops enforced sequential exclusion to prevent structural damage.
These structural treatises treat systems as bounded entities with explicit visibility, state transitions, and exclusion rules. By re-evaluating these principles through modern low-level systems programming, they provide a precise symbolic language for concurrent memory reclamation, specifically the separation between pointer visibility and heap resource destruction.
The Problem: Concurrent Deallocation of a Shared Target
Consider a container struct (house_t) holding two pointer fields (l, r). Both pointers reference a shared heap allocation p (the jade, 玉). Two worker threads execute cleanup operations concurrently. If both threads attempt to deallocate p without an explicit sequence for revoking visibility, the runtime encounters an uncoordinated race, resulting in a Double Free (減二回).
The problem is stated below in classical procedural syntax, followed by an English translation and concrete axiomatic definitions:
今有樓閣, 置一玉於樓中格.
置兩鏡於左右格.
欲除玉, 兩鏡各欲減玉, 遂致減二回.
若鏡唯識 "減一回" 與 "勿減", 則鏡當動幾何?
身物皆陽, 目視為陰.
Translation: Suppose there is a pavilion; a single jade is placed in its central chamber. Two mirrors are placed in the left and right chambers. To remove the jade, both mirrors attempt to diminish it, resulting in diminishing it twice. If a mirror can only identify two states, "Diminish Once" or "Do Not Diminish", how must the mirrors act?
Axiomatic Mapping:
- Substance and Data (身, 物) are Yang (陽): Concrete memory blocks allocated on the heap (the container node and the data payload p).
- Pointers and Visibility (目) are Yin (陰): Unsubstantial directional references (the address links l and r).
- Diminishing Substance (減身, 減物): Invoking free() on an allocation.
- Diminishing Reference (減目, 無目): Revoking visibility by writing NULL to pointer links.
The Four Methods (四法) and Thread Concurrency Mechanics
To model concurrent execution with concrete race semantics, consider two worker threads operating on shared state using C11 atomic primitives:
#include <stdatomic.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int value;
} payload_t;
typedef struct {
_Atomic(payload_t *) l;
_Atomic(payload_t *) r;
} house_t;
// Shared initialization prior to thread execution:
// house_t *house = malloc(sizeof(house_t));
// payload_t *p = malloc(sizeof(payload_t));
// atomic_init(&house->l, p);
// atomic_init(&house->r, p);
- The Qian Method (乾法): The Canonical Invariant
乾法
包兩鏡, 使鏡未知.
置兩鏡於樓外, 減二身.
置玉於樓外, 減一物.
減二身, 減一物.
// Thread A (Worker 1)
payload_t *expected = p;
if (atomic_compare_exchange_strong(&house->l, &expected, NULL)) {
// Left visibility successfully revoked
}
// Thread B (Worker 2)
payload_t *expected = p;
if (atomic_compare_exchange_strong(&house->r, &expected, NULL)) {
// Right visibility successfully revoked
}
// Barrier / Synchronization: Once both paths evaluate to NULL,
// exactly one coordinating thread retires the substance.
// (e.g., Coordinator or last CAS winner)
free(p);
Text Analysis:
The mirrors are veiled so they cannot observe the jade (包兩鏡, 使鏡未知). Both mirrors are moved outside the pavilion, retiring their node references (置兩鏡於樓外, 減二身). Finally, the jade is moved outside and deallocated (置玉於樓外, 減一物).
Concurrency Mechanics:
In Epoch-Based Reclamation (EBR) and Read-Copy-Update (RCU), unlinking must strictly precede memory reclamation. By clearing house->l and house->r before invoking free(p), concurrent worker threads can no longer traverse down to the node. Both paths transition into the "Do Not Diminish" state. Exactly one designated actor reclaims p, preventing data races and double frees.
- The Kun Method (坤法): Asymmetric Handoff
坤法
包右鏡, 使鏡未知.
置右鏡於樓外, 減一身.
置玉於樓外, 減一物.
減左鏡, 感一目.
減一身, 減一目, 減一物.
// Thread B (Reclaimer)
// 1. Sever right path first (包右鏡, 置右鏡於樓外)
atomic_store_explicit(&house->r, NULL, memory_order_release);
// 2. Safely retire payload
free(p);
// Thread A (Reader/Consumer executing concurrently)
// 3. Left path remains published; reader senses the remaining link (感一目)
payload_t *node = atomic_load_explicit(&house->l, memory_order_acquire);
if (node != NULL) {
// Stale or transferred reference: Thread A must not free(p)
atomic_store_explicit(&house->l, NULL, memory_order_release);
}
Text Analysis:
The right mirror is isolated first, eliminating one reference body (減一身). The jade is deallocated, and the left mirror is resolved while sensing an open visual path (感一目).
Concurrency Mechanics: This represents asymmetric transfer or pipeline ownership handoff. In biased lock-free structures or directed consumer pipelines, one path is unlinked while an alternate worker processes the remaining handle. It violates structural symmetry and requires explicit barrier synchronization, but remains valid within strictly ordered single-producer single-consumer stages.
- The Kan Method (坎法): The Dangling Trap
坎法
置鏡面向玉, 勿減其身, 感一目.
置玉於樓外, 減一物.
減兩鏡, 感一目.
減二目, 減一物.
// Thread A (Premature Reclaimer)
// Fails to sever visibility; frees memory while pointers remain live
free(p);
atomic_store_explicit(&house->l, NULL, memory_order_relaxed);
atomic_store_explicit(&house->r, NULL, memory_order_relaxed);
// Thread B (Concurrent Reader)
// Dereferences pointer before Thread A executes the NULL store
payload_t *target = atomic_load_explicit(&house->r, memory_order_relaxed);
if (target != NULL) {
int leak = target->value; // CRASH / Use-After-Free (UAF)
}
Text Analysis:
The mirrors remain facing the jade without severing their presence (置鏡面向玉, 勿減其身). The substance is moved out and deallocated first, while the paths remain open (置玉於樓外, 減一物).
Concurrency Mechanics:
A textbook Use-After-Free (UAF) race condition. The underlying heap block p is deallocated while house->l and house->r remain globally published. If Thread B dereferences house->r concurrently before Thread A's clear propagates, it accesses unmapped or recycled memory, causing undefined behavior or process termination.
- The Li Method (離法): Total Incineration
離法
包樓, 包鏡, 包玉, 使鏡未知.
置兩鏡於樓外, 減三目.
無目如不見.
去樓.
// Thread A (Zeroing Coordinator)
// Wipes both container pointers and payload concurrently
memset(house, 0, sizeof(house_t));
memset(p, 0, sizeof(payload_t));
free(house);
free(p);
// Thread B (Concurrent Reader)
// Concurrent read races with memset -> torn read or wild pointer
payload_t *corrupted = atomic_load_explicit(&house->l, memory_order_relaxed);
if (corrupted != NULL) {
int fault = corrupted->value; // Page fault / dereferencing freed container
}
Text Analysis:
The pavilion, mirrors, and jade are enveloped indiscriminately. References are scrubbed into complete blindness (無目如不見), and the structure itself is removed (去樓).
Concurrency Mechanics:
Rather than coordinating access lifecycles, this approach relies on raw memory zeroization (memset) immediately before freeing. While clearing addresses avoids double-free invocations on intact pointers, issuing raw byte-level resets across shared structs during active concurrent reads causes torn reads and dereferences into reclaimed parent containers. It bypasses concurrency coordination by destroying the entire allocation context.
The Verdict
勿用坎, 離消母子.
乾為正法, 坤為仄法.
Translation and Operational Assessment:
勿用坎(Do not employ Kan): Kan represents a hazardous depression or trap (坎險). Deallocating memory while reference paths remain published creates an invisible dangling pointer. This is an explicit prohibition of Use-After-Free patterns.離消母子(Li incinerates mother and child): Li represents uncontained combustion. Scrubbing the container (house, the mother) and its internal allocation (p, the child) simultaneously viamemsetwithout unlinking guarantees races across active readers.乾為正法, 坤為仄法(Qian is the orthodox rule; Kun is the oblique expedient): Symmetric visibility revocation (乾) is the canonical invariant for concurrent reclamation: sever paths first, synchronize, then deallocate. Asymmetric decoupling (坤) is permissible strictly within directed, non-symmetric pipelines.
Examined without mysticism or anachronistic bias, this Middle-Period framework provides a consistent state-machine vocabulary: substance occupies memory, visibility governs entry paths, and reference lifetime must never exceed the boundary of allocated storage.