Offload functions to worker threads with shared memory primitives for Node.js.
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

at e2cebd539403475e2f4e79d55ca1a84a0ce3510d 25 lines 749 B view raw
1import { mo } from 'moroutine'; 2import type { Int32Atomic, Mutex, RwLock } from 'moroutine'; 3 4export const atomicAdd = mo(import.meta, (counter: Int32Atomic, value: number): number => { 5 return counter.add(value); 6}); 7 8export const mutexIncrement = mo( 9 import.meta, 10 async (mutex: Mutex, counter: Int32Atomic, times: number): Promise<void> => { 11 for (let i = 0; i < times; i++) { 12 const guard = await mutex.lock(); 13 const val = counter.load(); 14 counter.store(val + 1); 15 mutex.unlock(); 16 } 17 }, 18); 19 20export const rwlockRead = mo(import.meta, async (rwlock: RwLock, counter: Int32Atomic): Promise<number> => { 21 const guard = await rwlock.readLock(); 22 const val = counter.load(); 23 rwlock.readUnlock(); 24 return val; 25});