Comments (3)
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.
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 (calledwriter_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
- It spins-lock until
- 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
- It spin-locks until
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.
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)
- Option for a resizing bus HOT 4
- Memory leak when idle HOT 3
- Should BusReader require `T: Sync`? HOT 1
- async feature's Stream implementation violates contract HOT 1
- Investigate whether crossbeam types can help HOT 1
- Test it_iterates panics if ran in a loop HOT 3
- Receivers don't always get the broadcasted item HOT 21
- Dynamic number readers? HOT 1
- Problem with rand-core HOT 5
- Async review HOT 1
- recv() cpu abuse HOT 4
- Separate add_rx from Bus HOT 13
- Implement "rendez-vous" mode HOT 1
- Leaky version? HOT 1
- BusReader stops receiving if another BusReader is dropped HOT 3
- Timeout used by park_timeout seems way too short, causes heavy CPU use HOT 13
- How do I use a bus with two threads? HOT 4
- Using a 0 sized buffer causes broadcasts to never be received HOT 13
- Can I cancel a recv after a timeout? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bus.