|
|
<!DOCTYPE HTML>
|
|
|
<html lang="zh-CN" class="light sidebar-visible" dir="ltr">
|
|
|
<head>
|
|
|
<!-- Book generated using mdBook -->
|
|
|
<meta charset="UTF-8">
|
|
|
<title>底层探秘: Future 执行与任务调度 - Rust语言圣经(Rust Course)</title>
|
|
|
|
|
|
|
|
|
<!-- Custom HTML head -->
|
|
|
|
|
|
<meta name="description" content="">
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
<meta name="theme-color" content="#ffffff">
|
|
|
|
|
|
<link rel="icon" href="../../favicon-de23e50b.svg">
|
|
|
<link rel="shortcut icon" href="../../favicon-8114d1fc.png">
|
|
|
<link rel="stylesheet" href="../../css/variables-8adf115d.css">
|
|
|
<link rel="stylesheet" href="../../css/general-2459343d.css">
|
|
|
<link rel="stylesheet" href="../../css/chrome-ae938929.css">
|
|
|
<link rel="stylesheet" href="../../css/print-9e4910d8.css" media="print">
|
|
|
|
|
|
<!-- Fonts -->
|
|
|
<link rel="stylesheet" href="../../fonts/fonts-9644e21d.css">
|
|
|
|
|
|
<!-- Highlight.js Stylesheets -->
|
|
|
<link rel="stylesheet" id="mdbook-highlight-css" href="../../highlight-493f70e1.css">
|
|
|
<link rel="stylesheet" id="mdbook-tomorrow-night-css" href="../../tomorrow-night-4c0ae647.css">
|
|
|
<link rel="stylesheet" id="mdbook-ayu-highlight-css" href="../../ayu-highlight-3fdfc3ac.css">
|
|
|
|
|
|
<!-- Custom theme stylesheets -->
|
|
|
<link rel="stylesheet" href="../../theme/style-78591068.css">
|
|
|
|
|
|
|
|
|
<!-- Provide site root and default themes to javascript -->
|
|
|
<script>
|
|
|
const path_to_root = "../../";
|
|
|
const default_light_theme = "light";
|
|
|
const default_dark_theme = "navy";
|
|
|
window.path_to_searchindex_js = "../../searchindex-4a1a74fa.js";
|
|
|
</script>
|
|
|
<!-- Start loading toc.js asap -->
|
|
|
<script src="../../toc-42887675.js"></script>
|
|
|
</head>
|
|
|
<body>
|
|
|
<div id="mdbook-help-container">
|
|
|
<div id="mdbook-help-popup">
|
|
|
<h2 class="mdbook-help-title">Keyboard shortcuts</h2>
|
|
|
<div>
|
|
|
<p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p>
|
|
|
<p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
|
|
|
<p>Press <kbd>?</kbd> to show this help</p>
|
|
|
<p>Press <kbd>Esc</kbd> to hide this help</p>
|
|
|
</div>
|
|
|
</div>
|
|
|
</div>
|
|
|
<div id="mdbook-body-container">
|
|
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
|
<script>
|
|
|
try {
|
|
|
let theme = localStorage.getItem('mdbook-theme');
|
|
|
let 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>
|
|
|
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
|
|
|
let theme;
|
|
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
|
const html = document.documentElement;
|
|
|
html.classList.remove('light')
|
|
|
html.classList.add(theme);
|
|
|
html.classList.add("js");
|
|
|
</script>
|
|
|
|
|
|
<input type="checkbox" id="mdbook-sidebar-toggle-anchor" class="hidden">
|
|
|
|
|
|
<!-- Hide / unhide sidebar before it is displayed -->
|
|
|
<script>
|
|
|
let sidebar = null;
|
|
|
const sidebar_toggle = document.getElementById("mdbook-sidebar-toggle-anchor");
|
|
|
if (document.body.clientWidth >= 1080) {
|
|
|
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
|
sidebar = sidebar || 'visible';
|
|
|
} else {
|
|
|
sidebar = 'hidden';
|
|
|
sidebar_toggle.checked = false;
|
|
|
}
|
|
|
if (sidebar === 'visible') {
|
|
|
sidebar_toggle.checked = true;
|
|
|
} else {
|
|
|
html.classList.remove('sidebar-visible');
|
|
|
}
|
|
|
</script>
|
|
|
|
|
|
<nav id="mdbook-sidebar" class="sidebar" aria-label="Table of contents">
|
|
|
<!-- populated by js -->
|
|
|
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
|
|
|
<noscript>
|
|
|
<iframe class="sidebar-iframe-outer" src="../../toc.html"></iframe>
|
|
|
</noscript>
|
|
|
<div id="mdbook-sidebar-resize-handle" class="sidebar-resize-handle">
|
|
|
<div class="sidebar-resize-indicator"></div>
|
|
|
</div>
|
|
|
</nav>
|
|
|
|
|
|
<div id="mdbook-page-wrapper" class="page-wrapper">
|
|
|
|
|
|
<div class="page">
|
|
|
<div id="mdbook-menu-bar-hover-placeholder"></div>
|
|
|
<div id="mdbook-menu-bar" class="menu-bar sticky">
|
|
|
<div class="left-buttons">
|
|
|
<label id="mdbook-sidebar-toggle" class="icon-button" for="mdbook-sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="mdbook-sidebar">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M0 96C0 78.3 14.3 64 32 64H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32C14.3 128 0 113.7 0 96zM0 256c0-17.7 14.3-32 32-32H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32c-17.7 0-32-14.3-32-32zM448 416c0 17.7-14.3 32-32 32H32c-17.7 0-32-14.3-32-32s14.3-32 32-32H416c17.7 0 32 14.3 32 32z"/></svg></span>
|
|
|
</label>
|
|
|
<button id="mdbook-theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="mdbook-theme-list">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M371.3 367.1c27.3-3.9 51.9-19.4 67.2-42.9L600.2 74.1c12.6-19.5 9.4-45.3-7.6-61.2S549.7-4.4 531.1 9.6L294.4 187.2c-24 18-38.2 46.1-38.4 76.1L371.3 367.1zm-19.6 25.4l-116-104.4C175.9 290.3 128 339.6 128 400c0 3.9 .2 7.8 .6 11.6c1.8 17.5-10.2 36.4-27.8 36.4H96c-17.7 0-32 14.3-32 32s14.3 32 32 32H240c61.9 0 112-50.1 112-112c0-2.5-.1-5-.2-7.5z"/></svg></span>
|
|
|
</button>
|
|
|
<ul id="mdbook-theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-default_theme">Auto</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-light">Light</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-rust">Rust</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-coal">Coal</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-navy">Navy</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-ayu">Ayu</button></li>
|
|
|
</ul>
|
|
|
<button id="mdbook-search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="mdbook-searchbar">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M416 208c0 45.9-14.9 88.3-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM208 352c79.5 0 144-64.5 144-144s-64.5-144-144-144S64 128.5 64 208s64.5 144 144 144z"/></svg></span>
|
|
|
</button>
|
|
|
</div>
|
|
|
|
|
|
<h1 class="menu-title">Rust语言圣经(Rust Course)</h1>
|
|
|
|
|
|
<div class="right-buttons">
|
|
|
<a href="../../print.html" title="Print this book" aria-label="Print this book">
|
|
|
<span class=fa-svg id="print-button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M128 0C92.7 0 64 28.7 64 64v96h64V64H354.7L384 93.3V160h64V93.3c0-17-6.7-33.3-18.7-45.3L400 18.7C388 6.7 371.7 0 354.7 0H128zM384 352v32 64H128V384 368 352H384zm64 32h32c17.7 0 32-14.3 32-32V256c0-35.3-28.7-64-64-64H64c-35.3 0-64 28.7-64 64v96c0 17.7 14.3 32 32 32H64v64c0 35.3 28.7 64 64 64H384c35.3 0 64-28.7 64-64V384zm-16-88c-13.3 0-24-10.7-24-24s10.7-24 24-24s24 10.7 24 24s-10.7 24-24 24z"/></svg></span>
|
|
|
</a>
|
|
|
<a href="https://github.com/sunface/rust-course" title="Git repository" aria-label="Git repository">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg></span>
|
|
|
</a>
|
|
|
<a href="https://github.com/sunface/rust-course/edit/main/src/advance/async/future-excuting.md" title="Suggest an edit" aria-label="Suggest an edit" rel="edit">
|
|
|
<span class=fa-svg id="git-edit-button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M421.7 220.3l-11.3 11.3-22.6 22.6-205 205c-6.6 6.6-14.8 11.5-23.8 14.1L30.8 511c-8.4 2.5-17.5 .2-23.7-6.1S-1.5 489.7 1 481.2L38.7 353.1c2.6-9 7.5-17.2 14.1-23.8l205-205 22.6-22.6 11.3-11.3 33.9 33.9 62.1 62.1 33.9 33.9zM96 353.9l-9.3 9.3c-.9 .9-1.6 2.1-2 3.4l-25.3 86 86-25.3c1.3-.4 2.5-1.1 3.4-2l9.3-9.3H112c-8.8 0-16-7.2-16-16V353.9zM453.3 19.3l39.4 39.4c25 25 25 65.5 0 90.5l-14.5 14.5-22.6 22.6-11.3 11.3-33.9-33.9-62.1-62.1L314.3 67.7l11.3-11.3 22.6-22.6 14.5-14.5c25-25 65.5-25 90.5 0z"/></svg></span>
|
|
|
</a>
|
|
|
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<div id="mdbook-search-wrapper" class="hidden">
|
|
|
<form id="mdbook-searchbar-outer" class="searchbar-outer">
|
|
|
<div class="search-wrapper">
|
|
|
<input type="search" id="mdbook-searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="mdbook-searchresults-outer" aria-describedby="searchresults-header">
|
|
|
<div class="spinner-wrapper">
|
|
|
<span class=fa-svg id="fa-spin"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M304 48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zm0 416c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM48 304c26.5 0 48-21.5 48-48s-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48zm464-48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM142.9 437c18.7-18.7 18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zm0-294.2c18.7-18.7 18.7-49.1 0-67.9S93.7 56.2 75 75s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zM369.1 437c18.7 18.7 49.1 18.7 67.9 0s18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9z"/></svg></span>
|
|
|
</div>
|
|
|
</div>
|
|
|
</form>
|
|
|
<div id="mdbook-searchresults-outer" class="searchresults-outer hidden">
|
|
|
<div id="mdbook-searchresults-header" class="searchresults-header"></div>
|
|
|
<ul id="mdbook-searchresults">
|
|
|
</ul>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
|
<script>
|
|
|
document.getElementById('mdbook-sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
|
document.getElementById('mdbook-sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
|
Array.from(document.querySelectorAll('#mdbook-sidebar a')).forEach(function(link) {
|
|
|
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
|
});
|
|
|
</script>
|
|
|
|
|
|
<div id="mdbook-content" class="content">
|
|
|
<main>
|
|
|
<h1 id="底层探秘-future-执行器与任务调度"><a class="header" href="#底层探秘-future-执行器与任务调度">底层探秘: Future 执行器与任务调度</a></h1>
|
|
|
<p>异步编程背后到底藏有什么秘密?究竟是哪只幕后之手在操纵这一切?如果你对这些感兴趣,就继续看下去,否则可以直接跳过,因为本章节的内容对于一个 API 工程师并没有太多帮助。</p>
|
|
|
<p>但是如果你希望能深入理解 <code>Rust</code> 的 <code>async/.await</code> 代码是如何工作、理解运行时和性能,甚至未来想要构建自己的 <code>async</code> 运行时或相关工具,那么本章节终究不会辜负于你。</p>
|
|
|
<h2 id="future-特征"><a class="header" href="#future-特征">Future 特征</a></h2>
|
|
|
<p><code>Future</code> 特征是 Rust 异步编程的核心,毕竟异步函数是异步编程的核心,而 <code>Future</code> 恰恰是异步函数的返回值和被执行的关键。</p>
|
|
|
<p>首先,来给出 <code>Future</code> 的定义:它是一个能产出值的异步计算(虽然该值可能为空,例如 <code>()</code> )。光看这个定义,可能会觉得很空洞,我们来看看一个简化版的 <code>Future</code> 特征:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>trait SimpleFuture {
|
|
|
type Output;
|
|
|
fn poll(&mut self, wake: fn()) -> Poll<Self::Output>;
|
|
|
}
|
|
|
|
|
|
enum Poll<T> {
|
|
|
Ready(T),
|
|
|
Pending,
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>在上一章中,我们提到过 <code>Future</code> 需要被执行器<code>poll</code>(轮询)后才能运行,诺,这里 <code>poll</code> 就来了,通过调用该方法,可以推进 <code>Future</code> 的进一步执行,直到被切走为止( 这里不好理解,但是你只需要知道 <code>Future</code> 并不能保证在一次 <code>poll</code> 中就被执行完,后面会详解介绍)。</p>
|
|
|
<p>若在当前 <code>poll</code> 中, <code>Future</code> 可以被完成,则会返回 <code>Poll::Ready(result)</code> ,反之则返回 <code>Poll::Pending</code>, 并且安排一个 <code>wake</code> 函数:当未来 <code>Future</code> 准备好进一步执行时, 该函数会被调用,然后管理该 <code>Future</code> 的执行器(例如上一章节中的<code>block_on</code>函数)会再次调用 <code>poll</code> 方法,此时 <code>Future</code> 就可以继续执行了。</p>
|
|
|
<p>如果没有 <code>wake</code> 方法,那执行器无法知道某个 <code>Future</code> 是否可以继续被执行,除非执行器定期的轮询每一个 <code>Future</code>,确认它是否能被执行,但这种作法效率较低。而有了 <code>wake</code>,<code>Future</code> 就可以主动通知执行器,然后执行器就可以精确的执行该 <code>Future</code>。 这种“事件通知 -> 执行”的方式要远比定期对所有 <code>Future</code> 进行一次全遍历来的高效。</p>
|
|
|
<p>也许大家还是迷迷糊糊的,没事,我们用一个例子来说明下。考虑一个需要从 <code>socket</code> 读取数据的场景:如果有数据,可以直接读取数据并返回 <code>Poll::Ready(data)</code>, 但如果没有数据,<code>Future</code> 会被阻塞且不会再继续执行,此时它会注册一个 <code>wake</code> 函数,当 <code>socket</code> 数据准备好时,该函数将被调用以通知执行器:我们的 <code>Future</code> 已经准备好了,可以继续执行。</p>
|
|
|
<p>下面的 <code>SocketRead</code> 结构体就是一个 <code>Future</code>:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>pub struct SocketRead<'a> {
|
|
|
socket: &'a Socket,
|
|
|
}
|
|
|
|
|
|
impl SimpleFuture for SocketRead<'_> {
|
|
|
type Output = Vec<u8>;
|
|
|
|
|
|
fn poll(&mut self, wake: fn()) -> Poll<Self::Output> {
|
|
|
if self.socket.has_data_to_read() {
|
|
|
// socket有数据,写入buffer中并返回
|
|
|
Poll::Ready(self.socket.read_buf())
|
|
|
} else {
|
|
|
// socket中还没数据
|
|
|
//
|
|
|
// 注册一个`wake`函数,当数据可用时,该函数会被调用,
|
|
|
// 然后当前Future的执行器会再次调用`poll`方法,此时就可以读取到数据
|
|
|
self.socket.set_readable_callback(wake);
|
|
|
Poll::Pending
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>这种 <code>Future</code> 模型允许将多个异步操作组合在一起,同时还无需任何内存分配。不仅仅如此,如果你需要同时运行多个 <code>Future</code>或链式调用多个 <code>Future</code> ,也可以通过无内存分配的状态机实现,例如:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>trait SimpleFuture {
|
|
|
type Output;
|
|
|
fn poll(&mut self, wake: fn()) -> Poll<Self::Output>;
|
|
|
}
|
|
|
|
|
|
enum Poll<T> {
|
|
|
Ready(T),
|
|
|
Pending,
|
|
|
}
|
|
|
|
|
|
/// 一个SimpleFuture,它会并发地运行两个Future直到它们完成
|
|
|
///
|
|
|
/// 之所以可以并发,是因为两个Future的轮询可以交替进行,一个阻塞,另一个就可以立刻执行,反之亦然
|
|
|
pub struct Join<FutureA, FutureB> {
|
|
|
// 结构体的每个字段都包含一个Future,可以运行直到完成.
|
|
|
// 等到Future完成后,字段会被设置为 `None`. 这样Future完成后,就不会再被轮询
|
|
|
a: Option<FutureA>,
|
|
|
b: Option<FutureB>,
|
|
|
}
|
|
|
|
|
|
impl<FutureA, FutureB> SimpleFuture for Join<FutureA, FutureB>
|
|
|
where
|
|
|
FutureA: SimpleFuture<Output = ()>,
|
|
|
FutureB: SimpleFuture<Output = ()>,
|
|
|
{
|
|
|
type Output = ();
|
|
|
fn poll(&mut self, wake: fn()) -> Poll<Self::Output> {
|
|
|
// 尝试去完成一个 Future `a`
|
|
|
if let Some(a) = &mut self.a {
|
|
|
if let Poll::Ready(()) = a.poll(wake) {
|
|
|
self.a.take();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 尝试去完成一个 Future `b`
|
|
|
if let Some(b) = &mut self.b {
|
|
|
if let Poll::Ready(()) = b.poll(wake) {
|
|
|
self.b.take();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if self.a.is_none() && self.b.is_none() {
|
|
|
// 两个 Future都已完成 - 我们可以成功地返回了
|
|
|
Poll::Ready(())
|
|
|
} else {
|
|
|
// 至少还有一个 Future 没有完成任务,因此返回 `Poll::Pending`.
|
|
|
// 当该 Future 再次准备好时,通过调用`wake()`函数来继续执行
|
|
|
Poll::Pending
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>上面代码展示了如何同时运行多个 <code>Future</code>, 且在此过程中没有任何内存分配,让并发编程更加高效。 类似的,多个<code>Future</code>也可以一个接一个的连续运行:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>/// 一个SimpleFuture, 它使用顺序的方式,一个接一个地运行两个Future
|
|
|
//
|
|
|
// 注意: 由于本例子用于演示,因此功能简单,`AndThenFut` 会假设两个 Future 在创建时就可用了.
|
|
|
// 而真实的`Andthen`允许根据第一个`Future`的输出来创建第二个`Future`,因此复杂的多。
|
|
|
pub struct AndThenFut<FutureA, FutureB> {
|
|
|
first: Option<FutureA>,
|
|
|
second: FutureB,
|
|
|
}
|
|
|
|
|
|
impl<FutureA, FutureB> SimpleFuture for AndThenFut<FutureA, FutureB>
|
|
|
where
|
|
|
FutureA: SimpleFuture<Output = ()>,
|
|
|
FutureB: SimpleFuture<Output = ()>,
|
|
|
{
|
|
|
type Output = ();
|
|
|
fn poll(&mut self, wake: fn()) -> Poll<Self::Output> {
|
|
|
if let Some(first) = &mut self.first {
|
|
|
match first.poll(wake) {
|
|
|
// 我们已经完成了第一个 Future, 可以将它移除, 然后准备开始运行第二个
|
|
|
Poll::Ready(()) => self.first.take(),
|
|
|
// 第一个 Future 还不能完成
|
|
|
Poll::Pending => return Poll::Pending,
|
|
|
};
|
|
|
}
|
|
|
|
|
|
// 运行到这里,说明第一个Future已经完成,尝试去完成第二个
|
|
|
self.second.poll(wake)
|
|
|
}
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>这些例子展示了在不需要内存对象分配以及深层嵌套回调的情况下,该如何使用 <code>Future</code> 特征去表达异步控制流。 在了解了基础的控制流后,我们再来看看真实的 <code>Future</code> 特征有何不同之处。</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>trait Future {
|
|
|
type Output;
|
|
|
fn poll(
|
|
|
// 首先值得注意的地方是,`self`的类型从`&mut self`变成了`Pin<&mut Self>`:
|
|
|
self: Pin<&mut Self>,
|
|
|
// 其次将`wake: fn()` 修改为 `cx: &mut Context<'_>`:
|
|
|
cx: &mut Context<'_>,
|
|
|
) -> Poll<Self::Output>;
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>首先这里多了一个 <code>Pin</code> ,关于它我们会在后面章节详细介绍,现在你只需要知道使用它可以创建一个无法被移动的 <code>Future</code> ,因为无法被移动,所以它将具有固定的内存地址,意味着我们可以存储它的指针(如果内存地址可能会变动,那存储指针地址将毫无意义!),也意味着可以实现一个自引用数据结构: <code>struct MyFut { a: i32, ptr_to_a: *const i32 }</code>。 而对于 <code>async/await</code> 来说,<code>Pin</code> 是不可或缺的关键特性。</p>
|
|
|
<p>其次,从 <code>wake: fn()</code> 变成了 <code>&mut Context<'_></code> 。意味着 <code>wake</code> 函数可以携带数据了,为何要携带数据?考虑一个真实世界的场景,一个复杂应用例如 web 服务器可能有数千连接同时在线,那么同时就有数千 <code>Future</code> 在被同时管理着,如果不能携带数据,当一个 <code>Future</code> 调用 <code>wake</code> 后,执行器该如何知道是哪个 <code>Future</code> 调用了 <code>wake</code> ,然后进一步去 <code>poll</code> 对应的 <code>Future</code> ?没有办法!那之前的例子为啥就可以使用没有携带数据的 <code>wake</code> ? 因为足够简单,不存在歧义性。</p>
|
|
|
<p>总之,在正式场景要进行 <code>wake</code> ,就必须携带上数据。 而 <code>Context</code> 类型通过提供一个 <code>Waker</code> 类型的值,就可以用来唤醒特定的的任务。</p>
|
|
|
<h2 id="使用-waker-来唤醒任务"><a class="header" href="#使用-waker-来唤醒任务">使用 Waker 来唤醒任务</a></h2>
|
|
|
<p>对于 <code>Future</code> 来说,第一次被 <code>poll</code> 时无法完成任务是很正常的。但它需要确保在未来一旦准备好时,可以通知执行器再次对其进行 <code>poll</code> 进而继续往下执行,该通知就是通过 <code>Waker</code> 类型完成的。</p>
|
|
|
<p><code>Waker</code> 提供了一个 <code>wake()</code> 方法可以用于告诉执行器:相关的任务可以被唤醒了,此时执行器就可以对相应的 <code>Future</code> 再次进行 <code>poll</code> 操作。</p>
|
|
|
<h4 id="构建一个定时器"><a class="header" href="#构建一个定时器">构建一个定时器</a></h4>
|
|
|
<p>下面一起来实现一个简单的定时器 <code>Future</code> 。为了让例子尽量简单,当计时器创建时,我们会启动一个线程接着让该线程进入睡眠,等睡眠结束后再通知给 <code>Future</code> 。</p>
|
|
|
<p>注意本例子还会在后面继续使用,因此我们重新创建一个工程来演示:使用 <code>cargo new --lib timer_future</code> 来创建一个新工程,在 <code>lib</code> 包的根路径 <code>src/lib.rs</code> 中添加以下内容:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>use std::{
|
|
|
future::Future,
|
|
|
pin::Pin,
|
|
|
sync::{Arc, Mutex},
|
|
|
task::{Context, Poll, Waker},
|
|
|
thread,
|
|
|
time::Duration,
|
|
|
};
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>继续来实现 <code>Future</code> 定时器,之前提到: 新建线程在睡眠结束后会需要将状态同步给定时器 <code>Future</code> ,由于是多线程环境,我们需要使用 <code>Arc<Mutex<T>></code> 来作为一个共享状态,用于在新线程和 <code>Future</code> 定时器间共享。</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>pub struct TimerFuture {
|
|
|
shared_state: Arc<Mutex<SharedState>>,
|
|
|
}
|
|
|
|
|
|
/// 在Future和等待的线程间共享状态
|
|
|
struct SharedState {
|
|
|
/// 定时(睡眠)是否结束
|
|
|
completed: bool,
|
|
|
|
|
|
/// 当睡眠结束后,线程可以用`waker`通知`TimerFuture`来唤醒任务
|
|
|
waker: Option<Waker>,
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>下面给出 <code>Future</code> 的具体实现:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>impl Future for TimerFuture {
|
|
|
type Output = ();
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
// 通过检查共享状态,来确定定时器是否已经完成
|
|
|
let mut shared_state = self.shared_state.lock().unwrap();
|
|
|
if shared_state.completed {
|
|
|
Poll::Ready(())
|
|
|
} else {
|
|
|
// 设置`waker`,这样新线程在睡眠(计时)结束后可以唤醒当前的任务,接着再次对`Future`进行`poll`操作,
|
|
|
//
|
|
|
// 下面的`clone`每次被`poll`时都会发生一次,实际上,应该是只`clone`一次更加合理。
|
|
|
// 选择每次都`clone`的原因是: `TimerFuture`可以在执行器的不同任务间移动,如果只克隆一次,
|
|
|
// 那么获取到的`waker`可能已经被篡改并指向了其它任务,最终导致执行器运行了错误的任务
|
|
|
shared_state.waker = Some(cx.waker().clone());
|
|
|
Poll::Pending
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>代码很简单,只要新线程设置了 <code>shared_state.completed = true</code> ,那任务就能顺利结束。如果没有设置,会为当前的任务克隆一份 <code>Waker</code> ,这样新线程就可以使用它来唤醒当前的任务。</p>
|
|
|
<p>最后,再来创建一个 API 用于构建定时器和启动计时线程:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>impl TimerFuture {
|
|
|
/// 创建一个新的`TimerFuture`,在指定的时间结束后,该`Future`可以完成
|
|
|
pub fn new(duration: Duration) -> Self {
|
|
|
let shared_state = Arc::new(Mutex::new(SharedState {
|
|
|
completed: false,
|
|
|
waker: None,
|
|
|
}));
|
|
|
|
|
|
// 创建新线程
|
|
|
let thread_shared_state = shared_state.clone();
|
|
|
thread::spawn(move || {
|
|
|
// 睡眠指定时间实现计时功能
|
|
|
thread::sleep(duration);
|
|
|
let mut shared_state = thread_shared_state.lock().unwrap();
|
|
|
// 通知执行器定时器已经完成,可以继续`poll`对应的`Future`了
|
|
|
shared_state.completed = true;
|
|
|
if let Some(waker) = shared_state.waker.take() {
|
|
|
waker.wake()
|
|
|
}
|
|
|
});
|
|
|
|
|
|
TimerFuture { shared_state }
|
|
|
}
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>至此,一个简单的定时器 <code>Future</code> 就已创建成功,那么该如何使用它呢?相信部分爱动脑筋的读者已经猜到了:我们需要创建一个执行器,才能让程序动起来。</p>
|
|
|
<h2 id="执行器-executor"><a class="header" href="#执行器-executor">执行器 Executor</a></h2>
|
|
|
<p>Rust 的 <code>Future</code> 是惰性的:只有屁股上拍一拍,它才会努力动一动。其中一个推动它的方式就是在 <code>async</code> 函数中使用 <code>.await</code> 来调用另一个 <code>async</code> 函数,但是这个只能解决 <code>async</code> 内部的问题,那么这些最外层的 <code>async</code> 函数,谁来推动它们运行呢?答案就是我们之前多次提到的执行器 <code>executor</code> 。</p>
|
|
|
<p>执行器会管理一批 <code>Future</code> (最外层的 <code>async</code> 函数),然后通过不停地 <code>poll</code> 推动它们直到完成。 最开始,执行器会先 <code>poll</code> 一次 <code>Future</code> ,后面就不会主动去 <code>poll</code> 了,而是等待 <code>Future</code> 通过调用 <code>wake</code> 函数来通知它可以继续,它才会继续去 <code>poll</code> 。这种 <strong>wake 通知然后 poll</strong> 的方式会不断重复,直到 <code>Future</code> 完成。</p>
|
|
|
<h4 id="构建执行器"><a class="header" href="#构建执行器">构建执行器</a></h4>
|
|
|
<p>下面我们将实现一个简单的执行器,它可以同时并发运行多个 <code>Future</code> 。例子中,需要用到 <code>futures</code> 包的 <code>ArcWake</code> 特征,它可以提供一个方便的途径去构建一个 <code>Waker</code> 。编辑 <code>Cargo.toml</code> ,添加下面依赖:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>[dependencies]
|
|
|
futures = "0.3"
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>在之前的内容中,我们在 <code>src/lib.rs</code> 中创建了定时器 <code>Future</code> ,现在在 <code>src/main.rs</code> 中来创建程序的主体内容,开始之前,先引入所需的包:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>use {
|
|
|
futures::{
|
|
|
future::{BoxFuture, FutureExt},
|
|
|
task::{waker_ref, ArcWake},
|
|
|
},
|
|
|
std::{
|
|
|
future::Future,
|
|
|
sync::mpsc::{sync_channel, Receiver, SyncSender},
|
|
|
sync::{Arc, Mutex},
|
|
|
task::{Context, Poll},
|
|
|
time::Duration,
|
|
|
},
|
|
|
// 引入之前实现的定时器模块
|
|
|
timer_future::TimerFuture,
|
|
|
};
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>执行器需要从一个消息通道( <code>channel</code> )中拉取事件,然后运行它们。当一个任务准备好后(可以继续执行),它会将自己放入消息通道中,然后等待执行器 <code>poll</code> 。</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>/// 任务执行器,负责从通道中接收任务然后执行
|
|
|
struct Executor {
|
|
|
ready_queue: Receiver<Arc<Task>>,
|
|
|
}
|
|
|
|
|
|
/// `Spawner`负责创建新的`Future`然后将它发送到任务通道中
|
|
|
#[derive(Clone)]
|
|
|
struct Spawner {
|
|
|
task_sender: SyncSender<Arc<Task>>,
|
|
|
}
|
|
|
|
|
|
/// 一个Future,它可以调度自己(将自己放入任务通道中),然后等待执行器去`poll`
|
|
|
struct Task {
|
|
|
/// 进行中的Future,在未来的某个时间点会被完成
|
|
|
///
|
|
|
/// 按理来说`Mutex`在这里是多余的,因为我们只有一个线程来执行任务。但是由于
|
|
|
/// Rust并不聪明,它无法知道`Future`只会在一个线程内被修改,并不会被跨线程修改。因此
|
|
|
/// 我们需要使用`Mutex`来满足这个笨笨的编译器对线程安全的执着。
|
|
|
///
|
|
|
/// 如果是生产级的执行器实现,不会使用`Mutex`,因为会带来性能上的开销,取而代之的是使用`UnsafeCell`
|
|
|
future: Mutex<Option<BoxFuture<'static, ()>>>,
|
|
|
|
|
|
/// 可以将该任务自身放回到任务通道中,等待执行器的poll
|
|
|
task_sender: SyncSender<Arc<Task>>,
|
|
|
}
|
|
|
|
|
|
fn new_executor_and_spawner() -> (Executor, Spawner) {
|
|
|
// 任务通道允许的最大缓冲数(任务队列的最大长度)
|
|
|
// 当前的实现仅仅是为了简单,在实际的执行中,并不会这么使用
|
|
|
const MAX_QUEUED_TASKS: usize = 10_000;
|
|
|
let (task_sender, ready_queue) = sync_channel(MAX_QUEUED_TASKS);
|
|
|
(Executor { ready_queue }, Spawner { task_sender })
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>下面再来添加一个方法用于生成 <code>Future</code> , 然后将它放入任务通道中:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>impl Spawner {
|
|
|
fn spawn(&self, future: impl Future<Output = ()> + 'static + Send) {
|
|
|
let future = future.boxed();
|
|
|
let task = Arc::new(Task {
|
|
|
future: Mutex::new(Some(future)),
|
|
|
task_sender: self.task_sender.clone(),
|
|
|
});
|
|
|
self.task_sender.send(task).expect("任务队列已满");
|
|
|
}
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>在执行器 <code>poll</code> 一个 <code>Future</code> 之前,首先需要调用 <code>wake</code> 方法进行唤醒,然后再由 <code>Waker</code> 负责调度该任务并将其放入任务通道中。创建 <code>Waker</code> 的最简单的方式就是实现 <code>ArcWake</code> 特征,先来为我们的任务实现 <code>ArcWake</code> 特征,这样它们就能被转变成 <code>Waker</code> 然后被唤醒:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>impl ArcWake for Task {
|
|
|
fn wake_by_ref(arc_self: &Arc<Self>) {
|
|
|
// 通过发送任务到任务管道的方式来实现`wake`,这样`wake`后,任务就能被执行器`poll`
|
|
|
let cloned = arc_self.clone();
|
|
|
arc_self
|
|
|
.task_sender
|
|
|
.send(cloned)
|
|
|
.expect("任务队列已满");
|
|
|
}
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>当任务实现了 <code>ArcWake</code> 特征后,它就变成了 <code>Waker</code> ,在调用 <code>wake()</code> 对其唤醒后会将任务复制一份所有权( <code>Arc</code> ),然后将其发送到任务通道中。最后我们的执行器将从通道中获取任务,然后进行 <code>poll</code> 执行:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>impl Executor {
|
|
|
fn run(&self) {
|
|
|
while let Ok(task) = self.ready_queue.recv() {
|
|
|
// 获取一个future,若它还没有完成(仍然是Some,不是None),则对它进行一次poll并尝试完成它
|
|
|
let mut future_slot = task.future.lock().unwrap();
|
|
|
if let Some(mut future) = future_slot.take() {
|
|
|
// 基于任务自身创建一个 `LocalWaker`
|
|
|
let waker = waker_ref(&task);
|
|
|
let context = &mut Context::from_waker(&*waker);
|
|
|
// `BoxFuture<T>`是`Pin<Box<dyn Future<Output = T> + Send + 'static>>`的类型别名
|
|
|
// 通过调用`as_mut`方法,可以将上面的类型转换成`Pin<&mut dyn Future + Send + 'static>`
|
|
|
if future.as_mut().poll(context).is_pending() {
|
|
|
// Future还没执行完,因此将它放回任务中,等待下次被poll
|
|
|
*future_slot = Some(future);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>恭喜!我们终于拥有了自己的执行器,下面再来写一段代码使用该执行器去运行之前的定时器 <code>Future</code> :</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021">fn main() {
|
|
|
let (executor, spawner) = new_executor_and_spawner();
|
|
|
|
|
|
// 生成一个任务
|
|
|
spawner.spawn(async {
|
|
|
println!("howdy!");
|
|
|
// 创建定时器Future,并等待它完成
|
|
|
TimerFuture::new(Duration::new(2, 0)).await;
|
|
|
println!("done!");
|
|
|
});
|
|
|
|
|
|
// drop掉任务,这样执行器就知道任务已经完成,不会再有新的任务进来
|
|
|
drop(spawner);
|
|
|
|
|
|
// 运行执行器直到任务队列为空
|
|
|
// 任务运行后,会先打印`howdy!`, 暂停2秒,接着打印 `done!`
|
|
|
executor.run();
|
|
|
}</code></pre>
|
|
|
<h2 id="执行器和系统-io"><a class="header" href="#执行器和系统-io">执行器和系统 IO</a></h2>
|
|
|
<p>前面我们一起看过一个使用 <code>Future</code> 从 <code>Socket</code> 中异步读取数据的例子:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>pub struct SocketRead<'a> {
|
|
|
socket: &'a Socket,
|
|
|
}
|
|
|
|
|
|
impl SimpleFuture for SocketRead<'_> {
|
|
|
type Output = Vec<u8>;
|
|
|
|
|
|
fn poll(&mut self, wake: fn()) -> Poll<Self::Output> {
|
|
|
if self.socket.has_data_to_read() {
|
|
|
// socket有数据,写入buffer中并返回
|
|
|
Poll::Ready(self.socket.read_buf())
|
|
|
} else {
|
|
|
// socket中还没数据
|
|
|
//
|
|
|
// 注册一个`wake`函数,当数据可用时,该函数会被调用,
|
|
|
// 然后当前Future的执行器会再次调用`poll`方法,此时就可以读取到数据
|
|
|
self.socket.set_readable_callback(wake);
|
|
|
Poll::Pending
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>该例子中,<code>Future</code> 将从 <code>Socket</code> 读取数据,若当前还没有数据,则会让出当前线程的所有权,允许执行器去执行其它的 <code>Future</code> 。当数据准备好后,会调用 <code>wake()</code> 函数将该 <code>Future</code> 的任务放入任务通道中,等待执行器的 <code>poll</code> 。</p>
|
|
|
<p>关于该流程已经反复讲了很多次,相信大家应该非常清楚了。然而该例子中还有一个疑问没有解决:</p>
|
|
|
<ul>
|
|
|
<li><code>set_readable_callback</code> 方法到底是怎么工作的?怎么才能知道 <code>socket</code> 中的数据已经可以被读取了?</li>
|
|
|
</ul>
|
|
|
<p>关于第二点,其中一个简单粗暴的方法就是使用一个新线程不停的检查 <code>socket</code> 中是否有了数据,当有了后,就调用 <code>wake()</code> 函数。该方法确实可以满足需求,但是性能着实太低了,需要为每个阻塞的 <code>Future</code> 都创建一个单独的线程!</p>
|
|
|
<p>在现实世界中,该问题往往是通过操作系统提供的 <code>IO</code> 多路复用机制来完成,例如 <code>Linux</code> 中的 <strong><code>epoll</code></strong>,<code>FreeBSD</code> 和 <code>macOS</code> 中的 <strong><code>kqueue</code></strong> ,<code>Windows</code> 中的 <strong><code>IOCP</code></strong>, <code>Fuchisa</code>中的 <strong><code>ports</code></strong> 等(可以通过 Rust 的跨平台包 <code>mio</code> 来使用它们)。借助 IO 多路复用机制,可以实现一个线程同时阻塞地去等待多个异步 IO 事件,一旦某个事件完成就立即退出阻塞并返回数据。相关实现类似于以下代码:</p>
|
|
|
<pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span>struct IoBlocker {
|
|
|
/* ... */
|
|
|
}
|
|
|
|
|
|
struct Event {
|
|
|
// Event的唯一ID,该事件发生后,就会被监听起来
|
|
|
id: usize,
|
|
|
|
|
|
// 一组需要等待或者已发生的信号
|
|
|
signals: Signals,
|
|
|
}
|
|
|
|
|
|
impl IoBlocker {
|
|
|
/// 创建需要阻塞等待的异步IO事件的集合
|
|
|
fn new() -> Self { /* ... */ }
|
|
|
|
|
|
/// 对指定的IO事件表示兴趣
|
|
|
fn add_io_event_interest(
|
|
|
&self,
|
|
|
|
|
|
/// 事件所绑定的socket
|
|
|
io_object: &IoObject,
|
|
|
|
|
|
event: Event,
|
|
|
) { /* ... */ }
|
|
|
|
|
|
/// 进入阻塞,直到某个事件出现
|
|
|
fn block(&self) -> Event { /* ... */ }
|
|
|
}
|
|
|
|
|
|
let mut io_blocker = IoBlocker::new();
|
|
|
io_blocker.add_io_event_interest(
|
|
|
&socket_1,
|
|
|
Event { id: 1, signals: READABLE },
|
|
|
);
|
|
|
io_blocker.add_io_event_interest(
|
|
|
&socket_2,
|
|
|
Event { id: 2, signals: READABLE | WRITABLE },
|
|
|
);
|
|
|
let event = io_blocker.block();
|
|
|
|
|
|
// 当socket的数据可以读取时,打印 "Socket 1 is now READABLE"
|
|
|
println!("Socket {:?} is now {:?}", event.id, event.signals);
|
|
|
<span class="boring">}</span></code></pre>
|
|
|
<p>这样,我们只需要一个执行器线程,它会接收 IO 事件并将其分发到对应的 <code>Waker</code> 中,接着后者会唤醒相关的任务,最终通过执行器 <code>poll</code> 后,任务可以顺利地继续执行, 这种 IO 读取流程可以不停的循环,直到 <code>socket</code> 关闭。</p>
|
|
|
|
|
|
</main>
|
|
|
|
|
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
|
<!-- Mobile navigation buttons -->
|
|
|
<a rel="prev" href="../../advance/async/getting-started.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 246.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z"/></svg></span>
|
|
|
</a>
|
|
|
|
|
|
<a rel="next prefetch" href="../../advance/async/pin-unpin.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z"/></svg></span>
|
|
|
</a>
|
|
|
|
|
|
<div style="clear: both"></div>
|
|
|
</nav>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
|
<a rel="prev" href="../../advance/async/getting-started.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 246.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z"/></svg></span>
|
|
|
</a>
|
|
|
|
|
|
<a rel="next prefetch" href="../../advance/async/pin-unpin.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z"/></svg></span>
|
|
|
</a>
|
|
|
</nav>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<template id=fa-eye><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M288 32c-80.8 0-145.5 36.8-192.6 80.6C48.6 156 17.3 208 2.5 243.7c-3.3 7.9-3.3 16.7 0 24.6C17.3 304 48.6 356 95.4 399.4C142.5 443.2 207.2 480 288 480s145.5-36.8 192.6-80.6c46.8-43.5 78.1-95.4 93-131.1c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C433.5 68.8 368.8 32 288 32zM432 256c0 79.5-64.5 144-144 144s-144-64.5-144-144s64.5-144 144-144s144 64.5 144 144zM288 192c0 35.3-28.7 64-64 64c-11.5 0-22.3-3-31.6-8.4c-.2 2.8-.4 5.5-.4 8.4c0 53 43 96 96 96s96-43 96-96s-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6z"/></svg></span></template>
|
|
|
<template id=fa-eye-slash><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M38.8 5.1C28.4-3.1 13.3-1.2 5.1 9.2S-1.2 34.7 9.2 42.9l592 464c10.4 8.2 25.5 6.3 33.7-4.1s6.3-25.5-4.1-33.7L525.6 386.7c39.6-40.6 66.4-86.1 79.9-118.4c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C465.5 68.8 400.8 32 320 32c-68.2 0-125 26.3-169.3 60.8L38.8 5.1zM223.1 149.5C248.6 126.2 282.7 112 320 112c79.5 0 144 64.5 144 144c0 24.9-6.3 48.3-17.4 68.7L408 294.5c5.2-11.8 8-24.8 8-38.5c0-53-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6c0 10.2-2.4 19.8-6.6 28.3l-90.3-70.8zm223.1 298L373 389.9c-16.4 6.5-34.3 10.1-53 10.1c-79.5 0-144-64.5-144-144c0-6.9 .5-13.6 1.4-20.2L83.1 161.5C60.3 191.2 44 220.8 34.5 243.7c-3.3 7.9-3.3 16.7 0 24.6c14.9 35.7 46.2 87.7 93 131.1C174.5 443.2 239.2 480 320 480c47.8 0 89.9-12.9 126.2-32.5z"/></svg></span></template>
|
|
|
<template id=fa-copy><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M502.6 70.63l-61.25-61.25C435.4 3.371 427.2 0 418.7 0H255.1c-35.35 0-64 28.66-64 64l.0195 256C192 355.4 220.7 384 256 384h192c35.2 0 64-28.8 64-64V93.25C512 84.77 508.6 76.63 502.6 70.63zM464 320c0 8.836-7.164 16-16 16H255.1c-8.838 0-16-7.164-16-16L239.1 64.13c0-8.836 7.164-16 16-16h128L384 96c0 17.67 14.33 32 32 32h47.1V320zM272 448c0 8.836-7.164 16-16 16H63.1c-8.838 0-16-7.164-16-16L47.98 192.1c0-8.836 7.164-16 16-16H160V128H63.99c-35.35 0-64 28.65-64 64l.0098 256C.002 483.3 28.66 512 64 512h192c35.2 0 64-28.8 64-64v-32h-47.1L272 448z"/></svg></span></template>
|
|
|
<template id=fa-play><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80V432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"/></svg></span></template>
|
|
|
<template id=fa-clock-rotate-left><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M75 75L41 41C25.9 25.9 0 36.6 0 57.9V168c0 13.3 10.7 24 24 24H134.1c21.4 0 32.1-25.9 17-41l-30.8-30.8C155 85.5 203 64 256 64c106 0 192 86 192 192s-86 192-192 192c-40.8 0-78.6-12.7-109.7-34.4c-14.5-10.1-34.4-6.6-44.6 7.9s-6.6 34.4 7.9 44.6C151.2 495 201.7 512 256 512c141.4 0 256-114.6 256-256S397.4 0 256 0C185.3 0 121.3 28.7 75 75zm181 53c-13.3 0-24 10.7-24 24V256c0 6.4 2.5 12.5 7 17l72 72c9.4 9.4 24.6 9.4 33.9 0s9.4-24.6 0-33.9l-65-65V152c0-13.3-10.7-24-24-24z"/></svg></span></template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
window.playground_copyable = true;
|
|
|
</script>
|
|
|
|
|
|
<script src="../../ace-2a3cd908.js"></script>
|
|
|
<script src="../../mode-rust-2c9d5c9a.js"></script>
|
|
|
<script src="../../editor-16ca416c.js"></script>
|
|
|
<script src="../../theme-dawn-4493f9c8.js"></script>
|
|
|
<script src="../../theme-tomorrow_night-9dbe62a9.js"></script>
|
|
|
|
|
|
<script src="../../elasticlunr-ef4e11c1.min.js"></script>
|
|
|
<script src="../../mark-09e88c2c.min.js"></script>
|
|
|
<script src="../../searcher-c2a407aa.js"></script>
|
|
|
|
|
|
<script src="../../clipboard-1626706a.min.js"></script>
|
|
|
<script src="../../highlight-abc7f01d.js"></script>
|
|
|
<script src="../../book-a0b12cfe.js"></script>
|
|
|
|
|
|
<!-- Custom JS scripts -->
|
|
|
<script src="../../assets/custom-7d8ae1df.js"></script>
|
|
|
<script src="../../assets/bigPicture-be03f9c8.js"></script>
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
</body>
|
|
|
</html>
|