All files / src/impl AtomicBoolean.impl.ts

100% Statements 23/23
100% Branches 9/9
100% Functions 10/10
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                  26x 1822x         26x 26x     4201x                         1222x               1831x             2098x 2098x     2098x             4x 4x 4x             4x 4x   1x   1x   2x               4x       1822x       1822x     1822x 1822x      
import { AtomicBoolean } from "@jonloucks/contracts-ts/auxiliary/AtomicBoolean";
import { RequiredType } from "@jonloucks/contracts-ts/api/Types";
 
/**
 * Factory to create an AtomicBoolean implementation
 * 
 * @param initialValue the initial boolean value
 * @returns the new AtomicBoolean implementation
 */
export function create(initialValue?: boolean): RequiredType<AtomicBoolean> {
  return AtomicBooleanImpl.internalCreate(initialValue);
}
 
// ---- Implementation details below ----
 
const FALSE_AS_NUMBER: number = 0;
const TRUE_AS_NUMBER: number = 1;
 
function booleanToNumber(value: boolean): number {
  return value ? TRUE_AS_NUMBER : FALSE_AS_NUMBER;
}
 
/**
 * Implementation of AtomicBoolean.
 */
class AtomicBooleanImpl implements AtomicBoolean {
 
  /**
   * AtomicBoolean.get override
   */
  get(): boolean {
    // Atomics.load ensures thread-safe reading
    return Atomics.load(this.#array, 0) === TRUE_AS_NUMBER;
  }
 
  /** 
   * AtomicBoolean.set override
   */
  set(value: boolean): void {
    // Atomics.store ensures thread-safe writing
    Atomics.store(this.#array, 0, value ? TRUE_AS_NUMBER : FALSE_AS_NUMBER);
  }
 
  /**
   * AtomicBoolean.compareAndSet override
   */
  compareAndSet(expected: boolean, newValue: boolean): boolean {
    const expectedVal: number = booleanToNumber(expected);
    const newTargetVal: number = booleanToNumber(newValue);
 
    // Atomics.compareExchange checks value and sets it if it matches expectation
    return Atomics.compareExchange(this.#array, 0, expectedVal, newTargetVal) === expectedVal;
  }
 
  /**
   * AtomicBoolean.getAndSet override
   */
  getAndSet(newValue: boolean): boolean {
    const newTargetVal: number = booleanToNumber(newValue);
    const previousVal: number = Atomics.exchange(this.#array, 0, newTargetVal);
    return previousVal === TRUE_AS_NUMBER;  
  }
 
  /** 
   * [Symbol.toPrimitive] override
   */
  [Symbol.toPrimitive](hint: string): string | boolean | number {
    const value: boolean = this.get();
    switch (hint) {
      case 'string':
        return value.toString();
      case 'number':
        return booleanToNumber(value);
      default:
        return value;
    }
  }
 
  /** 
   * Object.toString override
   */
  toString(): string {
    return this.get().toString();
  }
 
  static internalCreate(initialValue?: boolean): AtomicBoolean {
    return new AtomicBooleanImpl(initialValue ?? false);
  }
 
  private constructor(initialValue: boolean) {
    this.set(initialValue);
  }
 
  readonly #buffer: SharedArrayBuffer = new SharedArrayBuffer(4);
  readonly #array: Int32Array = new Int32Array(this.#buffer);
}