The problems addressed in the earlier version led to an "incorrect" example which is bad to pass along after reading a whole book. after getting some feedback in #2 i decided to show how we can create a proper `Parker`. The main example (which I assume most interested readers will copy) now uses a proper parking thechnique so there should be no more dataraces left. I also removed the "Reader Excercise" paragraph suggesting that they explore a way to implement proper parking since we now show that in our main example.
475 lines
20 KiB
HTML
475 lines
20 KiB
HTML
<!DOCTYPE HTML>
|
|
<html lang="en" class="sidebar-visible no-js light">
|
|
<head>
|
|
<!-- Book generated using mdBook -->
|
|
<meta charset="UTF-8">
|
|
<title>Finished example (editable) - Futures Explained in 200 Lines of Rust</title>
|
|
|
|
|
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
<meta name="description" content="This book aims to explain Futures in Rust using an example driven approach.">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<meta name="theme-color" content="#ffffff" />
|
|
|
|
<link rel="shortcut icon" href="favicon.png">
|
|
<link rel="stylesheet" href="css/variables.css">
|
|
<link rel="stylesheet" href="css/general.css">
|
|
<link rel="stylesheet" href="css/chrome.css">
|
|
<link rel="stylesheet" href="css/print.css" media="print">
|
|
|
|
<!-- Fonts -->
|
|
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
|
|
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
|
|
<!-- Highlight.js Stylesheets -->
|
|
<link rel="stylesheet" href="highlight.css">
|
|
<link rel="stylesheet" href="tomorrow-night.css">
|
|
<link rel="stylesheet" href="ayu-highlight.css">
|
|
|
|
<!-- Custom theme stylesheets -->
|
|
|
|
|
|
|
|
</head>
|
|
<body>
|
|
<!-- Provide site root to javascript -->
|
|
<script type="text/javascript">
|
|
var path_to_root = "";
|
|
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "light" : "light";
|
|
</script>
|
|
|
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
<script type="text/javascript">
|
|
try {
|
|
var theme = localStorage.getItem('mdbook-theme');
|
|
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
|
|
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
}
|
|
|
|
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
}
|
|
} catch (e) { }
|
|
</script>
|
|
|
|
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
<script type="text/javascript">
|
|
var theme;
|
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
var html = document.querySelector('html');
|
|
html.classList.remove('no-js')
|
|
html.classList.remove('light')
|
|
html.classList.add(theme);
|
|
html.classList.add('js');
|
|
</script>
|
|
|
|
<!-- Hide / unhide sidebar before it is displayed -->
|
|
<script type="text/javascript">
|
|
var html = document.querySelector('html');
|
|
var sidebar = 'hidden';
|
|
if (document.body.clientWidth >= 1080) {
|
|
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
sidebar = sidebar || 'visible';
|
|
}
|
|
html.classList.remove('sidebar-visible');
|
|
html.classList.add("sidebar-" + sidebar);
|
|
</script>
|
|
|
|
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
<div id="sidebar-scrollbox" class="sidebar-scrollbox">
|
|
<ol class="chapter"><li class="expanded affix "><a href="introduction.html">Introduction</a></li><li class="expanded "><a href="0_background_information.html"><strong aria-hidden="true">1.</strong> Background information</a></li><li class="expanded "><a href="1_futures_in_rust.html"><strong aria-hidden="true">2.</strong> Futures in Rust</a></li><li class="expanded "><a href="2_waker_context.html"><strong aria-hidden="true">3.</strong> Waker and Context</a></li><li class="expanded "><a href="3_generators_async_await.html"><strong aria-hidden="true">4.</strong> Generators and async/await</a></li><li class="expanded "><a href="4_pin.html"><strong aria-hidden="true">5.</strong> Pin</a></li><li class="expanded "><a href="6_future_example.html"><strong aria-hidden="true">6.</strong> Implementing Futures</a></li><li class="expanded "><a href="8_finished_example.html" class="active"><strong aria-hidden="true">7.</strong> Finished example (editable)</a></li><li class="expanded affix "><a href="conclusion.html">Conclusion and exercises</a></li></ol>
|
|
</div>
|
|
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
</nav>
|
|
|
|
<div id="page-wrapper" class="page-wrapper">
|
|
|
|
<div class="page">
|
|
|
|
<div id="menu-bar" class="menu-bar">
|
|
<div id="menu-bar-sticky-container">
|
|
<div class="left-buttons">
|
|
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
<i class="fa fa-bars"></i>
|
|
</button>
|
|
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
<i class="fa fa-paint-brush"></i>
|
|
</button>
|
|
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
</ul>
|
|
|
|
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
<i class="fa fa-search"></i>
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<h1 class="menu-title">Futures Explained in 200 Lines of Rust</h1>
|
|
|
|
<div class="right-buttons">
|
|
<a href="print.html" title="Print this book" aria-label="Print this book">
|
|
<i id="print-button" class="fa fa-print"></i>
|
|
</a>
|
|
|
|
<a href="https://github.com/cfsamson/books-futures-explained" title="Git repository" aria-label="Git repository">
|
|
<i id="git-repository-button" class="fa fa-github"></i>
|
|
</a>
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div id="search-wrapper" class="hidden">
|
|
<form id="searchbar-outer" class="searchbar-outer">
|
|
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
</form>
|
|
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
<div id="searchresults-header" class="searchresults-header"></div>
|
|
<ul id="searchresults">
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
<script type="text/javascript">
|
|
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
});
|
|
</script>
|
|
|
|
<div id="content" class="content">
|
|
<main>
|
|
<h1><a class="header" href="#our-finished-code" id="our-finished-code">Our finished code</a></h1>
|
|
<p>Here is the whole example. You can edit it right here in your browser and
|
|
run it yourself. Have fun!</p>
|
|
<pre><pre class="playpen"><code class="language-rust editable edition2018">fn main() {
|
|
let start = Instant::now();
|
|
let reactor = Reactor::new();
|
|
|
|
let fut1 = async {
|
|
let val = Task::new(reactor.clone(), 1, 1).await;
|
|
println!("Got {} at time: {:.2}.", val, start.elapsed().as_secs_f32());
|
|
};
|
|
|
|
let fut2 = async {
|
|
let val = Task::new(reactor.clone(), 2, 2).await;
|
|
println!("Got {} at time: {:.2}.", val, start.elapsed().as_secs_f32());
|
|
};
|
|
|
|
let mainfut = async {
|
|
fut1.await;
|
|
fut2.await;
|
|
};
|
|
|
|
block_on(mainfut);
|
|
reactor.lock().map(|mut r| r.close()).unwrap();
|
|
}
|
|
|
|
use std::{
|
|
future::Future, sync::{ mpsc::{channel, Sender}, Arc, Mutex, Condvar},
|
|
task::{Context, Poll, RawWaker, RawWakerVTable, Waker}, mem, pin::Pin,
|
|
thread::{self, JoinHandle}, time::{Duration, Instant}, collections::HashMap
|
|
};
|
|
// ============================= EXECUTOR ====================================
|
|
#[derive(Default)]
|
|
struct Parker(Mutex<bool>, Condvar);
|
|
|
|
impl Parker {
|
|
fn park(&self) {
|
|
let mut resumable = self.0.lock().unwrap();
|
|
while !*resumable {
|
|
resumable = self.1.wait(resumable).unwrap();
|
|
}
|
|
*resumable = false;
|
|
}
|
|
|
|
fn unpark(&self) {
|
|
*self.0.lock().unwrap() = true;
|
|
self.1.notify_one();
|
|
}
|
|
}
|
|
|
|
fn block_on<F: Future>(mut future: F) -> F::Output {
|
|
let parker = Arc::new(Parker::default());
|
|
let mywaker = Arc::new(MyWaker { parker: parker.clone() });
|
|
let waker = mywaker_into_waker(Arc::into_raw(mywaker));
|
|
let mut cx = Context::from_waker(&waker);
|
|
|
|
// SAFETY: we shadow `future` so it can't be accessed again.
|
|
let mut future = unsafe { Pin::new_unchecked(&mut future) };
|
|
loop {
|
|
match Future::poll(future.as_mut(), &mut cx) {
|
|
Poll::Ready(val) => break val,
|
|
Poll::Pending => parker.park(),
|
|
};
|
|
}
|
|
}
|
|
// ====================== FUTURE IMPLEMENTATION ==============================
|
|
#[derive(Clone)]
|
|
struct MyWaker {
|
|
parker: Arc<Parker>,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct Task {
|
|
id: usize,
|
|
reactor: Arc<Mutex<Box<Reactor>>>,
|
|
data: u64,
|
|
}
|
|
|
|
fn mywaker_wake(s: &MyWaker) {
|
|
let waker_arc = unsafe { Arc::from_raw(s) };
|
|
waker_arc.parker.unpark();
|
|
}
|
|
|
|
fn mywaker_clone(s: &MyWaker) -> RawWaker {
|
|
let arc = unsafe { Arc::from_raw(s) };
|
|
std::mem::forget(arc.clone()); // increase ref count
|
|
RawWaker::new(Arc::into_raw(arc) as *const (), &VTABLE)
|
|
}
|
|
|
|
const VTABLE: RawWakerVTable = unsafe {
|
|
RawWakerVTable::new(
|
|
|s| mywaker_clone(&*(s as *const MyWaker)), // clone
|
|
|s| mywaker_wake(&*(s as *const MyWaker)), // wake
|
|
|s| mywaker_wake(*(s as *const &MyWaker)), // wake by ref
|
|
|s| drop(Arc::from_raw(s as *const MyWaker)), // decrease refcount
|
|
)
|
|
};
|
|
|
|
fn mywaker_into_waker(s: *const MyWaker) -> Waker {
|
|
let raw_waker = RawWaker::new(s as *const (), &VTABLE);
|
|
unsafe { Waker::from_raw(raw_waker) }
|
|
}
|
|
|
|
impl Task {
|
|
fn new(reactor: Arc<Mutex<Box<Reactor>>>, data: u64, id: usize) -> Self {
|
|
Task { id, reactor, data }
|
|
}
|
|
}
|
|
|
|
impl Future for Task {
|
|
type Output = usize;
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
let mut r = self.reactor.lock().unwrap();
|
|
if r.is_ready(self.id) {
|
|
*r.tasks.get_mut(&self.id).unwrap() = TaskState::Finished;
|
|
Poll::Ready(self.id)
|
|
} else if r.tasks.contains_key(&self.id) {
|
|
r.tasks.insert(self.id, TaskState::NotReady(cx.waker().clone()));
|
|
Poll::Pending
|
|
} else {
|
|
r.register(self.data, cx.waker().clone(), self.id);
|
|
Poll::Pending
|
|
}
|
|
}
|
|
}
|
|
// =============================== REACTOR ===================================
|
|
enum TaskState {
|
|
Ready,
|
|
NotReady(Waker),
|
|
Finished,
|
|
}
|
|
struct Reactor {
|
|
dispatcher: Sender<Event>,
|
|
handle: Option<JoinHandle<()>>,
|
|
tasks: HashMap<usize, TaskState>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
enum Event {
|
|
Close,
|
|
Timeout(u64, usize),
|
|
}
|
|
|
|
impl Reactor {
|
|
fn new() -> Arc<Mutex<Box<Self>>> {
|
|
let (tx, rx) = channel::<Event>();
|
|
let reactor = Arc::new(Mutex::new(Box::new(Reactor {
|
|
dispatcher: tx,
|
|
handle: None,
|
|
tasks: HashMap::new(),
|
|
})));
|
|
|
|
let reactor_clone = Arc::downgrade(&reactor);
|
|
let handle = thread::spawn(move || {
|
|
let mut handles = vec![];
|
|
for event in rx {
|
|
let reactor = reactor_clone.clone();
|
|
match event {
|
|
Event::Close => break,
|
|
Event::Timeout(duration, id) => {
|
|
let event_handle = thread::spawn(move || {
|
|
thread::sleep(Duration::from_secs(duration));
|
|
let reactor = reactor.upgrade().unwrap();
|
|
reactor.lock().map(|mut r| r.wake(id)).unwrap();
|
|
});
|
|
handles.push(event_handle);
|
|
}
|
|
}
|
|
}
|
|
handles.into_iter().for_each(|handle| handle.join().unwrap());
|
|
});
|
|
reactor.lock().map(|mut r| r.handle = Some(handle)).unwrap();
|
|
reactor
|
|
}
|
|
|
|
fn wake(&mut self, id: usize) {
|
|
let state = self.tasks.get_mut(&id).unwrap();
|
|
match mem::replace(state, TaskState::Ready) {
|
|
TaskState::NotReady(waker) => waker.wake(),
|
|
TaskState::Finished => panic!("Called 'wake' twice on task: {}", id),
|
|
_ => unreachable!()
|
|
}
|
|
}
|
|
|
|
fn register(&mut self, duration: u64, waker: Waker, id: usize) {
|
|
if self.tasks.insert(id, TaskState::NotReady(waker)).is_some() {
|
|
panic!("Tried to insert a task with id: '{}', twice!", id);
|
|
}
|
|
self.dispatcher.send(Event::Timeout(duration, id)).unwrap();
|
|
}
|
|
|
|
fn close(&mut self) {
|
|
self.dispatcher.send(Event::Close).unwrap();
|
|
}
|
|
|
|
fn is_ready(&self, id: usize) -> bool {
|
|
self.tasks.get(&id).map(|state| match state {
|
|
TaskState::Ready => true,
|
|
_ => false,
|
|
}).unwrap_or(false)
|
|
}
|
|
}
|
|
|
|
impl Drop for Reactor {
|
|
fn drop(&mut self) {
|
|
self.handle.take().map(|h| h.join().unwrap()).unwrap();
|
|
}
|
|
}
|
|
</code></pre></pre>
|
|
|
|
</main>
|
|
|
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
<!-- Mobile navigation buttons -->
|
|
|
|
<a rel="prev" href="6_future_example.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
<i class="fa fa-angle-left"></i>
|
|
</a>
|
|
|
|
|
|
|
|
<a rel="next" href="conclusion.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
<i class="fa fa-angle-right"></i>
|
|
</a>
|
|
|
|
|
|
<div style="clear: both"></div>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
|
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
|
|
<a href="6_future_example.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
<i class="fa fa-angle-left"></i>
|
|
</a>
|
|
|
|
|
|
|
|
<a href="conclusion.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
<i class="fa fa-angle-right"></i>
|
|
</a>
|
|
|
|
</nav>
|
|
|
|
</div>
|
|
|
|
|
|
<!-- Livereload script (if served using the cli tool) -->
|
|
<script type="text/javascript">
|
|
var socket = new WebSocket("ws://localhost:3001");
|
|
socket.onmessage = function (event) {
|
|
if (event.data === "reload") {
|
|
socket.close();
|
|
location.reload(true); // force reload from server (not from cache)
|
|
}
|
|
};
|
|
|
|
window.onbeforeunload = function() {
|
|
socket.close();
|
|
}
|
|
</script>
|
|
|
|
|
|
|
|
<!-- Google Analytics Tag -->
|
|
<script type="text/javascript">
|
|
var localAddrs = ["localhost", "127.0.0.1", ""];
|
|
|
|
// make sure we don't activate google analytics if the developer is
|
|
// inspecting the book locally...
|
|
if (localAddrs.indexOf(document.location.hostname) === -1) {
|
|
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
|
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
|
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
|
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
|
|
|
|
ga('create', 'UA-157536992-1', 'auto');
|
|
ga('send', 'pageview');
|
|
}
|
|
</script>
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
window.playpen_line_numbers = true;
|
|
</script>
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
window.playpen_copyable = true;
|
|
</script>
|
|
|
|
|
|
|
|
<script src="ace.js" type="text/javascript" charset="utf-8"></script>
|
|
<script src="editor.js" type="text/javascript" charset="utf-8"></script>
|
|
<script src="mode-rust.js" type="text/javascript" charset="utf-8"></script>
|
|
<script src="theme-dawn.js" type="text/javascript" charset="utf-8"></script>
|
|
<script src="theme-tomorrow_night.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
|
|
|
<script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
<script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
<script src="searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
|
<script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
<script src="highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
<script src="book.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
<!-- Custom JS scripts -->
|
|
|
|
|
|
|
|
|
|
</body>
|
|
</html>
|