Giter Site home page Giter Site logo

Comments (8)

CMCDragonkai avatar CMCDragonkai commented on June 2, 2024 1

I've written a write-preferring RWLock. https://gist.github.com/CMCDragonkai/4de5c1526fc58dac259e321db8cf5331

from async-mutex.

DirtyHairy avatar DirtyHairy commented on June 2, 2024

A RW lock is a possibility, but would most likely by a separate mutex class. Do you have a usecase in mind?

from async-mutex.

CMCDragonkai avatar CMCDragonkai commented on June 2, 2024

This was my attempt using async-mutex. It's read-preferring.

from async-mutex.

CMCDragonkai avatar CMCDragonkai commented on June 2, 2024

https://gist.github.com/CMCDragonkai/4de5c1526fc58dac259e321db8cf5331

from async-mutex.

DirtyHairy avatar DirtyHairy commented on June 2, 2024

Sorry for my late response, life came in the way and I forgot to reply 😏 Yes, that implementation looks fine and valid. Actually, I like it better than the thought of adding it to the library. If that is fine for you I'll close the issue.

from async-mutex.

CMCDragonkai avatar CMCDragonkai commented on June 2, 2024

I was wondering how to do a write-preferring RWLock, the wikipedia article gives an method using condition variables, but we don't have this here.

A write-preferring one should mean that when a write call is done, it should block and queue up all the reads.

from async-mutex.

DirtyHairy avatar DirtyHairy commented on June 2, 2024

That's an interesting exercise 😏 I think the following algorithm with two mutexes and two counters should work:

Reader:

  1. Lock write mutex
  2. Check read counter, lock read mutex if it is zero
  3. Increment read counter
  4. Release write mutex
  5. ... [do async stuff]
  6. Decrement read counter
  7. Check read counter, release read mutex if it is zero

Writer:

  1. Check write counter, lock write mutex if it is zero
  2. Increment write counter
  3. Lock read mutex
  4. ... [do async stuff]
  5. Release read mutex
  6. Decrement write counter
  7. Check write counter, release write mutex if it zero

from async-mutex.

CMCDragonkai avatar CMCDragonkai commented on June 2, 2024

Finished up here: https://github.com/MatrixAI/js-async-locks

from async-mutex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.