async_unload/
async-unload.rs

1//! A simple example showing how to (async) unload a module.
2//!
3//! It tries to unload a module by name in an asynchronous way,
4//! retrying for 5s before giving up if the module is busy.
5//!
6//! This is an example ONLY: do NOT panic/unwrap/assert
7//! in production code!
8
9extern crate likemod;
10extern crate tokio;
11
12use std::{num, process, time};
13use tokio::runtime::current_thread;
14use tokio::timer::timeout;
15
16fn main() {
17    // Get from cmdline the name of the module to unload.
18    let modname = std::env::args().nth(1).expect("missing module name");
19
20    // Assemble a future to unload the module; if busy,
21    // retry every 500ms and time out after 5 seconds.
22    let pause_ms = num::NonZeroU64::new(500).unwrap();
23    let modunload = likemod::ModUnloader::new().unload_async(&modname, pause_ms);
24    let tout = time::Duration::from_secs(15);
25    let fut = timeout::Timeout::new(modunload, tout);
26
27    // Run the future until completion.
28    if let Err(err) = current_thread::block_on_all(fut) {
29        eprintln!("FAILED: {:?}", err);
30        process::exit(1)
31    }
32
33    // Success!
34    println!("module '{}' unloaded.", modname);
35}