mentat/docs/apis/rust/tokio/executor/current_thread/index.html
2018-06-22 12:08:32 +01:00

290 lines
No EOL
16 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="rustdoc">
<meta name="description" content="API documentation for the Rust `current_thread` mod in crate `tokio`.">
<meta name="keywords" content="rust, rustlang, rust-lang, current_thread">
<title>tokio::executor::current_thread - Rust</title>
<link rel="stylesheet" type="text/css" href="../../../normalize.css">
<link rel="stylesheet" type="text/css" href="../../../rustdoc.css"
id="mainThemeStyle">
<link rel="stylesheet" type="text/css" href="../../../dark.css">
<link rel="stylesheet" type="text/css" href="../../../light.css" id="themeStyle">
<script src="../../../storage.js"></script>
</head>
<body class="rustdoc mod">
<!--[if lte IE 8]>
<div class="warning">
This old browser is unsupported and will most likely display funky
things.
</div>
<![endif]-->
<nav class="sidebar">
<div class="sidebar-menu">&#9776;</div>
<p class='location'>Module current_thread</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'><a href='../../index.html'>tokio</a>::<wbr><a href='../index.html'>executor</a></p><script>window.sidebarCurrent = {name: 'current_thread', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div>
</nav>
<div class="theme-picker">
<button id="theme-picker" aria-label="Pick another theme!">
<img src="../../../brush.svg" width="18" alt="Pick another theme!">
</button>
<div id="theme-choices"></div>
</div>
<script src="../../../theme.js"></script>
<nav class="sub">
<form class="search-form js-only">
<div class="search-container">
<input class="search-input" name="search"
autocomplete="off"
placeholder="Click or press S to search, ? for more options…"
type="search">
</div>
</form>
</nav>
<section id='main' class="content"><h1 class='fqn'><span class='in-band'>Module <a href='../../index.html'>tokio</a>::<wbr><a href='../index.html'>executor</a>::<wbr><a class="mod" href=''>current_thread</a></span><span class='out-of-band'><span id='render-detail'>
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
[<span class='inner'>&#x2212;</span>]
</a>
</span><a class='srclink' href='../../../src/tokio/executor/current_thread/mod.rs.html#1-835' title='goto source code'>[src]</a></span></h1><div class='docblock'><p>Execute many tasks concurrently on the current thread.</p>
<p><a href="struct.CurrentThread.html"><code>CurrentThread</code></a> is an executor that keeps tasks on the same thread that
they were spawned from. This allows it to execute futures that are not
<code>Send</code>.</p>
<p>A single <a href="struct.CurrentThread.html"><code>CurrentThread</code></a> instance is able to efficiently manage a large
number of tasks and will attempt to schedule all tasks fairly.</p>
<p>All tasks that are being managed by a <a href="struct.CurrentThread.html"><code>CurrentThread</code></a> executor are able to
spawn additional tasks by calling <a href="fn.spawn.html"><code>spawn</code></a>. This function only works from
within the context of a running <a href="struct.CurrentThread.html"><code>CurrentThread</code></a> instance.</p>
<p>The easiest way to start a new <a href="struct.CurrentThread.html"><code>CurrentThread</code></a> executor is to call
<a href="fn.block_on_all.html"><code>block_on_all</code></a> with an initial task to seed the executor.</p>
<p>For example:</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">futures</span>::<span class="ident">future</span>::<span class="ident">lazy</span>;
<span class="comment">// Calling execute here results in a panic</span>
<span class="comment">// current_thread::spawn(my_future);</span>
<span class="ident">current_thread</span>::<span class="ident">block_on_all</span>(<span class="ident">lazy</span>(<span class="op">||</span> {
<span class="comment">// The execution context is setup, futures may be executed.</span>
<span class="ident">current_thread</span>::<span class="ident">spawn</span>(<span class="ident">lazy</span>(<span class="op">||</span> {
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;called from the current thread executor&quot;</span>);
<span class="prelude-val">Ok</span>(())
}));
<span class="prelude-val">Ok</span>::<span class="op">&lt;</span><span class="kw">_</span>, ()<span class="op">&gt;</span>(())
}));</pre>
<p>The <code>block_on_all</code> function will block the current thread until <strong>all</strong>
tasks that have been spawned onto the <a href="struct.CurrentThread.html"><code>CurrentThread</code></a> instance have
completed.</p>
<p>More fine-grain control can be achieved by using <a href="struct.CurrentThread.html"><code>CurrentThread</code></a> directly.</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">futures</span>::<span class="ident">future</span>::{<span class="ident">lazy</span>, <span class="ident">empty</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">time</span>::<span class="ident">Duration</span>;
<span class="comment">// Calling execute here results in a panic</span>
<span class="comment">// current_thread::spawn(my_future);</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">current_thread</span> <span class="op">=</span> <span class="ident">CurrentThread</span>::<span class="ident">new</span>();
<span class="comment">// Spawn a task, the task is not executed yet.</span>
<span class="ident">current_thread</span>.<span class="ident">spawn</span>(<span class="ident">lazy</span>(<span class="op">||</span> {
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;Spawning a task&quot;</span>);
<span class="prelude-val">Ok</span>(())
}));
<span class="comment">// Spawn a task that never completes</span>
<span class="ident">current_thread</span>.<span class="ident">spawn</span>(<span class="ident">empty</span>());
<span class="comment">// Run the executor, but only until the provided future completes. This</span>
<span class="comment">// provides the opportunity to start executing previously spawned tasks.</span>
<span class="kw">let</span> <span class="ident">res</span> <span class="op">=</span> <span class="ident">current_thread</span>.<span class="ident">block_on</span>(<span class="ident">lazy</span>(<span class="op">||</span> {
<span class="prelude-val">Ok</span>::<span class="op">&lt;</span><span class="kw">_</span>, ()<span class="op">&gt;</span>(<span class="string">&quot;Hello&quot;</span>)
})).<span class="ident">unwrap</span>();
<span class="comment">// Now, run the executor for *at most* 1 second. Since a task was spawned</span>
<span class="comment">// that never completes, this function will return with an error.</span>
<span class="ident">current_thread</span>.<span class="ident">run_timeout</span>(<span class="ident">Duration</span>::<span class="ident">from_secs</span>(<span class="number">1</span>)).<span class="ident">unwrap_err</span>();</pre>
<h1 id="execution-model" class="section-header"><a href="#execution-model">Execution model</a></h1>
<p>Internally, <a href="struct.CurrentThread.html"><code>CurrentThread</code></a> maintains a queue. When one of its tasks is
notified, the task gets added to the queue. The executor will pop tasks from
the queue and call <a href="https://docs.rs/futures/0.1/futures/future/trait.Future.html#tymethod.poll"><code>Future::poll</code></a>. If the task gets notified while it is
being executed, it won't get re-executed until all other tasks currently in
the queue get polled.</p>
<p>Before the task is polled, a thread-local variable referencing the current
<a href="struct.CurrentThread.html"><code>CurrentThread</code></a> instance is set. This enables <a href="fn.spawn.html"><code>spawn</code></a> to spawn new tasks
onto the same executor without having to thread through a handle value.</p>
<p>If the <a href="struct.CurrentThread.html"><code>CurrentThread</code></a> instance still has uncompleted tasks, but none of
these tasks are ready to be polled, the current thread is put to sleep. When
a task is notified, the thread is woken up and processing resumes.</p>
<p>All tasks managed by <a href="struct.CurrentThread.html"><code>CurrentThread</code></a> remain on the current thread. When a
task completes, it is dropped.</p>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table>
<tr class=' module-item'>
<td><a class="struct" href="struct.BlockError.html"
title='struct tokio::executor::current_thread::BlockError'>BlockError</a></td>
<td class='docblock-short'>
<p>Error returned by the <code>block_on</code> function.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.CurrentThread.html"
title='struct tokio::executor::current_thread::CurrentThread'>CurrentThread</a></td>
<td class='docblock-short'>
<p>Executes tasks on the current thread</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.Entered.html"
title='struct tokio::executor::current_thread::Entered'>Entered</a></td>
<td class='docblock-short'>
<p>A <code>CurrentThread</code> instance bound to a supplied execution context.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.Handle.html"
title='struct tokio::executor::current_thread::Handle'>Handle</a></td>
<td class='docblock-short'>
<p>Handle to spawn a future on the corresponding <code>CurrentThread</code> instance</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.RunError.html"
title='struct tokio::executor::current_thread::RunError'>RunError</a></td>
<td class='docblock-short'>
<p>Error returned by the <code>run</code> function.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.RunTimeoutError.html"
title='struct tokio::executor::current_thread::RunTimeoutError'>RunTimeoutError</a></td>
<td class='docblock-short'>
<p>Error returned by the <code>run_timeout</code> function.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.TaskExecutor.html"
title='struct tokio::executor::current_thread::TaskExecutor'>TaskExecutor</a></td>
<td class='docblock-short'>
<p>Executes futures on the current thread.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.Turn.html"
title='struct tokio::executor::current_thread::Turn'>Turn</a></td>
<td class='docblock-short'>
<p>Returned by the <code>turn</code> function.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.TurnError.html"
title='struct tokio::executor::current_thread::TurnError'>TurnError</a></td>
<td class='docblock-short'>
<p>Error returned by the <code>turn</code> function.</p>
</td>
</tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
<table>
<tr class=' module-item'>
<td><a class="fn" href="fn.block_on_all.html"
title='fn tokio::executor::current_thread::block_on_all'>block_on_all</a></td>
<td class='docblock-short'>
<p>Run the executor bootstrapping the execution with the provided future.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="fn" href="fn.spawn.html"
title='fn tokio::executor::current_thread::spawn'>spawn</a></td>
<td class='docblock-short'>
<p>Executes a future on the current thread.</p>
</td>
</tr></table></section>
<section id='search' class="content hidden"></section>
<section class="footer"></section>
<aside id="help" class="hidden">
<div>
<h1 class="hidden">Help</h1>
<div class="shortcuts">
<h2>Keyboard Shortcuts</h2>
<dl>
<dt><kbd>?</kbd></dt>
<dd>Show this help dialog</dd>
<dt><kbd>S</kbd></dt>
<dd>Focus the search field</dd>
<dt><kbd></kbd></dt>
<dd>Move up in search results</dd>
<dt><kbd></kbd></dt>
<dd>Move down in search results</dd>
<dt><kbd></kbd></dt>
<dd>Switch tab</dd>
<dt><kbd>&#9166;</kbd></dt>
<dd>Go to active search result</dd>
<dt><kbd>+</kbd></dt>
<dd>Expand all sections</dd>
<dt><kbd>-</kbd></dt>
<dd>Collapse all sections</dd>
</dl>
</div>
<div class="infos">
<h2>Search Tricks</h2>
<p>
Prefix searches with a type followed by a colon (e.g.
<code>fn:</code>) to restrict the search to a given type.
</p>
<p>
Accepted types are: <code>fn</code>, <code>mod</code>,
<code>struct</code>, <code>enum</code>,
<code>trait</code>, <code>type</code>, <code>macro</code>,
and <code>const</code>.
</p>
<p>
Search functions by type signature (e.g.
<code>vec -> usize</code> or <code>* -> vec</code>)
</p>
</div>
</div>
</aside>
<script>
window.rootPath = "../../../";
window.currentCrate = "tokio";
</script>
<script src="../../../main.js"></script>
<script defer src="../../../search-index.js"></script>
</body>
</html>