sharded_thread/lib.rs
1//! `sharded_thread` is a module to provides any runtime efficient
2//! channels-like abstractions.
3
4/// A mesh to connect multiple executor together.
5///
6/// A mesh is composed of multiple peers and a way to communicate between each
7/// of these peers. Everyone is the mesh can communicate with the others peer.
8///
9/// # Examples
10///
11/// A thread-per-core architecture with `monoio`.
12///
13/// ```rust
14/// use sharded_thread::{mesh::MeshBuilder, shard::Shard};
15/// use futures::StreamExt;
16/// use std::sync::Arc;
17///
18/// cfg_if::cfg_if! {
19/// if #[cfg(target_os = "linux")] {
20/// type Driver = monoio::IoUringDriver;
21/// } else {
22/// type Driver = monoio::LegacyDriver;
23/// }
24/// }
25///
26/// // Message to send
27/// type Msg = usize;
28///
29/// let cpus = 4;
30/// let mesh = Arc::new(MeshBuilder::<Msg>::new(cpus).unwrap());
31///
32/// let mut handles = Vec::new();
33/// for peer in 0..cpus {
34/// let mesh = mesh.clone();
35/// let handle = std::thread::spawn(move || {
36/// // We lock the thread for the core
37/// monoio::utils::bind_to_cpu_set(Some(peer)).unwrap();
38///
39/// let mut rt = monoio::RuntimeBuilder::<Driver>::new()
40/// .with_entries(1024)
41/// .enable_timer()
42/// .build()
43/// .expect("Cannot build runtime");
44///
45/// let shard: Shard<Msg> = mesh.join_with(peer).unwrap();
46///
47/// rt.block_on(async move {
48/// let handle = monoio::spawn(async move {
49/// let mut receiver = shard.receiver().unwrap();
50///
51/// // We send it unchecked because we are not sure every shard joined when we send
52/// // it.
53/// // Even if the shard did not join, we can buffer it inside the internal channel.
54/// // It's not unsafe, it's unchecked.
55/// shard.send_to_unchecked(peer, (peer + 1) % cpus);
56///
57/// while let Some(val) = receiver.next().await {
58/// println!("Received {val} on CPU {peer}");
59/// // In this example we break at the begining
60/// return ();
61/// }
62/// });
63/// handle.await
64/// })
65/// });
66///
67/// handles.push(handle);
68/// }
69///
70/// for handle in handles {
71/// handle.join();
72/// }
73/// ```
74pub mod mesh;
75pub(crate) mod queue;
76
77/// Sharding utilities built on top of a mesh.
78pub mod shard;