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

@wicle/mutex

v0.9.0

Published

Simple `Mutex` and `Semaphore` implementation written in TypeScript.

Downloads

8

Readme

@wicle/mutex

Simple Mutex and Semaphore implementation written in TypeScript.

Semaphore uses Promise to implement posix semaphore functions, wait, post, and get value.

Mutex uses Semaphore to implement posix mutex funtions, lock, unlock, and tryLock.

Installation

# npm
npm i '@wicle/mutex'

# yarn
yarn add '@wicle/mutex'

# pnpm
pnom add '@wicle/mutex'

Usage

Semaphore

import { Semaphore } from 'mutex'

const sem = new Semaphore()
setTimeout(() => { sem.release() }, 1000)

console.time('semaphore wait')
await sem.wait()
console.timeEnd('semaphore wait')

Mutex

import { Mutex } from 'mutex'

const mtx = new Mutex()
let crit_data = 1

console.log('Access crit data #1')
await mtx.lock()
setTimeout(() => { crit_data = 2; mtx.unlock() }, 1000)

console.log('Access crit data #2')
await mtx.lock()
console.log('crit_data = ', crit_data)
mtx.unlock()

API

Semaphore

constructor

const sem = new Semaphore([value]) Create a new Semaphore instance. value is number of available resources, whcich defaults to 0.

sem.wait()

POSIX semaphore interface for sem_wait(). Returns a promise waiting for the semaphore to be signaled (resource available). Use await or Proimise.then() to wait for the signal.

await sem.wait()
// your code...

// or
sem.wait().then(()=>{
    // your code...
})

sem.tryWait()

POSIX semaphore interface for sem_trywait(). Check if tghe semaphore is signaled without waiting. Returns true if the semaphoire is in signaled state, or false

sem.post()

POSIX semaphore interface for sem_post(). Alias fore release() Returns void

sem.value

Semapohore value, whichi means resouce count currently available or signaled count.

sem.aquire()

Aquire semaphore resource. Alias to sem.wait(). Use await or Proimise.then() to wait for the resource to be aquired.

sem.tryAquire()

Try aquiring semaphore resource. returns immediately if the access not available. Alias to sem.tryWait(). Returns true if the access aquired, or false.

sem.release()

Release semaphore resource. Alias to sem.post().

Mutex

constructor

const mtx = new Mutex() Create a new Mutex instance.

mtx.lock()

Get access to the exclusive resource. Returns Promise to get the access the resource (lock). Use await or Proimise.then() to wait for the resource to be available.

await mtx.lock()
// your code...

// or
mtx.lock().then(()=>{
    // your code...
})

mtx.tryLock()

Try to get access to the exclusive resource without waiting. Returns true if the aquired, or false.

mtx.unlock()

Unlock the exclusive resource.

License

MIT