juggle

Build License Crate Documentation

Async task switching for cooperative multitasking in single threaded environments.

This library provides tools to dynamically manage group of tasks in single threaded or embedded environments. Note than this is not an operating system but can serve as a simple replacement where you don't need context switches, for example on embedded applications. Tasks use async/await mechanisms of Rust and it's programmer job to insert switching points into them. Luckily this crate provides Yield utility for this as well as handling busy waits. Primary scheduler (Wheel) can dynamically spawn, suspend, resume or cancel tasks managed by round robin algorithm.

Crate by default uses std library (feature std) but also can be configured as #![no_std] with alloc crate, this disables some features in util module.

Examples

Simple program that reads data from sensor and processes it. ```rust use juggle::*; use alloc::collections::VecDeque; use core::cell::RefCell;

async fn collecttemperature(queue: &RefCell>,handle: WheelHandle<'>){ loop{ // loop forever or until cancelled let temperature: i32 = readtemperaturesensor().await; queue.borrowmut().pushback(temperature); yield_once!(); // give scheduler opportunity to execute other tasks } }

async fn waitfortimer(id: IdNum,queue: &RefCell>,handle: WheelHandle<'>){ inittimer(); for _ in 0..5 { yieldwhile!(gettimervalue() < 200); // busy wait but also executes other tasks. processdata(&mut queue.borrowmut()); resettimer(); } handle.cancel(id); // cancel 'collecttemperature' task. shutdowntimer(); }

fn main(){ let queue = &RefCell::new(VecDeque::new()); let wheel = Wheel::new(); let handle = wheel.handle(); // handle to manage tasks, can be cloned inside this thread

let temp_id = handle.spawn(SpawnParams::default(),
                           collect_temperature(queue,handle.clone()));
handle.spawn(SpawnParams::default(),
             wait_for_timer(temp_id.unwrap(),queue,handle.clone()));

// execute tasks
smol::block_on(wheel).unwrap(); // or any other utility to block on future.

} ```