UNPKG

rc-js-util

Version:

A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.

143 lines 6.9 kB
import { nullPtr } from "../emscripten/null-pointer.js"; import { _Production } from "../../production/_production.js"; import { promisePoll } from "../../promise/impl/promise-poll.js"; import { _Debug } from "../../debug/_debug.js"; import { NestedError } from "../../error-handling/nested-error.js"; import { WasmErrorCause } from "../wasm-error-cause.js"; import { ESharedObjectOwnerKind, SharedObjectCleanup } from "../shared-memory/shared-object-cleanup.js"; /** * @public * How to handle jobs which don't "overflow", i.e. the workers cannot keep up with the work being sent. */ export var EWorkerPoolOverflowMode; (function (EWorkerPoolOverflowMode) { /** * Delete the job and then throw a {@link NestedError} with a cause of {@link WorkerPoolErrorCause.overflow}. * @remarks This is intended mainly for unit tests. * @remarks Ownership of the job is transferred to the job queue. */ EWorkerPoolOverflowMode[EWorkerPoolOverflowMode["Throw"] = 1] = "Throw"; /** * If no worker is able to accept the job, the job runs on the producer (caller) thread. This automatically * "fixes" backpressure by throttling the caller thread. This will result in degraded performance on the producer thread * (often the UI thread) which is not always desirable. * @remarks {@link IWorkerPool.addJob} will return false where the job ran synchronously. * @remarks Ownership of the job is transferred to the job queue. */ EWorkerPoolOverflowMode[EWorkerPoolOverflowMode["Synchronous"] = 2] = "Synchronous"; /** * Do nothing, it's up to you to choose an action. * @remarks {@link IWorkerPool.addJob} will return false where the job did not run. * @remarks Ownership of the job is NOT transferred to the job queue, the caller must clean up. */ EWorkerPoolOverflowMode[EWorkerPoolOverflowMode["Noop"] = 3] = "Noop"; })(EWorkerPoolOverflowMode || (EWorkerPoolOverflowMode = {})); /** * @public * The static members are the cause in {@link INestedError}. */ export class WorkerPoolErrorCause { } WorkerPoolErrorCause.overflow = "WorkerPoolErrorCause.overflow"; /** * @public * {@inheritDoc IWorkerPool} */ export class WorkerPool { static createRoundRobin(wrapper, config, bindToReference, allocationFailThrows = true) { return createRoundRobinImpl(wrapper, config, bindToReference, allocationFailThrows); } getWrapper() { return this.wrapper; } start() { _BUILD.DEBUG && _Debug.assert(!this.resourceHandle.getIsDestroyed(), "use after free"); const started = this.wrapper.instance._workerPool_start(this.pointer); return promisePoll(() => this.wrapper.instance._workerPool_isAcceptingJobs(this.pointer)) .getPromise() .then(() => started); } stop() { _BUILD.DEBUG && _Debug.assert(!this.resourceHandle.getIsDestroyed(), "use after free"); this.wrapper.instance._workerPool_stop(this.pointer, false); return promisePoll(() => !this.wrapper.instance._workerPool_isAnyWorkerRunning(this.pointer)) .getPromise() .then(() => undefined); } isRunning() { _BUILD.DEBUG && _Debug.assert(!this.resourceHandle.getIsDestroyed(), "use after free"); return Boolean(this.wrapper.instance._workerPool_isAnyWorkerRunning(this.pointer)); } isBatchDone() { _BUILD.DEBUG && _Debug.assert(!this.resourceHandle.getIsDestroyed(), "use after free"); return Boolean(this.wrapper.instance._workerPool_isBatchDone(this.pointer)); } setBatchEnd() { _BUILD.DEBUG && _Debug.assert(!this.resourceHandle.getIsDestroyed(), "use after free"); this.wrapper.instance._workerPool_setBatchEndPoint(this.pointer); } invalidateBatch() { _BUILD.DEBUG && _Debug.assert(!this.resourceHandle.getIsDestroyed(), "use after free"); this.wrapper.instance._workerPool_invalidateBatch(this.pointer); } areWorkersSynced() { return Boolean(this.wrapper.instance._workerPool_areWorkersSynced(this.pointer)); } hasPendingWork() { _BUILD.DEBUG && _Debug.assert(!this.resourceHandle.getIsDestroyed(), "use after free"); return Boolean(this.wrapper.instance._workerPool_hasPendingWork(this.pointer)); } addJob(jobPtr) { _BUILD.DEBUG && _Debug.runBlock(() => { _Debug.assert(!this.resourceHandle.getIsDestroyed(), "use after free"); _Debug.assert(jobPtr !== nullPtr, "expected job, got nullptr"); }); const added = this.wrapper.instance._workerPool_addJob(this.pointer, jobPtr); if (!added) { switch (this.overflowMode) { case EWorkerPoolOverflowMode.Noop: // intentional fallthrough case EWorkerPoolOverflowMode.Synchronous: break; case EWorkerPoolOverflowMode.Throw: { throw new NestedError("WorkerPool job queue overflowed.", WorkerPoolErrorCause.overflow); } default: _Production.assertValueIsNever(this.overflowMode); } } return added; } // @internal constructor(wrapper, ownerNode, pointer, overflowMode) { this.wrapper = wrapper; this.resourceHandle = wrapper.lifecycleStrategy.createNode(ownerNode); this.pointer = pointer; this.overflowMode = overflowMode; this.cleanup = new SharedObjectCleanup(this, ESharedObjectOwnerKind.SharedMemoryOwner); SharedObjectCleanup.registerCleanup(this, this.cleanup, new SharedObjectCleanup.Options("WorkerPool", null, ESharedObjectOwnerKind.SharedMemoryOwner)); } } function createRoundRobinImpl(wrapper, config, bindToReference, allocationFailThrows = true) { var _a; _BUILD.DEBUG && wrapper.debugUtils.onAllocate.emit(); const overflowMode = (_a = config.overflowMode) !== null && _a !== void 0 ? _a : EWorkerPoolOverflowMode.Synchronous; const maxSize = 0xFFFF; if (config.workerCount > maxSize) { throw _Production.createError(`Requested pool size ${config.workerCount}, exceeds limit ${maxSize}.`); } if (config.queueSize > maxSize) { throw _Production.createError(`Requested queue size ${config.queueSize}, exceeds limit ${maxSize}.`); } const pointer = wrapper.instance._workerPool_createRoundRobin(config.workerCount, config.queueSize, overflowMode === EWorkerPoolOverflowMode.Synchronous); if (pointer == nullPtr) { if (allocationFailThrows) { throw new NestedError("Failed to allocate memory for worker pool.", WasmErrorCause.allocationFailure); } else { return null; } } return new WorkerPool(wrapper, bindToReference, pointer, overflowMode); } //# sourceMappingURL=worker-pool.js.map