Git Product home page Git Product logo

Comments (3)

jonhoo avatar jonhoo commented on August 30, 2024

Hmm, that's an interesting feature... It'd be pretty tricky to add, because the value may already have gone out to some readers, and others may be accessing it concurrently. I think doing this in a race-free way would be pretty tough. If you have an idea for how to do it though, I'd be happy to take a look at a PR!

from bus.

gyscos avatar gyscos commented on August 30, 2024

I'm still reading through the code, so I have an incomplete view, but I think it would go like that:

  • We need an atomic counter (called num_readers) and an atomic bool (called writer_waiting).
  • When a reader wants to receive a value:
    • It spins-lock until writer_waiting is false
    • It increases num_readers
    • If the head pointer is higher than the reader pointer, update the reader pointer to the head pointer.
    • It copies the value out of the bus
    • It decreases num_readers
  • When a writer wants to send a value and the bus is full:
    • It spin-locks until writer_waiting.compare_and_swap(false, true) returns false
    • It spin-lock until num_readers is 0 (should be the duration of a few memcopys)
    • It overwrites the oldest value
    • It updates the head/tail pointers
    • It sets writer_waiting to false

The counter and bool might be replaced by a RWLock.

This ensures that no-one is reading the cell we are overwriting, and limits the time the writer waits (to the time taken by mem-copying the value out of the bus by the outstanding readers).

I'm not 100% sure it should share the code of the current implementation: it risk adding complexity/overhead to cases where it's not needed, while not benefiting from features of the current implementation. :S

from bus.

jonhoo avatar jonhoo commented on August 30, 2024

Hmm, yeah, I'd worry that this would end up adding significant contention for both reads and writes, which is something the current implementation tries very hard to avoid. It could be that it should be its own implementation that we can optimize separately?

from bus.

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.