|
| 1 | +import { |
| 2 | + register, |
| 3 | + Serializable, |
| 4 | + toDeserialized, |
| 5 | + toSerialized, |
| 6 | +} from "../shared.ts"; |
| 7 | + |
| 8 | +const IDX_LOCK = 0; |
| 9 | +const IDX_CAP = 1; // Capacity (N) |
| 10 | +const IDX_COUNT = 2; // Current count (Starts at 0, goes to N, or N down to 0) |
| 11 | +const IDX_GEN = 3; // Generation ID |
| 12 | + |
| 13 | +const META_SIZE = 4; |
| 14 | + |
| 15 | +const LOCK_UNLOCKED = 0; |
| 16 | +const LOCK_LOCKED = 1; |
| 17 | + |
| 18 | +export interface BarrierWaitResult { |
| 19 | + isLeader: boolean; |
| 20 | +} |
| 21 | + |
| 22 | +export class Barrier extends Serializable { |
| 23 | + static { |
| 24 | + register(8, this); |
| 25 | + } |
| 26 | + |
| 27 | + readonly #state: Int32Array<SharedArrayBuffer>; |
| 28 | + |
| 29 | + constructor(n?: number, _buffer?: SharedArrayBuffer) { |
| 30 | + super(); |
| 31 | + if (_buffer) { |
| 32 | + this.#state = new Int32Array(_buffer); |
| 33 | + } else { |
| 34 | + if (n === undefined) { |
| 35 | + throw new Error("Barrier capacity must be provided"); |
| 36 | + } |
| 37 | + this.#state = new Int32Array( |
| 38 | + new SharedArrayBuffer(META_SIZE * Int32Array.BYTES_PER_ELEMENT), |
| 39 | + ); |
| 40 | + this.#state[IDX_LOCK] = LOCK_UNLOCKED; |
| 41 | + this.#state[IDX_CAP] = n; |
| 42 | + this.#state[IDX_COUNT] = n; // We count down from N to 0 |
| 43 | + this.#state[IDX_GEN] = 0; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Internal Spin/Wait Lock for protecting state updates. |
| 49 | + * Updates are very fast, so we use a simple atomic lock mechanism. |
| 50 | + */ |
| 51 | + #lock() { |
| 52 | + while ( |
| 53 | + Atomics.compareExchange( |
| 54 | + this.#state, |
| 55 | + IDX_LOCK, |
| 56 | + LOCK_UNLOCKED, |
| 57 | + LOCK_LOCKED, |
| 58 | + ) !== LOCK_UNLOCKED |
| 59 | + ) { |
| 60 | + Atomics.wait(this.#state, IDX_LOCK, LOCK_LOCKED); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + #unlock() { |
| 65 | + if ( |
| 66 | + Atomics.compareExchange( |
| 67 | + this.#state, |
| 68 | + IDX_LOCK, |
| 69 | + LOCK_LOCKED, |
| 70 | + LOCK_UNLOCKED, |
| 71 | + ) !== LOCK_LOCKED |
| 72 | + ) { |
| 73 | + throw new Error("Barrier lock state corrupted"); |
| 74 | + } |
| 75 | + Atomics.notify(this.#state, IDX_LOCK, 1); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Async version of the internal lock for Main Thread compatibility. |
| 80 | + */ |
| 81 | + async #lockAsync() { |
| 82 | + while ( |
| 83 | + Atomics.compareExchange( |
| 84 | + this.#state, |
| 85 | + IDX_LOCK, |
| 86 | + LOCK_UNLOCKED, |
| 87 | + LOCK_LOCKED, |
| 88 | + ) !== LOCK_UNLOCKED |
| 89 | + ) { |
| 90 | + const res = Atomics.waitAsync(this.#state, IDX_LOCK, LOCK_LOCKED); |
| 91 | + if (res.async) { |
| 92 | + await res.value; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Blocks the current thread until all participating threads have reached the barrier. |
| 99 | + */ |
| 100 | + blockingWait(): BarrierWaitResult { |
| 101 | + this.#lock(); |
| 102 | + const localGen = Atomics.load(this.#state, IDX_GEN); |
| 103 | + const count = Atomics.load(this.#state, IDX_COUNT) - 1; |
| 104 | + |
| 105 | + if (count === 0) { |
| 106 | + // We are the leader (the last one to arrive) |
| 107 | + Atomics.store(this.#state, IDX_COUNT, Atomics.load(this.#state, IDX_CAP)); |
| 108 | + Atomics.add(this.#state, IDX_GEN, 1); |
| 109 | + this.#unlock(); |
| 110 | + |
| 111 | + // Wake everyone up. They are waiting on IDX_GEN changing. |
| 112 | + Atomics.notify(this.#state, IDX_GEN, Infinity); |
| 113 | + return { isLeader: true }; |
| 114 | + } else { |
| 115 | + // We are a follower |
| 116 | + Atomics.store(this.#state, IDX_COUNT, count); |
| 117 | + this.#unlock(); |
| 118 | + |
| 119 | + // Wait until the generation changes |
| 120 | + while (Atomics.load(this.#state, IDX_GEN) === localGen) { |
| 121 | + Atomics.wait(this.#state, IDX_GEN, localGen); |
| 122 | + } |
| 123 | + return { isLeader: false }; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Asynchronously waits until all participating threads have reached the barrier. |
| 129 | + */ |
| 130 | + async wait(): Promise<BarrierWaitResult> { |
| 131 | + await this.#lockAsync(); |
| 132 | + const localGen = Atomics.load(this.#state, IDX_GEN); |
| 133 | + const count = Atomics.load(this.#state, IDX_COUNT) - 1; |
| 134 | + |
| 135 | + if (count === 0) { |
| 136 | + // We are the leader |
| 137 | + Atomics.store(this.#state, IDX_COUNT, Atomics.load(this.#state, IDX_CAP)); |
| 138 | + Atomics.add(this.#state, IDX_GEN, 1); |
| 139 | + this.#unlock(); |
| 140 | + |
| 141 | + Atomics.notify(this.#state, IDX_GEN, Infinity); |
| 142 | + return { isLeader: true }; |
| 143 | + } else { |
| 144 | + // We are a follower |
| 145 | + Atomics.store(this.#state, IDX_COUNT, count); |
| 146 | + this.#unlock(); |
| 147 | + |
| 148 | + // Wait until the generation changes |
| 149 | + while (Atomics.load(this.#state, IDX_GEN) === localGen) { |
| 150 | + const res = Atomics.waitAsync(this.#state, IDX_GEN, localGen); |
| 151 | + if (res.async) { |
| 152 | + await res.value; |
| 153 | + } |
| 154 | + } |
| 155 | + return { isLeader: false }; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + [toSerialized]() { |
| 160 | + return { |
| 161 | + value: this.#state.buffer, |
| 162 | + transfer: [], |
| 163 | + }; |
| 164 | + } |
| 165 | + |
| 166 | + static override [toDeserialized]( |
| 167 | + buffer: ReturnType<Barrier[typeof toSerialized]>["value"], |
| 168 | + ) { |
| 169 | + return new Barrier(undefined, buffer); |
| 170 | + } |
| 171 | +} |
0 commit comments