This document uses ASD-STE100 Simplified Technical English. The mathematical notation does not change.
$S \in \mathbb{N}$ and $8 \mid S$. The header size is $h = 16$.
A cell is a pair $(o, p) \in \mathbb{N}^2$ with $8 \mid o$ and $8 \mid p$. The cell occupies $[o,\ o + h + p)$.
A chain from $a$ to $b$ is a list of cells $c_1, \dots, c_k$ with $o_1 = a$, $o_{i+1} = o_i + h + p_i$ and $o_k + h + p_k = b$. A chain with no cells is a chain from $a$ to $a$.
The state is $\sigma = (E, T, O_E, O_T, P, Q)$:
The start state is $\sigma_0 = (0, S, 0, 0, \varnothing, \varnothing)$.
$$ I(\sigma) \;\equiv\; E = e_P + O_E \;\wedge\; T = t_Q - O_T \;\wedge\; e_P \le t_Q $$
The rest condition is $R(\sigma) \equiv I(\sigma) \wedge O_E = 0 \wedge O_T = 0$. At rest, $E = e_P$ and $T = t_Q$.
Each operation is a function $\Sigma \to \Sigma \times \bot\} \cup \mathbb{N}$. The operations of §3.1 and §3.2 can interleave with each other. An operation of §3.3 runs only when no other operation is in progress.
$E$ and $T$ are the two halves of one atomic word of $2H$ bits, $W = T \cdot 2^H + E$. Each read of a boundary reads $W$. Each move of a boundary is one read-modify-write on $W$. The old value of this read-modify-write contains the two halves. Two limits prevent a move out of its half:
$$ S \le 2^{H-1},\qquad n \le h + 2^{H-8} $$
A bump up does not add to $W$ and returns $\bot$ if one of these conditions is true:
This bump does not change the overrun. The exclusive path does the request.
A take or a growth with a request of more than $S$ bytes returns $\bot$ before it rounds the request. It does not change the state. Thus the round-up of a request to a multiple of 8 does not wrap, and $h + p$ does not wrap. The cell that a take returns is never smaller than its request.
Thus $E$ is never more than $2^{H-1} + L\,(h + 2^{H-8})$, where $L$ is the number of bumps in progress at the same time. $E$ does not carry into the half of $T$ while $2^{H-1} + L\,(h + 2^{H-8}) < 2^H$. This permits up to 127 bumps in progress at $H = 32$, and up to 120 at $H = 16$.
Each bump uses one lane of the turnstiles, and no other bump uses that lane at the same time. Thus $L$ is not more than the number of lanes. There are 63 lanes at $H = 32$ and 31 lanes at $H = 16$. A static assert in the C stops the build if $S + L\,(h + 2^{H-8}) \ge 2^H$ for this number of lanes.
A bump down subtracts $n \cdot 2^H$. It does not change the half of $E$. A borrow out of the half of $T$ goes out of the word.
A bump down does not read $E$ before its read-modify-write. Thus the limit on bumps down in progress is the number of lanes only. The bumps down in progress subtract not more than $L\,(h + 2^{H-8})$ from $T$. If they move $T$ below 0, the half of $T$ wraps to a value of more than $2^H - L\,(h + 2^{H-8})$. This value is more than $S$, because $S + L\,(h + 2^{H-8}) < 2^H$. Thus the condition $w - n \le S$ of §3.2 rejects each bump that reads the wrapped top, and no bump puts a cell below 0.
$$ (w, T_w) := (E, T),\quad W := W + n $$
$$ \tau \equiv (w + n \le T_w) \wedge (w + n \ge w) \wedge (T_w \le S) $$
$$ \tau \Rightarrow P := P \cdot (w, p),\ \text{answer } w + 8 $$
$$ \neg\tau \Rightarrow O_E := O_E + n,\ \text{answer } \bot $$
The first line is one atomic read-modify-write on $W$. $w$ and $T_w$ are the two halves of $W$ before the add. The fourth line is one atomic add. No operation subtracts from $E$.
$$ (E_w, w) := (E, T),\quad W := W - n \cdot 2^H $$
$$ \tau \equiv (w - n \ge E_w) \wedge (w - n \le w) \wedge (w - n \le S) $$
$$ \tau \Rightarrow Q := (w - n, p) \cdot Q,\ \text{answer } w - n + 8 $$
$$ \neg\tau \Rightarrow O_T := O_T + n,\ \text{answer } \bot $$
$$ E := E - O_E,\quad O_E := 0,\quad T := T + O_T,\quad O_T := 0 $$
Each exclusive operation starts with Settle. The exclusive operations are spill, trim, growth and sort. While an exclusive operation runs, no operation of §3.1 or §3.2 is in progress, and no such operation starts.
The operation sets the state bit of the cell to free. $P$, $Q$, $E$ and $T$ do not change. Then a full fence follows, and the operation reads the closed bit and the two edges. If the cell is at the edge of $e_P$ or at the edge of $t_Q$ after this read, or if the closed bit is 1, a Trim follows. A Trim that starts before the free reads the cell as free, or stores an edge that this read sees.
The free operation does not wait for an exclusive operation. One read-modify-write sets the closed bit and the trim bit of the tier of the cell. If the closed bit was 0, the free operation does the Trim. If the closed bit was 1, the operation stops, and the trim bit stays set. Each exclusive operation ends with one open pass:
Each take and each read of the gap starts with a check of the trim bits. If the word holds a trim bit, the operation sets the closed bit, does the open pass, and then continues. Thus, assume that the word holds no trim bit and that no free operation is in progress. Then no free cell is at the edge of $e_P$ or at the edge of $t_Q$.
Where the build declares $N > 0$ regions of $R$ bytes and a large region of $L$ bytes, the temporary tier is the regions, and $T = S - N R - L$ never moves. The large region lies from $S - L$ to $S$. Region $i$ lies from $S - L - (i + 1) R$ to $S - L - i R$. Each has a depth $D_i$, the bytes its stack holds from its end down.
A thread's place $k$, which the host gives, selects region $i = k \bmod N$ for a take within $R$. A take over $R$ goes to the large region, from any thread. Where $L = 0$, the cellblock refuses it. A take within $R$ that region $i$ refuses, bumped and spilled, goes to the large region the same way. The large region refuses it the same way.
Bump in region $i$, need $n$: $D_i := D_i + n$ by one fetch-add. If $D_i \le R$ after it, the cell lies at $S - i R - D_i$. If not, $D_i := D_i - n$ by one fetch-sub, before the pass ends, and the take goes to the spill. So a writer that drains the passes reads every $D_i$ exact.
Spill in region $i$, need $n$ (exclusive): the region swept as §3.5 sweeps the tier, from $S - i R - D_i$ to $S - i R$. The free run at its top returns to the region's gap. Then the last run that holds $n$, else the gap where it holds $n$, else $\bot$ with no busy cell moved.
Free of a cell in region $i$: as §3.4, with the region's top $S - i R - D_i$ in place of $T$. The trim of §3.5 sweeps each region and sets each $D_i$ from the free run at its top.
The idle walk passes the program, with the cellblock closed, the gap $[E, T)$ and each region's room $[S - L - (i + 1) R, S - L - i R - D_i)$, the large region's from $S - L$. Idle.lean proves that no byte of a span lies in a cell. A cellblock over a hospes pool has $S = 0$ until the program places it, and a take of $n$ bytes permits nothing unless $n < S$. Idle.lean proves that an unplaced cellblock refuses every take.
Regions.lean proves that each $D_i$ stays within its region's bound after any sequence of takes and gives from any thread. It proves that a refused take moves no busy cell, and that the regions together stay within $N R + L$. Stacks.lean proves that one stack two threads share sinks past every depth. A thread's own region has one thread, so it does not sink; the large region is the one stack threads share, and $L$ bounds it.
A pool can be larger than $2^{H-1}$ bytes. The pool is then a run of banks of $B = 2^{H-2}$ bytes, and bank $k$ starts at $k B$. The closed word holds the bank count and a frame for each tier, above its four flag bits. The frame of a tier is the bank that its position counts from. Each half of $W$ holds the position of its tier. The boundary is the base of its frame plus its position: $E = f_E B + e$ and $T = f_T B + t$. §3.1 to §3.8 are the case $f_E = f_T = 0$.
A pass reads the closed word once for its permit. The frames do not change while the pass holds its lane, because only an exclusive operation changes a frame, after the drain. Thus a bump reads the frames with no other load. An exclusive operation reads the frames in the fetch-or that sets the closed bit. Only that operation changes them until it clears the bit, so it also reads them with no other load.
The two limits of §3 apply to the positions, with $2^{H-1}$ in place of $S$. A bump up that reads $e > 2^{H-1}$ returns $\bot$ as in §3. A position $t > 2^{H-1}$ is a wrapped position. §3.1 and §3.2 compare $E$ and $T$ as offsets in the pool, and they test the wrap on the positions:
$$ \tau_{up} \equiv (w + n \le T_w) \wedge (e + n \ge e) \wedge (t_w \le 2^{H-1}) $$
$$ \tau_{down} \equiv (w - n \ge E_w) \wedge (t - n \le t) \wedge (t - n \le 2^{H-1}) $$
Each exclusive operation that stores a boundary selects the frames. The persistent end gets its own bank, $f_E = \min(\lfloor E / B \rfloor, c)$. The temporary top gets the bank below its own bank, $f_T = \min(\max(\lfloor T / B \rfloor - 1, 0), c)$. Here $c = \max(K - 2, 0)$ for a pool of $K$ banks. A position from bank $c$ reaches the end of the pool, so no frame is above it. Thus $e \le 2B$ and $t \le 2B$.
Below the cap, $e < B$, and $t \ge B$ if $T \ge B$. A bump then has a room of $B$ bytes or more before its position stops it. A pool of two banks or less has $c = 0$. Its frames stay at bank 0, and its read outside a pass reads $W$ alone.
One fetch-add on the closed word moves the frames. This add does not change the flag bits, so a trim bit that a free sets keeps its value. The largest pool is $(2^b - 1) B$ bytes, where $b$ is the number of bits in a field. This is $2^{50} - 2^{30}$ bytes at $H = 32$, and $511 \cdot 2^{14}$ bytes at $H = 16$.
A free does not read a frame. The header of a busy cell holds its tier in the second state bit. The free compares its cell with the boundary modulo $B$. A cell at the boundary agrees with the position modulo $B$ for each frame. A cell that agrees but is not at the boundary causes a Trim that does not change the boundary.
In a pool of more than two banks, a read of a boundary outside a pass reads an epoch, the frames, $W$ and the epoch again. An exclusive operation makes the epoch odd before it changes a frame, and even after the change. The read starts again until the two epochs agree and are even.
A round is a max interval between two exclusive operations. In a round, only the operations of §3.1, §3.2 and §3.4 run.
Lemma 1 (monotone in a round). In a round, $E$ does not decrease and $T$ does not increase.
Proof. §3.1 adds $n > 0$ to $E$. §3.2 subtracts $n > 0$ from $T$. §3.4 does not change $E$ or $T$. No other operation of the round changes them. $\square$
Lemma 2 (a rejection is absorbing). In a round, §3.1 rejects a bump up $B_1$. Then §3.1 also rejects each bump up $B_2$ whose read-modify-write follows the read-modify-write of $B_1$.
Proof. If §3.1 rejects $B_1$ and $B_1$ does not wrap, then $w_1 + n_1 > T_1$. $T_1$ is the value of $T$ that $B_1$ read. From Lemma 1, $w_2 \ge w_1 + n_1 > T_1 \ge T_2$.
$T_1 \ge T_2$ is true because $B_2$ reads $T$ in the same atomic step as its read-modify-write. This read-modify-write follows the read-modify-write of $B_1$. Thus $w_2 + n_2 > T_2$, and $\tau_2$ is false.
If §3.1 rejects $B_1$ because it wraps, then $w_1 + n_1 < w_1$, and $E$ is past $2^{64} - n_1$. Then $w_2 \ge w_1 + n_1$ modulo $2^{64}$ has one of these two results:
The mirror statement is true for bumps down, with $E$ in place of $T$.
All of Lemma 2 is the step $T_1 \ge T_2$. This step is true only if the first line of §3.1 is one atomic step on $E$ and $T$ together. Thus one word must contain the two boundaries. §6.2 shows the result with two words.
Lemma 3 (accepted bumps are contiguous). In a round, $B_1, \dots, B_k$ are the accepted bumps up, in read-modify-write order. Then $o_1 = e_P$ at the start of the round, and $o_{i+1} = o_i + n_i$.
Proof. From Lemma 2, each accepted bump is before each rejected bump in read-modify-write order. Thus nothing changes $E$ between two consecutive accepted bumps, and $w_{i+1} = w_i + n_i$. The first accepted bump reads the value of $E$ at the end of the previous round. Because $R$ is true at the start of the round, this value is $e_P$. $\square$
Lemma 4 ($I$ stays true in a round). $R(\sigma)$ is true at the start of a round. Then $I(\sigma)$ is true for all of the round, after the two atomic operations of each bump are complete.
Proof. From Lemma 3, the accepted bumps extend $P$ without gaps. Thus $e_P$ increases by exactly the needs of the accepted bumps. $E$ increases by each need, accepted or rejected, and $O_E$ increases by each rejected need. Thus $E - O_E = e_P$. The mirror is true for $T$.
For $e_P \le t_Q$: an accepted bump up has $w + n \le T \le t_Q$. Thus $e_P \le t_Q$ after it. The mirror is true for a bump down. $\square$
Lemma 5 (Settle puts the state at rest). If $I(\sigma)$ is true and no bump is in progress, Settle causes $R(\sigma)$.
Proof. From $I$, $E - O_E = e_P$ and $T + O_T = t_Q$. Settle writes these values and sets the overruns to zero. $\square$
Theorem. Start at $\sigma_0$, and interleave §3.1, §3.2 and §3.4 in any sequence between exclusive operations. Then these two statements are true:
Proof. $R(\sigma_0)$ is true. Use induction on rounds. If $R$ is true at the start of a round, $I$ is true for all of the round (Lemma 4). The exclusive operation at the end of the round finds no bump in progress, and its Settle causes $R$ (Lemma 5).
Each exclusive operation of §3.5 to §3.7 changes a chain into a chain. It sets $E = e_P$ and $T = t_Q$, with $e_P \le t_Q$. Thus $R$ is true at the start of the next round.
The cells do not overlap. Accepted cells are consecutive in a chain (Lemma 3), and $e_P \le t_Q$ separates $P$ from $Q$. $\square$
Replace the rejected branch of §3.1 with $E := E - n$. Then $E$ is not monotone in a round, and Lemma 2 fails:
$$ \begin{array}{lll} B:\ w = 6088,\ n = 64 & E \to 6152 & \neg\tau \ C:\ w = 6152,\ n = 64 & E \to 6216 & \neg\tau,\ \text{undo}\ E \to 6152 \ D:\ w = 6152,\ n = 24 & E \to 6176 & \neg\tau \ B\ \text{undo} & E \to 6112 & \ X:\ w = 6112,\ n = 32 & E \to 6144 & \tau,\quad 6144 \le T = 6144 \ D\ \text{undo} & E \to 6120 & \end{array} $$
§3.1 accepts $X$ at $o = 6112$, but $e_P = 6088$. The chain has a gap, and the cell of $X$ is past $E$.
Lemma 6 (two words are not absorbing). $E$ and $T$ are two atomic words. Thus a bump is two steps: first the read-modify-write $w := E,\ E := E + n$, and later the read of $T$ that decides $\tau$.
Then Lemma 2 fails. The check can reject a bump $A$ and accept a bump $B$, although the read-modify-write of $A$ is before the read-modify-write of $B$. After the round, $I$ is false.
Proof. The example is an interleaving with $S = 2^{20}$. The round starts at rest with $E = e_P = \mathtt{fd4c0}$ and $T = t_Q = \mathtt{fd868}$:
$$ \begin{array}{llll} A:\ w_A = \mathtt{fd4c0},\ n_A = \mathtt{1e8} & E \to \mathtt{fd6a8} & & \ B:\ w_B = \mathtt{fd6a8},\ n_B = \mathtt{a8} & E \to \mathtt{fd750} & & \ B\ \text{reads}\ T = \mathtt{fd868} & & \mathtt{fd750} \le \mathtt{fd868} & \tau_B,\ P := P \cdot (\mathtt{fd6a8}, \mathtt{98}) \ C:\ w_C = \mathtt{fd868},\ n_C = \mathtt{210} & T \to \mathtt{fd658} & & \ A\ \text{reads}\ T = \mathtt{fd658} & & \mathtt{fd6a8} > \mathtt{fd658} & \neg\tau_A,\ O_E := \mathtt{1e8} \ C\ \text{reads}\ E = \mathtt{fd750} & & \mathtt{fd658} < \mathtt{fd750} & \neg\tau_C,\ O_T := \mathtt{210} \end{array} $$
$B$ reads $T$ before $C$ moves it, and $A$ reads $T$ after this move. Thus $T_B > T_A$, but the read-modify-write of $A$ is before the read-modify-write of $B$. Here the step $T_1 \ge T_2$ of Lemma 2 fails.
After the round, $E - O_E = \mathtt{fd568} < e_P = \mathtt{fd750}$. The rejected bytes of $A$ are below the cell of $B$. Settle subtracts them from the top, and $E$ goes into the gap below a cell that is in use. The check of §3.1 accepts the next bump up over the cell of $B$. $\square$
Thus the two boundaries must be one word. The first line of §3.1 and the first line of §3.2 must each be one read-modify-write on this word. $E$ is the low half of the word, and $T$ is the high half. A bump up adds $n$ to the low half and reads the high half in the same step. Because of the order of the word, $T_1 \ge T_2$ is true.
Lean checks the models in theory/locus_carcerum. The theory requires embedded_types' theory, and the files on the turnstile import its Manifold proofs: Order.lean carries Manifold.Open.passes_below_open and Manifold.Drain.drain_lane_complete, Pass.lean carries the drain's completion, and Sleep.lean carries Manifold.Backoff.limit_fits.