polyfractal / bounded-spsc-queue Goto Github PK
View Code? Open in Web Editor NEWA Bounded SPSC queue for Rust
License: Apache License 2.0
A Bounded SPSC queue for Rust
License: Apache License 2.0
Classic nightly, moving out from under you all the time. Anyway, this fails to build again on 1.30.
error[E0425]: cannot find function oom in module alloc
--> ...\github.com-1ecc6299db9ec823\bounded-spsc-queue-0.3.0\src\lib.rs:351:260.3.0\src\lib.rs:351:26
|
351 | Err(_) => alloc::oom(),
| ^^^ not found in alloc
Compiling winapi v0.3.6 e
error[E0599]: no method named as_opaque found for type std::ptr::NonNull<T> in the current scop0.3.0\src\lib.rs:256:40e
--> ...\github.com-1ecc6299db9ec823\bounded-spsc-queue-0.3.0\src\lib.rs:256:40
|
256 | Global.dealloc(self.buffer.as_opaque(), layout);
| ^^^^^^^^^
error: aborting due to 2 previous errors
Some errors occurred: E0425, E0599.
Specifically:
UnsafeCell
can be used to emulate !Sync
Vec
may be used instead of manually using buffer
+ capacity
+ allocated_size
(and depending on liballoc)Currently, if you try to request too much memory for the queue and allocation fails, we throw an OOM panic. That's pretty unfriendly :)
Instead, we should return a Result
with the proper error so the user can decide what to do.
I'm getting into realtime embedded systems, and it would be fantastic to use your queue with a stack-allocated array. If we could pass in a pointer or a mutable slice or something along those lines that would basically do it for us. (This also seems like something that could totally be feature-gated, as it's probably not particularly often-used.)
The line in question:
unsafe impl<T: Sync> Sync for Buffer<T> {}
So, as far as I know this should line should be removed. Buffer inherits !Send and !Sync from the *mut T. Marking it as Sync is unsafe because it requires Buffer to implement thread safety itself, from any number of threads accessing any of its functions. Buffer itself is obviously not safe to be marked as Sync because try_push/try_pop for example will have inconsistencies if multiple threads call either one at once. Buffer's logic only guarantees one thread calling the consumer side (try_pop) of things and one thread calling the producer side (try_push) of things.
Arc is only Send and Sync if the underlying type being wrapped is also Send and Sync. The unsafe Sync marker on Buffer does not change the auto traits of Arc because since Buffer is not Send, Arc can be neither Send or Sync.
On top of all of this, Buffer should not be marked as pub
in the first place since you cannot directly access it anyway.
In low-latency applications, it's often desirable to drop old items from the queue in favor of newer items. A suitable interface might be something like:
fn force_push(&self, v: T) -> option<T>
where the return value is None
or, if the oldest element was dropped, Some(old_value)
.
Such a function would be non-blocking, of course.
Right now Producer and Consumer are Sync, which allows the user to call try_{push, pop}
from multiple threads at once, thus violating the single-consumer-single-producer property and possibly causing memory unsafety.
cc @gankro didn't see anything else obvious
I'm working on some audio programming applications where I'd actually like to drop the old data in the queue (and still push), rather than drop the new stuff (and never push it). Would it be possible to implement a version of this method on the Producer
?
This is a very common application in audio programming -- we always want to update the buffer with the newest data, even if no one's listening -- so it would be great to have available in a library like this one.
Looks like the unstable heap API updated.
error[E0308]: mismatched types
--> C:\Users\Mooma\.cargo\registry\src\github.com-1ecc6299db9ec823\bounded-spsc-queue-0.2.0\src\lib.rs:262:26
|
262 | Heap.dealloc(self.buffer as *mut u8, layout);
| ^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::ptr::NonNull`, found *-ptr
|
= note: expected type `std::ptr::NonNull<std::heap::Opaque>`
found type `*mut u8`
error[E0599]: no method named `oom` found for type `std::heap::Global` in the current scope
--> C:\Users\Mooma\.cargo\registry\src\github.com-1ecc6299db9ec823\bounded-spsc-queue-0.2.0\src\lib.rs:350:24
|
350 | Err(e) => Heap.oom(e),
| ^^^
error[E0605]: non-primitive cast: `std::ptr::NonNull<std::heap::Opaque>` as `*mut T`
--> C:\Users\Mooma\.cargo\registry\src\github.com-1ecc6299db9ec823\bounded-spsc-queue-0.2.0\src\lib.rs:349:20
|
349 | Ok(ptr) => ptr as *mut T,
| ^^^^^^^^^^^^^
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
error: aborting due to 3 previous errors
Some errors occurred: E0308, E0599, E0605.
For more information about an error, try `rustc --explain E0308`.
error: Could not compile `bounded-spsc-queue`.
warning: build failed, waiting for other jobs to finish...
error: build failed
Currently it seems to crash at runtime with no error message when trying to create a channel of zero-sized types (such as unit ()
).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.