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.

feat(shared): add RwLock.tryWriteLock synchronous variant

+39
+11
src/shared/rwlock.ts
··· 106 106 } 107 107 } 108 108 109 + /** 110 + * Attempts to acquire a write lock without waiting. 111 + * @returns A disposable {@link WriteGuard} if acquired, otherwise `null`. 112 + */ 113 + tryWriteLock(): WriteGuard | null { 114 + if (Atomics.compareExchange(this.view, 0, UNLOCKED, WRITE_LOCKED) === UNLOCKED) { 115 + return new WriteGuard(this); 116 + } 117 + return null; 118 + } 119 + 109 120 /** Releases the write lock and wakes all waiting threads. */ 110 121 writeUnlock(): void { 111 122 Atomics.store(this.view, 0, UNLOCKED);
+28
test/shared/rwlock.test.ts
··· 150 150 assert.equal(guard, null); 151 151 rw.writeUnlock(); 152 152 }); 153 + 154 + it('tryWriteLock returns a guard when unlocked', () => { 155 + const rw = rwlock(); 156 + const guard = rw.tryWriteLock(); 157 + assert.ok(guard); 158 + assert.equal(typeof guard[Symbol.dispose], 'function'); 159 + guard[Symbol.dispose](); 160 + // Should be able to re-acquire after dispose (proves writeUnlock was called) 161 + const guard2 = rw.tryWriteLock(); 162 + assert.ok(guard2); 163 + rw.writeUnlock(); 164 + }); 165 + 166 + it('tryWriteLock returns null when read-locked', async () => { 167 + const rw = rwlock(); 168 + await rw.readLock(); 169 + const guard = rw.tryWriteLock(); 170 + assert.equal(guard, null); 171 + rw.readUnlock(); 172 + }); 173 + 174 + it('tryWriteLock returns null when write-locked', async () => { 175 + const rw = rwlock(); 176 + await rw.writeLock(); 177 + const guard = rw.tryWriteLock(); 178 + assert.equal(guard, null); 179 + rw.writeUnlock(); 180 + }); 153 181 });