| 87 | const Mutex = require('../ch6-mutex/mutex.js'); |
| 88 | |
| 89 | class SharedRingBuffer { |
| 90 | constructor(shared/*: number | SharedArrayBuffer*/) { |
| 91 | this.shared = typeof shared === 'number' ? |
| 92 | new SharedArrayBuffer(shared + 16) : shared; |
| 93 | this.ringBuffer = new RingBuffer( |
| 94 | new Uint32Array(this.shared, 4, 3), |
| 95 | new Uint8Array(this.shared, 16) |
| 96 | ); |
| 97 | this.lock = new Mutex(new Int32Array(this.shared, 0, 1)); |
| 98 | } |
| 99 | |
| 100 | write(data) { |
| 101 | return this.lock.exec(() => this.ringBuffer.write(data)); |
| 102 | } |
| 103 | |
| 104 | read(bytes) { |
| 105 | return this.lock.exec(() => this.ringBuffer.read(bytes)); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | const { isMainThread, Worker, workerData } = require('worker_threads'); |
| 110 | const fs = require('fs'); |
nothing calls this directly
no outgoing calls
no test coverage detected