greenie/
lib.rs

1//! Lightweigh green threads & coroutines in stable Rust.
2//!
3//! # Getting started
4//!  
5//! Add greenie to your `Cargo.toml`:
6//! ```toml
7//! [dependencies]
8//! greenie = "*"
9//! ```
10//!
11//! Create greenie main function in `src/main.rs`:
12//! ```ignore
13//! use greenie::*;
14//!
15//! #[greenie_main]
16//! fn main() {
17//!     
18//! }
19//! // Or you can invoke `create_main`:
20//! fn main() {
21//!     create_main(|| {
22//!     });
23//! }
24//!
25//! ```
26//!
27//!
28//! # Example
29//! Ping-pong program
30//! ```ignore
31//! use greenie::channel::*;
32//!
33//! use greenie::{greeny_main, Fiber};
34//! #[greeny_main]
35//! fn main() {
36//!     let chan_1 = Channel::<&'static str>::new(2);
37//!     let chan_2 = Channel::<&'static str>::new(2);
38//!     let fping = Fiber::new_capture(
39//!         |chan_1, chan_2| {
40//!             chan_1.send("ping");
41//!             println!("{}", chan_2.recv().unwrap());
42//!             chan_1.send("ping");
43//!             println!("{}", chan_2.recv().unwrap());
44//!             chan_1.send("ping");
45//!             println!("{}", chan_2.recv().unwrap());
46//!         },
47//!         (chan_1.clone(), chan_2.clone()),
48//!     );
49//!     let fpong = Fiber::new_capture(
50//!         |chan_1, chan_2| {
51//!             chan_2.send("pong");
52//!             println!("{}", chan_1.recv().unwrap());
53//!             chan_2.send("pong");
54//!             println!("{}", chan_1.recv().unwrap());
55//!             chan_2.send("pong");
56//!             println!("{}", chan_1.recv().unwrap());
57//!         },
58//!         (chan_1.clone(), chan_2.clone()),
59//!     );
60//!
61//!     fpong.start().unwrap();
62//!     fping.start().unwrap();
63//! }
64//! ```
65
66#![allow(dead_code, improper_ctypes)]
67
68#[macro_use]
69extern crate intrusive_collections;
70
71pub mod algorithm;
72pub mod common;
73pub mod ctx;
74pub mod fiber;
75pub mod generator;
76pub mod ptr;
77pub mod scheduler;
78pub use generator::generator_yield;
79pub use scheduler::{spawn_greenie, yield_thread};
80
81pub use greenie_proc::{greenify, greeny_main};
82/// Puts the current thread to sleep for at least the specified amount of time.
83///
84/// The thread may sleep longer than the duration specified due to scheduling specifics or platform-dependent functionality. It will
85/// never sleep less.
86pub fn thread_sleep(duration: std::time::Duration) {
87    let now = std::time::Instant::now();
88
89    while duration > now.elapsed() {
90        crate::yield_thread();
91    }
92}
93use ctx::Context;
94
95pub use fiber::Fiber;
96pub use generator::*;
97/// Specify entry point for program that will use greenie.
98pub fn create_main(main_fn: fn()) -> ! {
99    scheduler::RUNTIME.with(|rt| {
100        let h = rt.get().spawn(|f, _| f(), (main_fn, ()));
101        rt.get().main_ctx = h.thread();
102        rt.main_ctx.get().is_main = true;
103        rt.get().run();
104
105        //unsafe { std::intrinsics::drop_in_place(rt.0) };
106    });
107    unreachable!()
108}