Coder Social home page Coder Social logo

Comments (1)

wishawa avatar wishawa commented on July 22, 2024

When merging N streams, we can either

  • poll each of them every time, or
  • poll only the ones that have are ready to be polled.

We go with the second approach (because when N is huge, the first can become quite slow).

This requires keeping track of which streams are ready to be polled. Each stream tell us that it is ready by waking the waker we passed to it when we polled it last time. So when one of our wakers is woken, we need to record that the associated stream is ready to be polled again.

The way Rust's async is designed, wakers can be woken from anywhere at anytime. It doesn't matter what is going on with the Merge; one stream might have sent a waker to a different thread and that thread can wake it at will.

So the state where we record streams' readiness needs to be accessible from any thread at anytime. Hence the Mutex.

That said, the Mutex is not absolutely required; we can switch WakerArray from having an array of bool inside a Mutex to using an array of AtomicBool.
PR #115, will make things harder (as WakerArray will then contain a list of woken futures/streams), but even then avoiding Mutex is still possible by use of a non-blocking linked list or something similar.

from futures-concurrency.

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.