### Perform Direct I/O with DmaFile Source: https://context7.com/datadog/glommio/llms.txt Demonstrates how to bypass the OS page cache for predictable performance using DmaFile. Includes examples for stream writing, stream reading, and aligned random access reads. ```rust use glommio::{io::{DmaFile, DmaStreamWriterBuilder, DmaStreamReaderBuilder}, LocalExecutor}; use futures_lite::{AsyncWriteExt, AsyncReadExt}; fn main() { let ex = LocalExecutor::default(); ex.run(async { let file = DmaFile::create("test.dat").await.unwrap(); let mut writer = DmaStreamWriterBuilder::new(file) .with_buffer_size(128 << 10) .with_write_behind(4) .build(); for _ in 0..1000 { writer.write_all(&[1u8; 4096]).await.unwrap(); } writer.close().await.unwrap(); let file = DmaFile::open("test.dat").await.unwrap(); let mut reader = DmaStreamReaderBuilder::new(file) .with_buffer_size(128 << 10) .with_read_ahead(4) .build(); let mut buf = vec![0u8; 4096]; let n = reader.read(&mut buf).await.unwrap(); println!("Read {} bytes", n); reader.close().await.unwrap(); let file = DmaFile::open("test.dat").await.unwrap(); let result = file.read_at_aligned(0, 4096).await.unwrap(); println!("Random read: {} bytes", result.len()); file.close().await.unwrap(); }); } ``` -------------------------------- ### Perform Buffered I/O with BufferedFile Source: https://context7.com/datadog/glommio/llms.txt Shows how to utilize the OS page cache for standard file operations. Provides examples for sequential writing, reading, and random access. ```rust use glommio::{ io::{BufferedFile, StreamWriterBuilder, StreamReaderBuilder}, LocalExecutor }; use futures_lite::{AsyncWriteExt, AsyncReadExt}; fn main() { let ex = LocalExecutor::default(); ex.run(async { let file = BufferedFile::create("buffered.txt").await.unwrap(); let mut writer = StreamWriterBuilder::new(file).build(); writer.write_all(b"Hello, Buffered I/O!\n").await.unwrap(); writer.close().await.unwrap(); let file = BufferedFile::open("buffered.txt").await.unwrap(); let mut reader = StreamReaderBuilder::new(file).build(); let mut contents = String::new(); reader.read_to_string(&mut contents).await.unwrap(); println!("Contents: {}", contents); reader.close().await.unwrap(); let file = BufferedFile::open("buffered.txt").await.unwrap(); let data = file.read_at(0, 5).await.unwrap(); println!("First 5 bytes: {:?}", data); file.close().await.unwrap(); }); } ``` -------------------------------- ### Configure Task Queues with Priority Scheduling in Rust Source: https://context7.com/datadog/glommio/llms.txt Demonstrates how to create task queues with specific CPU shares and latency requirements. This allows developers to prioritize critical tasks over background work within the Glommio executor. ```rust use glommio::{LocalExecutor, Latency, Shares}; use std::time::Duration; fn main() { let ex = LocalExecutor::default(); ex.run(async { let high_priority = glommio::executor().create_task_queue( Shares::Static(2000), Latency::Matters(Duration::from_millis(1)), "high-priority", ); let low_priority = glommio::executor().create_task_queue( Shares::Static(1000), Latency::NotImportant, "low-priority", ); let t1 = glommio::spawn_local_into(async { println!("High priority task"); }, high_priority).unwrap(); let t2 = glommio::spawn_local_into(async { println!("Low priority task"); }, low_priority).unwrap(); futures::join!(t1, t2); }); } ``` -------------------------------- ### Spawn and Manage Asynchronous Tasks Source: https://context7.com/datadog/glommio/llms.txt Illustrates how to spawn tasks on the current executor using spawn_local. It covers awaiting individual tasks, joining multiple tasks concurrently, and running background tasks using detach. ```rust use glommio::{LocalExecutor, Task}; use futures::future::join_all; fn main() { let ex = LocalExecutor::default(); ex.run(async { // Spawn a task and await it let task: Task = glommio::spawn_local(async { 1 + 2 }); assert_eq!(task.await, 3); // Spawn multiple tasks let mut tasks = vec![]; for i in 0..5 { tasks.push(glommio::spawn_local(async move { println!("Task {}", i); i * 2 })); } let results: Vec = join_all(tasks).await; println!("Results: {:?}", results); // Detached task runs in background glommio::spawn_local(async { println!("Background task"); }).detach(); }); } ``` -------------------------------- ### Initialize and Configure LocalExecutor in Glommio Source: https://context7.com/datadog/glommio/llms.txt Demonstrates how to initialize a default LocalExecutor or a custom one with specific CPU pinning, I/O memory allocation, and io_uring ring depth. This is the fundamental building block for running async tasks in a thread-per-core model. ```rust use glommio::{LocalExecutor, LocalExecutorBuilder, Placement}; fn main() -> std::io::Result<()> { // Simple default executor let ex = LocalExecutor::default(); ex.run(async { println!("Hello from Glommio!"); }); // Executor pinned to CPU 0 let ex = LocalExecutorBuilder::new(Placement::Fixed(0)) .name("my-executor") .io_memory(20 << 20) // 20 MiB for I/O buffers .ring_depth(256) // io_uring submission queue depth .make()?; ex.run(async { println!("Running on CPU 0!"); }); Ok(()) } ``` -------------------------------- ### Perform Async UDP Networking in Rust Source: https://context7.com/datadog/glommio/llms.txt Demonstrates how to use UdpSocket for datagram communication, including binding, sending, receiving, and connecting to specific endpoints using Glommio's async API. ```rust use glommio::{net::UdpSocket, LocalExecutor}; fn main() { let ex = LocalExecutor::default(); ex.run(async { let socket = UdpSocket::bind("127.0.0.1:9000").unwrap(); let target = "127.0.0.1:9001".parse().unwrap(); socket.send_to(b"Hello UDP!", target).await.unwrap(); let mut buf = [0u8; 1024]; let (len, addr) = socket.recv_from(&mut buf).await.unwrap(); socket.connect(target).await.unwrap(); socket.send(b"Connected message").await.unwrap(); }); } ``` -------------------------------- ### Monitor Executor and I/O Statistics Source: https://context7.com/datadog/glommio/llms.txt Shows how to retrieve runtime metrics for the executor, specific task queues, and I/O rings. This is essential for debugging performance bottlenecks and monitoring throughput in high-load applications. ```rust use glommio::{LocalExecutor, Shares, Latency}; fn main() { let ex = LocalExecutor::default(); ex.run(async { let tq = glommio::executor().create_task_queue( Shares::default(), Latency::NotImportant, "my-queue" ); glommio::spawn_local_into(async { for _ in 0..1000 { glommio::yield_if_needed().await; } }, tq).unwrap().await; let stats = glommio::executor().executor_stats(); println!("Executor runtime: {:?}", stats.executor_runtime()); println!("Tasks executed: {}", stats.tasks_executed()); println!("Scheduler runs: {}", stats.scheduler_runs()); let tq_stats = glommio::executor().task_queue_stats(tq).unwrap(); println!("Queue runtime: {:?}", tq_stats.runtime()); println!("Queue selected: {} times", tq_stats.queue_selected()); let io_stats = glommio::executor().io_stats(); let all_rings = io_stats.all_rings(); println!("Files opened: {}", all_rings.files_opened()); println!("File reads: {:?}", all_rings.file_reads()); }); } ``` -------------------------------- ### Create Async TCP Server and Client in Rust Source: https://context7.com/datadog/glommio/llms.txt Utilizes Glommio's io_uring-backed TcpListener and TcpStream to implement an asynchronous echo server and client. It demonstrates non-blocking I/O operations and task spawning. ```rust use glommio::{net::{TcpListener, TcpStream}, LocalExecutor, spawn_local}; use futures_lite::{AsyncReadExt, AsyncWriteExt}; fn main() -> std::io::Result<()> { let ex = LocalExecutor::default(); ex.run(async { let listener = TcpListener::bind("127.0.0.1:8080").unwrap(); spawn_local(async move { loop { let mut stream = listener.accept().await.unwrap(); spawn_local(async move { let mut buf = [0u8; 1024]; loop { let n = stream.read(&mut buf).await.unwrap(); if n == 0 { break; } stream.write_all(&buf[..n]).await.unwrap(); } }).detach(); } }).detach(); let mut client = TcpStream::connect("127.0.0.1:8080").await.unwrap(); client.write_all(b"Hello!").await.unwrap(); let mut response = vec![0u8; 6]; client.read_exact(&mut response).await.unwrap(); println!("Received: {}", String::from_utf8_lossy(&response)); Ok::<_, std::io::Error>(()) }) } ``` -------------------------------- ### Build sharded architectures with MeshBuilder Source: https://context7.com/datadog/glommio/llms.txt Demonstrates creating a mesh of channels to connect multiple executors, allowing for complex sharded communication patterns. Each shard can send and receive messages from other shards in the mesh. ```rust use glommio::{channels::channel_mesh::MeshBuilder, LocalExecutorBuilder, Placement, enclose}; fn main() { let nr_shards = 4; let mesh = MeshBuilder::full(nr_shards, 1024); let executors: Vec<_> = (0..nr_shards).map(|i| { LocalExecutorBuilder::new(Placement::Unbound) .spawn(enclose!((mesh) move || async move { let (sender, receiver) = mesh.join().await.unwrap(); let my_id = sender.peer_id(); for peer in 0..sender.nr_consumers() { if peer != my_id { sender.send_to(peer, format!("Hello from {}", my_id)) .await.unwrap(); } } for peer in 0..receiver.nr_producers() { if peer != my_id { let msg = receiver.recv_from(peer).await.unwrap(); println!("Shard {} received: {:?}", my_id, msg); } } })) .unwrap() }).collect(); for ex in executors { ex.join().unwrap(); } } ``` -------------------------------- ### Create Multi-CPU Executor Pool Source: https://context7.com/datadog/glommio/llms.txt Utilizes LocalExecutorPoolBuilder to distribute tasks across multiple CPU cores. It demonstrates how to execute code on all shards and aggregate results using atomic counters. ```rust use glommio::{LocalExecutorPoolBuilder, PoolPlacement}; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; fn main() { let counter = Arc::new(AtomicUsize::new(0)); let handles = LocalExecutorPoolBuilder::new(PoolPlacement::MaxSpread(4, None)) .name("worker-pool") .on_all_shards({ let counter = Arc::clone(&counter); move || { let counter = Arc::clone(&counter); async move { counter.fetch_add(1, Ordering::Relaxed); println!("Executor {} started", glommio::executor().id()); } } }) .expect("failed to spawn executors"); handles.join_all(); println!("Total executors ran: {}", counter.load(Ordering::Relaxed)); } ``` -------------------------------- ### Implement RwLock for shared data access in Rust Source: https://context7.com/datadog/glommio/llms.txt Demonstrates the use of RwLock to manage shared data with multiple readers or a single writer. It requires the glommio crate and showcases asynchronous locking mechanisms within a LocalExecutor. ```rust use glommio::{sync::RwLock, LocalExecutor, spawn_local}; use std::rc::Rc; fn main() { let ex = LocalExecutor::default(); ex.run(async { let lock = Rc::new(RwLock::new(0)); let mut readers = vec![]; for i in 0..5 { let lock = lock.clone(); readers.push(spawn_local(async move { let guard = lock.read().await.unwrap(); println!("Reader {} sees value: {}", i, *guard); })); } futures::future::join_all(readers).await; { let mut guard = lock.write().await.unwrap(); *guard += 1; println!("Writer updated value to: {}", *guard); } let guard = lock.read().await.unwrap(); assert_eq!(*guard, 1); }); } ``` -------------------------------- ### Spawning a Local Executor in Glommio (Rust) Source: https://github.com/datadog/glommio/blob/master/README.md This snippet demonstrates how to initialize and spawn a local executor using Glommio's `LocalExecutorBuilder`. It's the fundamental step to running asynchronous code within the Glommio framework. The executor is configured with default settings and then used to run a provided asynchronous task. ```rust use glommio::prelude::*; LocalExecutorBuilder::default().spawn(|| async move { /// your async code here }) .expect("failed to spawn local executor") .join(); ``` -------------------------------- ### Perform intra-executor communication using local_channel Source: https://context7.com/datadog/glommio/llms.txt Shows how to facilitate communication between tasks running on the same executor using unbounded or bounded channels. It demonstrates producer-consumer patterns using task queues. ```rust use glommio::{channels::local_channel, LocalExecutor, Latency, Shares, spawn_local_into}; use futures_lite::stream::StreamExt; fn main() { let ex = LocalExecutor::default(); ex.run(async { let (sender, receiver) = local_channel::new_unbounded::(); let (bounded_sender, bounded_receiver) = local_channel::new_bounded::(10); let producer_tq = glommio::executor().create_task_queue( Shares::default(), Latency::NotImportant, "producer" ); let producer = spawn_local_into(async move { for i in 0..5 { sender.send(i).await.unwrap(); println!("Sent: {}", i); } }, producer_tq).unwrap(); let consumer_tq = glommio::executor().create_task_queue( Shares::default(), Latency::NotImportant, "consumer" ); let consumer = spawn_local_into(async move { let mut receiver = receiver; while let Some(value) = receiver.stream().next().await { println!("Received: {}", value); } }, consumer_tq).unwrap(); futures::join!(producer, consumer); }); } ``` -------------------------------- ### Implement Cooperative Yielding in Rust Source: https://context7.com/datadog/glommio/llms.txt Shows how to prevent task starvation by yielding control back to the executor during long-running computations. It covers both automatic yielding and manual preemption checks. ```rust use glommio::LocalExecutor; fn main() { let ex = LocalExecutor::default(); ex.run(async { for i in 0..1_000_000 { let _ = (0..100).sum::(); if i % 1000 == 0 { glommio::yield_if_needed().await; } } if glommio::executor().need_preempt() { glommio::executor().yield_task_queue_now().await; } }); } ``` -------------------------------- ### Control Concurrency with Semaphores Source: https://context7.com/datadog/glommio/llms.txt Demonstrates how to limit concurrent access to shared resources using a counting semaphore. Includes both blocking acquisition and non-blocking try-acquisition patterns. ```rust use glommio::{sync::Semaphore, LocalExecutor, spawn_local}; use std::rc::Rc; fn main() { let ex = LocalExecutor::default(); ex.run(async { let sem = Rc::new(Semaphore::new(3)); let mut handles = vec![]; for i in 0..10 { let sem = sem.clone(); handles.push(spawn_local(async move { let _permit = sem.acquire_permit(1).await.unwrap(); println!("Task {} acquired permit", i); glommio::timer::sleep(std::time::Duration::from_millis(100)).await; println!("Task {} releasing permit", i); })); } futures::future::join_all(handles).await; if let Some(permit) = sem.try_acquire(1) { println!("Got permit immediately"); drop(permit); } }); } ``` -------------------------------- ### Manage Asynchronous Timing Operations Source: https://context7.com/datadog/glommio/llms.txt Illustrates the use of timers and sleep functions to handle delays and enforce timeouts on asynchronous tasks. ```rust use glommio::{timer::{Timer, sleep, timeout}, LocalExecutor}; use std::time::Duration; fn main() { let ex = LocalExecutor::default(); ex.run(async { sleep(Duration::from_millis(100)).await; println!("Slept for 100ms"); Timer::new(Duration::from_millis(50)).await; println!("Timer fired"); let result = timeout(Duration::from_millis(10), async { sleep(Duration::from_millis(1000)).await; Ok(42) }).await; match result { Ok(value) => println!("Got value: {:?}", value), Err(e) => println!("Timed out: {}", e), } }); } ``` -------------------------------- ### Execute blocking operations in Glommio Source: https://context7.com/datadog/glommio/llms.txt Demonstrates how to offload synchronous, blocking code to a dedicated thread pool to prevent stalling the asynchronous executor. It uses the LocalExecutorBuilder to configure the pool and spawn_blocking to execute tasks. ```rust use glommio::{LocalExecutor, LocalExecutorBuilder, Placement, PoolPlacement}; use std::time::Duration; fn main() { let ex = LocalExecutorBuilder::new(Placement::Unbound) .blocking_thread_pool_placement(PoolPlacement::Unbound(4)) .make() .unwrap(); ex.run(async { let result = glommio::executor().spawn_blocking(|| { std::thread::sleep(Duration::from_millis(100)); "Blocking operation complete" }).await; println!("{}", result); let tasks: Vec<_> = (0..4).map(|i| { glommio::executor().spawn_blocking(move || { std::thread::sleep(Duration::from_millis(50)); i * 2 }) }).collect(); let results: Vec<_> = futures::future::join_all(tasks).await; println!("Results: {:?}", results); }); } ``` -------------------------------- ### Spawn Glommio Executor in a New Thread Source: https://context7.com/datadog/glommio/llms.txt Shows how to spawn an executor in a separate thread using LocalExecutorBuilder. This returns a handle that can be used to join the executor and retrieve its return value. ```rust use glommio::{LocalExecutorBuilder, Placement}; use std::time::Duration; fn main() -> std::io::Result<()> { let handle = LocalExecutorBuilder::new(Placement::Fixed(1)) .name("worker") .spin_before_park(Duration::from_millis(10)) // Spin before sleeping .spawn(|| async move { // Your async code here println!("Executor ID: {}", glommio::executor().id()); 42 // Return value })?; // Wait for the executor to finish let result = handle.join().unwrap(); assert_eq!(result, 42); Ok(()) } ``` -------------------------------- ### Enable inter-executor communication with shared_channel Source: https://context7.com/datadog/glommio/llms.txt Illustrates cross-thread communication between different executors using shared channels. Endpoints are created before spawning executors and connected within the async context. ```rust use glommio::{channels::shared_channel, LocalExecutorBuilder, Placement}; fn main() { let (sender, receiver) = shared_channel::new_bounded::(100); let producer = LocalExecutorBuilder::new(Placement::Fixed(0)) .spawn(move || async move { let sender = sender.connect().await; for i in 0..10 { sender.send(format!("Message {}", i)).await.unwrap(); } println!("Producer done"); }) .unwrap(); let consumer = LocalExecutorBuilder::new(Placement::Fixed(1)) .spawn(move || async move { let receiver = receiver.connect().await; for _ in 0..10 { let msg = receiver.recv().await.unwrap(); println!("Received: {}", msg); } println!("Consumer done"); }) .unwrap(); producer.join().unwrap(); consumer.join().unwrap(); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.