Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rt): implement runtime trait to define a unique runtime #15

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitsigners
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hyyfiw7gfnqfmgf8df6b6mu8pg6bka3end4kyxrtyekmd35ftzusq4 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAtadMUTirMU4y+D5cztN4KsZQIelAeSIEKWPOyxvOzt
8 changes: 4 additions & 4 deletions examples/rio_triage_await/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use github::model::NewIssue;
use hackmd::api::HackmdAPI;
use hackmd::model::NewNote;
use log::debug;
use rio_rt::runitime as rio;
use rio_rt::runitime::Runtime;
use rio_rt::Rt;
use surf;

pub(crate) mod extractor;
Expand All @@ -29,6 +30,7 @@ async fn run(

fn main() {
env_logger::init();
let rio = Runtime::new();

// FIXME: load conf from json
let conf = model::TriageConf {
Expand All @@ -44,9 +46,7 @@ fn main() {
let github = github::GithubExtractor::new(&conf);
let hackmd_api = hackmd::api::HackmdAPI::new("", false);

rio::block_on(async move {
rio.block_on(async move {
run(&github, &hackmd_api).await.unwrap();
});

rio::wait();
}
6 changes: 5 additions & 1 deletion rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
//!
//! In addition, from this crate the user can consider to learn more on the async
//! programming, because there is a lot to learn and a lot to contribute.
// TODO: move feature inside a std :)
#![feature(once_cell)]
#[allow(unused_imports)]
#[allow(incomplete_features)]
mod rt;
pub mod runitime;
pub mod task;

pub use rt::Rt;
21 changes: 21 additions & 0 deletions rt/src/rt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! A reference implementation of
//! the async main and tests proposed
//! discussed at https://rust-lang.zulipchat.com/#narrow/stream/187312-wg-async/topic/Weekly.20sync.202022-12-15
use std::future::Future;

/// Runtime trait definition is a
/// generic interface of the trait
/// that allow to have a generic
/// interface.
///
/// This allow the possibility to start
/// experimenting with this trait and
/// see if it is possible design library
/// executor agnostics.
pub trait Rt {
fn new() -> &'static Self;

/// Allow to run the future and block the execution till this
/// future is ended.
fn block_on(&self, future: impl Future<Output = ()> + Send + 'static);
}
19 changes: 13 additions & 6 deletions rt/src/runitime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ use std::task::Poll;

use log::debug;

use crate::rt::Rt;
use crate::task::Task;

pub(crate) type Queue = Arc<Mutex<LinkedList<Arc<Task>>>>;

/// Runtime definition
pub(crate) struct Runtime {
pub struct Runtime {
pub(crate) task_queue: Queue,
/// Size of the runtime
pub(crate) size: AtomicUsize,
Expand Down Expand Up @@ -92,14 +93,20 @@ fn configure() -> Runtime {
pub fn spawn(future: impl Future<Output = ()> + Send + 'static) {
Runtime::get().spawn(future);
}
/// Block on a `Future` and stop others on the `whorl` runtime until this
/// one completes.
pub fn block_on(future: impl Future<Output = ()> + Send + 'static) {
Runtime::get().spawn_blocking(future);
}

/// Block further execution of a program until all of the tasks on the
/// `whorl` runtime are completed.
pub fn wait() {
let runtime = Runtime::get();
while runtime.size.load(Ordering::Relaxed) > 0 {}
}

impl Rt for Runtime {
fn new() -> &'static Self {
Runtime::get()
}

fn block_on(&self, future: impl Future<Output = ()> + Send + 'static) {
self.spawn_blocking(future)
}
}