-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Safe Abstractions for FuriStreamBuffer
#177
Merged
JarvisCraft
merged 17 commits into
flipperzero-rs:main
from
cptpiepmatz:safe-stream-buffer
Nov 14, 2024
Merged
Changes from 6 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
586b24d
initial implementation of safe abstractions for stream buffers
cptpiepmatz a5e0323
started implementing an example for the stream buffer
cptpiepmatz 9b50cab
fixed use of incorrect function
cptpiepmatz ec67af1
updated some docs
cptpiepmatz 628ac5a
finished example
cptpiepmatz 3a78fbb
fmt
cptpiepmatz 54c1cae
handled some nitpicks
cptpiepmatz 666c572
exposed the StreamBuffer type and updated stream pair
cptpiepmatz 8ca373f
typo
cptpiepmatz bc16adc
updated example to use updated stream buffer api
cptpiepmatz 7c761d5
added into_stream_buffer methods
cptpiepmatz 3333284
call it Interrupt Routines everywhere
cptpiepmatz 0a8e60b
updated docs
cptpiepmatz cb95d86
added a brief description of the module
cptpiepmatz 491f549
added error type to make clippy happy
cptpiepmatz 7f02bbd
Merge branch 'main' into safe-stream-buffer
JarvisCraft 8ed6d51
fix: update to latest `Status` API
JarvisCraft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//! Example showcasing the use of a stream buffer on multiple threads. | ||
|
||
#![no_main] | ||
#![no_std] | ||
|
||
// Required for panic handler | ||
extern crate flipperzero_rt; | ||
|
||
// Required for allocator | ||
extern crate flipperzero_alloc; | ||
|
||
extern crate alloc; | ||
|
||
use core::{ffi::CStr, num::NonZeroUsize, time::Duration}; | ||
|
||
use flipperzero::{furi, println}; | ||
use flipperzero_rt::{entry, manifest}; | ||
|
||
// Define the FAP Manifest for this application | ||
manifest!(name = "Stream buffer example"); | ||
|
||
// Define the entry function | ||
entry!(main); | ||
|
||
// Entry point | ||
fn main(_args: Option<&CStr>) -> i32 { | ||
// Create a stream buffer pair | ||
let size = NonZeroUsize::new(1024).unwrap(); | ||
let trigger_level = 16; | ||
let (tx, rx) = furi::stream_buffer::stream_buffer(size, trigger_level); | ||
|
||
// Stream buffer is empty | ||
assert_eq!(tx.spaces_available(), size.into()); | ||
assert_eq!(rx.spaces_available(), size.into()); | ||
assert_eq!(tx.bytes_available(), 0); | ||
assert_eq!(rx.bytes_available(), 0); | ||
|
||
// Sending 4 bytes immediately | ||
assert_eq!(tx.send(&[1, 2, 3, 4]), 4); | ||
assert_eq!(tx.bytes_available(), 4); | ||
|
||
// Receive bytes | ||
let mut recv_buf = [0; 32]; | ||
assert_eq!(rx.recv(&mut recv_buf), 4); | ||
assert_eq!(recv_buf[0..4], [1, 2, 3, 4]); | ||
|
||
// Move sender to another thread | ||
let tx_thread = furi::thread::spawn(move || { | ||
// Wait 2 seconds before we send some bytes | ||
furi::thread::sleep(Duration::from_secs(2)); | ||
assert_eq!(tx.send(&[5; 20]), 20); | ||
|
||
// Send some bytes in a loop to see how the receiver handles them | ||
for i in 4..20 { | ||
furi::thread::sleep(Duration::from_millis(200)); | ||
tx.send(&[i as u8; 3]); | ||
} | ||
|
||
0 | ||
}); | ||
|
||
// Move receiver to another thread | ||
let rx_thread = furi::thread::spawn(move || { | ||
let mut buf = [0; 32]; | ||
|
||
// The sender waits 2 seconds, so we don't block and get no bytes | ||
assert_eq!(rx.recv(&mut buf), 0); | ||
|
||
// The sender sends 20 bytes after two seconds, that is more than the trigger, so we continue | ||
assert_eq!(rx.recv_blocking(&mut buf), 20); | ||
|
||
// Try to receive bytes as long as the sender is alive | ||
while rx.is_sender_alive() { | ||
let n = rx.recv_with_timeout(&mut buf, Duration::from_secs(2)); | ||
println!("got {} bytes: {:?}", n, buf[0..n]); | ||
} | ||
|
||
0 | ||
}); | ||
|
||
tx_thread.join(); | ||
rx_thread.join(); | ||
|
||
0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the following point, but I would prefer a separate
stream-buffer
feature which depends onalloc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels really bloated to a feature just because of the
Arc
I used. I could replace it with some custom counting logic using anAtomicUsize
andsys::malloc
to get a pointer but I'm not sure if that is better.