npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-synch

v1.0.1

Published

Synchronization Primitives in JavaScript

Downloads

16

Readme

node-synch

Build Coverage

Synchronization Primitives in JavaScript

/* Main thread */
import { Sema, Lock } from 'node-synch';

const sema = new Sema();
const lock = new Lock();

const worker = new Worker(...);

worker.postMessage({ sema, lock });

// Do something with the locks



/* Worker thread */
import { Sema, Lock } from 'node-synch';
import { mainThread } from 'worker_threads';

...
mainThread.on('message', ({ sema, lock }) => {
  sema = Sema.from(sema);
  lock = Lock.from(lock);
  
  // Do something with the locks
});

Installation

npm install node-synch

Features

  • Synchronization Primitives
  • Browser support
  • Synchronous and Asynchronous methods

Table of Contents

Class: Sema

The Sema class represents a single instance of a semaphore. A semaphore is a synchronization primitive that keeps an internal counter. On attempting to decrement past 0, the decrementing thread is put to sleep until this counter is incremented again. To use semaphores in both a worker thread and the main thread, this class must be imported in both locations.

Sema.from(sema)

  • sema <Sema> A Sema object or a structuredClone Sema object to create a shared clone
  • Returns: <Sema> A Sema object representing a shared copy of the semaphore.
/* Main Thread */
import { Sema } from 'node-synch';

const sema = new Sema();

const worker = new Worker(..., {
  workerData: { sema }
});

await sema.down();
...
sema.up();



/* Worker Thread */
import { Sema } from 'node-synch';
import { workerData } from 'worker_threads';

let { sema } = workerData;

sema = Sema.from(sema);

await sema.down();
...
sema.up();

new Sema(init)

  • init <number> An optional non-negative Int-32 number representing the internal counter of the semaphore. This is used to determine the semaphore's value. Default: 1.

sema.down()

Attempts to decrease the semaphore's internal counter. This will sleep the thread if counter is at 0. Unlike sema.downSync(), this method is asynchronous and non-blocking. Returns Promise that resolves to true.

import { Sema } from 'node-synch';

const sema = new Sema();

await sema.down();

... // Critical section

sema.up();

sema.downSync()

Attempts to decrease the semaphore's internal counter. This will sleep the thread if counter is at 0. Unlike sema.down(), this method is synchronous and blocking. Returns true.

import { Sema } from 'node-synch';

const sema = new Sema();

sema.downSync();

... // Critical section

sema.up();

sema.up()

Increases the internal counter of the semaphore. Wakes up threads that are waiting on the semaphore. Returns true.

sema.value()

Returns the current value of the semaphore.

sema.clone()

Returns a shared copy of the semaphore.

sema.toString()

Returns the string format for the semaphore.

Class: Lock

The Lock class represents a single instance of a lock. To use locks in both a worker thread and the main thread, this class must be imported in both locations. Unlike Lock Web API, this lock synchronization primitive stalls threads that attempt to hold it when it is held by another thread. Once unlocked, the waiting threads are woken up.

Lock.from(lock[, tid])

  • lock <Lock> A Lock object or a structuredClone Lock object to create a shared clone
  • tid <number> An optional non-negative Int-32 number representing the tid of the lock. This is used to determine the holder when the lock is acquired. If not specified, the lock keeps an internal counter which it uses to determine the tid of the shared cloned Lock. Default: Next internal lock number.
  • Returns: <Lock> A Lock object representing a shared copy of the lock.
/* Main Thread */
import { Lock } from 'node-synch';

const lock = new Lock();

const worker = new Worker(..., {
  workerData: { lock }
});

await lock.acquire();
...
lock.release();



/* Worker Thread */
import { Lock } from 'node-synch';
import { workerData, threadId } from 'worker_threads';

let { lock } = workerData;

lock = Lock.from(lock, threadId);

await lock.acquire();
...
lock.release();

new Lock([tid])

  • tid <number> An optional non-negative Int-32 number representing the tid of the lock. This is used to determine the holder when the lock is acquired. Default: 0.

lock.acquire()

Attempts to acquire the lock with the internal tid. This will sleep the thread if the lock is already held by a different tid. Unlike lock.acquireSync(), this method is asynchronous and non-blocking. Returns Promise that resolves to true if the lock is currently not held, false if the lock is already held by the current tid.

import { Lock } from 'node-synch';

const lock = new Lock();

await lock.acquire();

... // Critical section

lock.release();

lock.acquireSync()

Attempts to acquire the lock with the internal tid. This will sleep the thread if the lock is already held by a different tid. Unlike lock.acquire(), this method is synchronous and blocking. Returns true if the lock is currently not held, false if the lock is already held by the current tid.


import { Lock } from 'node-synch';

const lock = new Lock();

lock.acquireSync();

... // Critical section

lock.release();

lock.release()'

Attempts to release the lock. Threads that are currently waiting on this lock are woken up. Returns true if the lock is held by the current thread, false otherwise.

lock.isLocked()

Returns true if the lock is held by a thread, false otherwise.

lock.holder()

Returns the tid of the thread holding the lock, -1 otherwise.

lock.tid()

Returns the internal tid of the lock clone/instance.

lock.clone()

Returns a shared copy of the lock.

lock.toString()

Returns the string format for the lock.