### Mixed Blocking and Async Sender/Receiver Example Source: https://github.com/frostyplanet/crossfire-rs/blob/master/README.md Demonstrates how to use blocking and asynchronous senders and receivers together with Crossfire's bounded_async channel. Ensure the 'tokio' feature is enabled for this example. ```rust extern crate crossfire; use crossfire::*; #[macro_use] extern crate tokio; use tokio::time::{sleep, interval, Duration}; #[tokio::main] async fn main() { let (tx, rx) = mpmc::bounded_async::(100); let mut recv_counter = 0; let mut co_tx = Vec::new(); let mut co_rx = Vec::new(); const ROUND: usize = 1000; let _tx: MTx> = tx.clone().into_blocking(); co_tx.push(tokio::task::spawn_blocking(move || { for i in 0..ROUND { _tx.send(i).expect("send ok"); } })); co_tx.push(tokio::spawn(async move { for i in 0..ROUND { tx.send(i).await.expect("send ok"); } })); let _rx: MRx> = rx.clone().into_blocking(); co_rx.push(tokio::task::spawn_blocking(move || { let mut count: usize = 0; 'A: loop { match _rx.recv() { Ok(_i) => { count += 1; } Err(_) => break 'A, } } count })); co_rx.push(tokio::spawn(async move { let mut count: usize = 0; 'A: loop { match rx.recv().await { Ok(_i) => { count += 1; } Err(_) => break 'A, } } count })); for th in co_tx { let _ = th.await.unwrap(); } for th in co_rx { recv_counter += th.await.unwrap(); } assert_eq!(recv_counter, ROUND * 2); } ``` -------------------------------- ### Run Project Tests Source: https://github.com/frostyplanet/crossfire-rs/blob/master/README.md Execute the project's test suite using the provided make command. Ensure you are in the test-suite directory. ```bash make test ``` -------------------------------- ### Run Crossfire Benchmarks Source: https://github.com/frostyplanet/crossfire-rs/blob/master/README.md Execute the benchmark suite for Crossfire. Use 'make bench crossfire' for general benchmarks and 'make bench crossfire_select' for select-related benchmarks. ```shell make bench crossfire ``` ```shell make bench crossfire_select ``` -------------------------------- ### Scenario Analysis Without Init State Source: https://github.com/frostyplanet/crossfire-rs/wiki/state-transfer Illustrates a potential race condition and deadlock scenario in V2.0 without the 'Init' state, when using a bounded channel of size 1. This sequence of events led to the addition of the 'Init' state. ```text (1): thread1(sender) try_send failed (2): thread1(sender) reg_send_async (waker1) (3): thread1(sender) while double check with try_send SUCCESS, but waker is still in the sender Registry. (4): thread2(sender) reg_send_async (waker2) (5): thread2(sender) try_send failed (channel is full). (6): thread3(receiver) try_recv SUCCESS, (channel is empty) (7): thread3(receiver) on_recv, saw waker1. (waked=false) (8): thread3(receiver) waker1.wake() and returned. waked=true. (9): thread1(sender) tried to cancel waker1, which is registered from (2), but it has already been consumed in (7). (10): No more receivers will call try_recv() again, ``` -------------------------------- ### Bounded Channel Send Logic Pseudocode Source: https://github.com/frostyplanet/crossfire-rs/wiki/crossbeam-related Illustrates the core loop for sending a value into a bounded channel. It includes logic for attempting to send, handling backoff on contention, and managing the channel's full state by parking the thread if necessary. ```pseudocode 'MAIN: loop { for some backoff retry { if start_send() { // content with a value position to write write_value; update_stamp; return; } backoff; } register_waker(); //examine the channel full condition. if is_full() { park() } else { cancel_waker(); continue 'MAIN; } } ``` -------------------------------- ### CPU Information Source: https://github.com/frostyplanet/crossfire-rs/wiki/benchmark-v2.1.3-Arm-2025.09.29 Displays CPU details for the benchmarking environment. This information is useful for understanding the hardware characteristics influencing benchmark results. ```text CPU implementer : 0x41 CPU architecture: 8 CPU variant : 0x3 CPU part : 0xd0c CPU revision : 1 ``` -------------------------------- ### Enable Compat Model Feature Flag Source: https://github.com/frostyplanet/crossfire-rs/blob/master/README.md Enable the 'compat' feature flag to use the V2.x API namespace struct. ```toml [dependencies.crossfire] version = "3.1" features = ["compat"] ``` -------------------------------- ### Enable Logging for Deadlock Debugging Source: https://github.com/frostyplanet/crossfire-rs/blob/master/README.md Run benchmarks or tests with the `trace_log` feature enabled to capture logs when a deadlock occurs. Press Ctrl+C or send SIGINT to trigger a log dump to /tmp/crossfire_ring.log. ```bash --features trace_log ``` -------------------------------- ### Add Crossfire Dependency to Cargo.toml Source: https://github.com/frostyplanet/crossfire-rs/blob/master/README.md Include this in your Cargo.toml file to add the Crossfire crate as a dependency. ```toml [dependencies] crossfire = "3.1" ``` -------------------------------- ### Enable Trace Log Feature Flag for Development Source: https://github.com/frostyplanet/crossfire-rs/blob/master/README.md Enable the 'trace_log' feature flag for development mode to activate internal logging for testing or benchmarking, particularly for debugging deadlock issues. ```toml [dependencies.crossfire] version = "3.1" features = ["trace_log"] ``` -------------------------------- ### Enable Tokio Feature Flag for Async Operations Source: https://github.com/frostyplanet/crossfire-rs/blob/master/README.md Enable the 'tokio' feature flag to use `send_timeout()` and `recv_timeout()` with Tokio's sleep function. This feature conflicts with the 'async_std' feature. ```toml [dependencies.crossfire] version = "3.1" features = ["tokio"] ``` -------------------------------- ### ArrayQueue Full Condition Check Source: https://github.com/frostyplanet/crossfire-rs/wiki/crossbeam-related This code snippet demonstrates how to check if an ArrayQueue is full by comparing the tail and head pointers. It's used in the modified ArrayQueue to double-check the full condition before attempting a send. ```rust let tail = self.tail.load(Ordering::SeqCst); let head = self.head.load(Ordering::SeqCst); if head.wrapping_add(self.one_lap) == $tail { // full... } ``` -------------------------------- ### Enable Async-Std Feature Flag for Async Operations Source: https://github.com/frostyplanet/crossfire-rs/blob/master/README.md Enable the 'async_std' feature flag to use `send_timeout()` and `recv_timeout()` with async-std's sleep function. This feature conflicts with the 'tokio' feature. ```toml [dependencies.crossfire] version = "3.1" features = ["async_std"] ``` -------------------------------- ### ArrayQueue Structure Definition Source: https://github.com/frostyplanet/crossfire-rs/wiki/crossbeam-related Defines the `Slot` and `ArrayQueue` structs used in the crossbeam algorithm's array-based queue implementation. It details the atomic fields for managing queue state and the buffer for storing elements. ```rust struct Slot { /// The current stamp. /// If the stamp equals the tail, this node will be next written to. If it equals head + 1, /// this node will be next read from. stamp: AtomicUsize, /// The value in this slot. value: UnsafeCell>, } pub struct ArrayQueue { /// The head of the queue. /// /// This value is a "stamp" consisting of an index into the buffer and a lap, but packed into a /// single `usize`. The lower bits represent the index, while the upper bits represent the lap. /// /// Elements are popped from the head of the queue. head: CachePadded, /// The tail of the queue. /// /// This value is a "stamp" consisting of an index into the buffer and a lap, but packed into a /// single `usize`. The lower bits represent the index, while the upper bits represent the lap. /// /// Elements are pushed into the tail of the queue. tail: CachePadded, /// The buffer holding slots. buffer: Box<[Slot]>, /// A stamp with the value of `{ lap: 1, index: 0 }`. one_lap: usize, } ``` -------------------------------- ### WakerState Enum Definition Source: https://github.com/frostyplanet/crossfire-rs/wiki/state-transfer Defines the possible states for a waker in the Crossfire-RS library. Note that the 'Copy' state has been omitted due to the disabling of direct copy in certain asynchronous or deadline-based scenarios. ```rust pub enum WakerState { Init = 0, // A temporary state, https://github.com/frostyplanet/crossfire-rs/issues/22 Waiting = 1, //Copy = 2, // Omit due to skipping direct copy on async or with deadline Waked = 3, Closed = 4, // Channel closed, or timeout cancellation Done = 5, } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.