···33//
44// Run: node examples/atomics/main.ts
5566-import { pool, int32atomic } from '../../src/index.ts';
66+import { workers, int32atomic } from '../../src/index.ts';
77import { increment } from './increment.ts';
8899const counter = int32atomic();
10101111{
1212- using run = pool(4);
1212+ using run = workers(4);
13131414 // Fire off 100 increments across 4 workers
1515 await Promise.all(Array.from({ length: 100 }, () => run(increment(counter))));
+2-2
examples/non-blocking/main.ts
···44//
55// Run: node examples/non-blocking/main.ts
6677-import { pool } from '../../src/index.ts';
77+import { workers } from '../../src/index.ts';
88import { fibonacci } from './fibonacci.ts';
991010// Tick a counter on the main thread to prove it's not blocked
···1818console.log('Meanwhile, the main thread keeps ticking:\n');
19192020{
2121- using run = pool(2);
2121+ using run = workers(2);
2222 const start = performance.now();
2323 const [a, b] = await Promise.all([run(fibonacci(42)), run(fibonacci(41))]);
2424 const elapsed = (performance.now() - start).toFixed(0);
+2-2
examples/parallel-batch/main.ts
···44//
55// Run: node examples/parallel-batch/main.ts
6677-import { pool } from '../../src/index.ts';
77+import { workers } from '../../src/index.ts';
88import { heavyWork } from './heavy-work.ts';
991010const items = Array.from({ length: 20 }, (_, i) => i + 1);
···2323let parTime: string;
2424console.log('Parallel (worker pool, 4 workers)...');
2525{
2626- using run = pool(4);
2626+ using run = workers(4);
2727 const parStart = performance.now();
2828 const parResults = await Promise.all(items.map((item) => run(heavyWork(item))));
2929 parTime = (performance.now() - parStart).toFixed(0);
+2-2
examples/shared-state/main.ts
···44//
55// Run: node examples/shared-state/main.ts
6677-import { pool, shared, int32, mutex } from '../../src/index.ts';
77+import { workers, shared, int32, mutex } from '../../src/index.ts';
88import { updatePosition } from './update-position.ts';
991010const lock = mutex();
···1313const steps = 1000;
14141515{
1616- using run = pool(4);
1616+ using run = workers(4);
17171818 // 4 workers each move the position (1, 2) per step, 1000 steps each
1919 await Promise.all([
+1-1
src/index.ts
···11export { mo } from './mo.ts';
22export { Task } from './task.ts';
33-export { pool } from './worker-pool.ts';
33+export { workers } from './worker-pool.ts';
44export { transfer } from './transfer.ts';
55export type { Runner } from './runner.ts';
66export {
+6-6
src/worker-pool.ts
···5566const workerEntryUrl = new URL('./worker-entry.ts', import.meta.url);
7788-export function pool(size: number): Runner {
99- const workers: Worker[] = [];
88+export function workers(size: number): Runner {
99+ const pool: Worker[] = [];
1010 for (let i = 0; i < size; i++) {
1111 const worker = new Worker(workerEntryUrl);
1212 worker.unref();
1313 setupWorker(worker);
1414- workers.push(worker);
1414+ pool.push(worker);
1515 }
16161717 let next = 0;
···2020 const run: Runner = Object.assign(
2121 <T>(task: Task<T>): Promise<T> => {
2222 if (disposed) return Promise.reject(new Error('Worker pool is disposed'));
2323- const worker = workers[next % workers.length];
2323+ const worker = pool[next % pool.length];
2424 next++;
2525 return execute<T>(worker, task.id, task.args);
2626 },
2727 {
2828 [Symbol.dispose]() {
2929 disposed = true;
3030- for (const worker of workers) {
3030+ for (const worker of pool) {
3131 worker.terminate();
3232 }
3333- workers.length = 0;
3333+ pool.length = 0;
3434 },
3535 },
3636 );
+2-2
test/error.test.ts
···11import { describe, it } from 'node:test';
22import assert from 'node:assert/strict';
33-import { pool } from 'moroutine';
33+import { workers } from 'moroutine';
44import { fail } from './fixtures/math.ts';
5566describe('error handling', () => {
···1111 });
12121313 it('rejects with error from pool worker', async () => {
1414- const run = pool(1);
1414+ const run = workers(1);
1515 try {
1616 await assert.rejects(() => run(fail('pool boom')), {
1717 message: 'pool boom',
+6-6
test/pool.test.ts
···11import { describe, it } from 'node:test';
22import assert from 'node:assert/strict';
33-import { pool } from 'moroutine';
33+import { workers } from 'moroutine';
44import { double, add } from './fixtures/math.ts';
5566-describe('pool', () => {
66+describe('workers', () => {
77 it('executes a moroutine through the pool', async () => {
88- const run = pool(2);
88+ const run = workers(2);
99 try {
1010 const result = await run(double(2));
1111 assert.equal(result, 4);
···1515 });
16161717 it('handles concurrent calls across pool workers', async () => {
1818- const run = pool(2);
1818+ const run = workers(2);
1919 try {
2020 const results = await Promise.all([run(double(1)), run(double(2)), run(double(3)), run(double(4))]);
2121 assert.deepEqual(results, [2, 4, 6, 8]);
···2525 });
26262727 it('handles multiple argument moroutines', async () => {
2828- const run = pool(1);
2828+ const run = workers(1);
2929 try {
3030 const result = await run(add(10, 20));
3131 assert.equal(result, 30);
···3535 });
36363737 it('dispose terminates pool workers', async () => {
3838- const run = pool(2);
3838+ const run = workers(2);
3939 await run(double(1));
4040 run[Symbol.dispose]();
4141 // After dispose, calls should fail
+10-10
test/shared/cross-worker.test.ts
···11import { describe, it } from 'node:test';
22import assert from 'node:assert/strict';
33-import { pool, int32atomic, mutex, rwlock, shared, int32, string, bytes } from 'moroutine';
33+import { workers, int32atomic, mutex, rwlock, shared, int32, string, bytes } from 'moroutine';
44import { atomicAdd, mutexIncrement, rwlockRead } from '../fixtures/sync.ts';
55import { readValue, writeValue } from '../fixtures/shared-new.ts';
6677describe('shared primitives across workers', () => {
88 it('Int32Atomic is shared across worker threads', async () => {
99 const counter = int32atomic();
1010- const run = pool(2);
1010+ const run = workers(2);
1111 try {
1212 await Promise.all([run(atomicAdd(counter, 10)), run(atomicAdd(counter, 20))]);
1313 assert.equal(counter.load(), 30);
···1919 it('Mutex serializes access across workers', async () => {
2020 const m = mutex();
2121 const counter = int32atomic();
2222- const run = pool(2);
2222+ const run = workers(2);
2323 try {
2424 await Promise.all([run(mutexIncrement(m, counter, 100)), run(mutexIncrement(m, counter, 100))]);
2525 assert.equal(counter.load(), 200);
···3232 const rw = rwlock();
3333 const counter = int32atomic();
3434 counter.store(42);
3535- const run = pool(2);
3535+ const run = workers(2);
3636 try {
3737 const [a, b] = await Promise.all([run(rwlockRead(rw, counter)), run(rwlockRead(rw, counter))]);
3838 assert.equal(a, 42);
···4646 const point = shared({ x: int32, y: int32 });
4747 point.store({ x: 1, y: 2 });
48484949- const run = pool(1);
4949+ const run = workers(1);
5050 try {
5151 const result = await run(readValue(point));
5252 assert.deepEqual(result, { x: 1, y: 2 });
···5858 it('worker can write to struct via shared()', async () => {
5959 const point = shared({ x: int32, y: int32 });
60606161- const run = pool(1);
6161+ const run = workers(1);
6262 try {
6363 await run(writeValue(point, { x: 10, y: 20 }));
6464 assert.deepEqual(point.load(), { x: 10, y: 20 });
···7171 const t = shared([int32, int32]);
7272 t.store([1, 2]);
73737474- const run = pool(1);
7474+ const run = workers(1);
7575 try {
7676 const result = await run(readValue(t));
7777 assert.deepEqual(result, [1, 2]);
···8484 const s = shared(string(32));
8585 s.store('hello');
86868787- const run = pool(1);
8787+ const run = workers(1);
8888 try {
8989 const result = await run(readValue(s));
9090 assert.equal(result, 'hello');
···9797 const b = shared(bytes(4));
9898 b.store(new Uint8Array([1, 2, 3, 4]));
9999100100- const run = pool(1);
100100+ const run = workers(1);
101101 try {
102102 const result = (await run(readValue(b))) as Uint8Array;
103103 assert.deepEqual([...result], [1, 2, 3, 4]);
···110110 const entity = shared({ name: string(16), hp: int32 });
111111 entity.store({ name: 'goblin', hp: 50 });
112112113113- const run = pool(1);
113113+ const run = workers(1);
114114 try {
115115 const result = await run(readValue(entity));
116116 assert.deepEqual(result, { name: 'goblin', hp: 50 });
+5-5
test/transfer.test.ts
···11import { describe, it } from 'node:test';
22import assert from 'node:assert/strict';
33-import { pool, transfer } from 'moroutine';
33+import { workers, transfer } from 'moroutine';
44import { sumBuffer, sumUint8, makeBuffer } from './fixtures/transfer.ts';
5566describe('transfer', () => {
···1212 view[2] = 3;
1313 view[3] = 4;
14141515- const run = pool(1);
1515+ const run = workers(1);
1616 try {
1717 const sum = await run(sumBuffer(transfer(buf)));
1818 assert.equal(sum, 10);
···2727 const arr = new Uint8Array([5, 10, 15, 20]);
2828 const originalBuffer = arr.buffer;
29293030- const run = pool(1);
3030+ const run = workers(1);
3131 try {
3232 const sum = await run(sumUint8(transfer(arr)));
3333 assert.equal(sum, 50);
···3939 });
40404141 it('auto-transfers return values from worker (zero-copy)', async () => {
4242- const run = pool(1);
4242+ const run = workers(1);
4343 try {
4444 const buf = await run(makeBuffer(4));
4545 const view = new Uint8Array(buf);
···5858 view[2] = 3;
5959 view[3] = 4;
60606161- const run = pool(1);
6161+ const run = workers(1);
6262 try {
6363 const sum = await run(sumBuffer(buf));
6464 assert.equal(sum, 10);