Skip to content

Commit

Permalink
Make pin easier to prove correct. Simplify unparking.
Browse files Browse the repository at this point in the history
  • Loading branch information
spacejam committed Jan 20, 2020
1 parent 15ac119 commit fd99cf6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "extreme"
version = "666.666.66666"
version = "666.666.666666"
authors = ["Tyler Neely <t@jujit.su>"]
edition = "2018"
description = "Extremely boring async function runner. MIT/Apache-2.0 license is available for spacejam's github sponsors."
Expand Down
17 changes: 8 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use std::sync::{Arc, Condvar, Mutex};
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};

#[derive(Default)]
struct Park(Mutex<u64>, Condvar);
struct Park(Mutex<bool>, Condvar);

fn unpark(park: &Park) {
let mut counter = park.0.lock().unwrap();
*counter += 1;
*park.0.lock().unwrap() = true;
park.1.notify_one();
}

Expand All @@ -23,21 +22,21 @@ static VTABLE: RawWakerVTable = RawWakerVTable::new(

/// Run a `Future`.
pub fn run<F: std::future::Future>(mut f: F) -> F::Output {
let mut f = unsafe { std::pin::Pin::new_unchecked(&mut f) };
let park = Arc::new(Park::default());
let sender = Arc::into_raw(park.clone());
let raw_waker = RawWaker::new(sender as *const _, &VTABLE);
let waker = unsafe { Waker::from_raw(raw_waker) };
let mut cx = Context::from_waker(&waker);

loop {
let pin = unsafe { std::pin::Pin::new_unchecked(&mut f) };
match F::poll(pin, &mut cx) {
match f.as_mut().poll(&mut cx) {
Poll::Pending => {
let mut counter = park.0.lock().unwrap();
while *counter == 0 {
counter = park.1.wait(counter).unwrap();
let mut runnable = park.0.lock().unwrap();
while !*runnable {
runnable = park.1.wait(runnable).unwrap();
}
*counter -= 1;
*runnable = false;
}
Poll::Ready(val) => return val,
}
Expand Down

0 comments on commit fd99cf6

Please sign in to comment.