Files
entt/md_docs_md_process.html
2023-06-15 11:47:04 +02:00

201 lines
15 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.6"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>EnTT: Crash Course: cooperative scheduler</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen-awesome.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">EnTT<span id="projectnumber">&#160;3.12.0</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.6 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
</div><!-- top -->
<div><div class="header">
<div class="headertitle"><div class="title">Crash Course: cooperative scheduler </div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h1><a class="anchor" id="autotoc_md179"></a>
Introduction</h1>
<p>Processes are a useful tool to work around the strict definition of a system and introduce logic in a different way, usually without resorting to other component types.<br />
<code>EnTT</code> offers minimal support to this paradigm by introducing a few classes used to define and execute cooperative processes.</p>
<h1><a class="anchor" id="autotoc_md180"></a>
The process</h1>
<p>A typical task inherits from the <code>process</code> class template that stays true to the CRTP idiom. Moreover, derived classes specify what the intended type for elapsed times is.</p>
<p>A process should expose publicly the following member functions whether needed (note that it isn't required to define a function unless the derived class wants to <em>override</em> the default behavior):</p>
<ul>
<li><p class="startli"><code>void update(Delta, void *);</code></p>
<p class="startli">This is invoked once per tick until a process is explicitly aborted or ends either with or without errors. Even though it's not mandatory to declare this member function, as a rule of thumb each process should at least define it to work <em>properly</em>. The <code>void *</code> parameter is an opaque pointer to user data (if any) forwarded directly to the process during an update.</p>
</li>
<li><p class="startli"><code>void init();</code></p>
<p class="startli">This is invoked when the process joins the running queue of a scheduler. It happens usually as soon as the process is attached to the scheduler if it's a top level one, otherwise when it replaces its parent if it's a <em>continuation</em>.</p>
</li>
<li><p class="startli"><code>void succeeded();</code></p>
<p class="startli">This is invoked in case of success, immediately after an update and during the same tick.</p>
</li>
<li><p class="startli"><code>void failed();</code></p>
<p class="startli">This is invoked in case of errors, immediately after an update and during the same tick.</p>
</li>
<li><p class="startli"><code>void aborted();</code></p>
<p class="startli">This is invoked only if a process is explicitly aborted. There is no guarantee that it executes in the same tick, it depends solely on whether the process is aborted immediately or not.</p>
</li>
</ul>
<p>Derived classes can also change the internal state of a process by invoking <code>succeed</code> and <code>fail</code>, as well as <code>pause</code> and <code>unpause</code> the process itself.<br />
All these are protected member functions made available to manage the life cycle of a process from a derived class.</p>
<p>Here is a minimal example for the sake of curiosity:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>my_process: <a class="code hl_class" href="classentt_1_1process.html">entt::process</a>&lt;my_process, std::uint32_t&gt; {</div>
<div class="line"> <span class="keyword">using </span>delta_type = std::uint32_t;</div>
<div class="line"> </div>
<div class="line"> my_process(delta_type delay)</div>
<div class="line"> : remaining{delay}</div>
<div class="line"> {}</div>
<div class="line"> </div>
<div class="line"> <span class="keywordtype">void</span> update(delta_type delta, <span class="keywordtype">void</span> *) {</div>
<div class="line"> remaining -= std::min(remaining, delta);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// ...</span></div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">if</span>(!remaining) {</div>
<div class="line"> <a class="code hl_function" href="classentt_1_1process.html#a4380dab12dc4c7559ada2182468aa1db">succeed</a>();</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"><span class="keyword">private</span>:</div>
<div class="line"> delta_type remaining;</div>
<div class="line">};</div>
<div class="ttc" id="aclassentt_1_1process_html"><div class="ttname"><a href="classentt_1_1process.html">entt::process</a></div><div class="ttdoc">Base class for processes.</div><div class="ttdef"><b>Definition:</b> <a href="process_8hpp_source.html#l00070">process.hpp:70</a></div></div>
<div class="ttc" id="aclassentt_1_1process_html_a4380dab12dc4c7559ada2182468aa1db"><div class="ttname"><a href="classentt_1_1process.html#a4380dab12dc4c7559ada2182468aa1db">entt::process::succeed</a></div><div class="ttdeci">void succeed() noexcept</div><div class="ttdoc">Terminates a process with success if it's still alive.</div><div class="ttdef"><b>Definition:</b> <a href="process_8hpp_source.html#l00121">process.hpp:121</a></div></div>
</div><!-- fragment --><h2><a class="anchor" id="autotoc_md181"></a>
Adaptor</h2>
<p>Lambdas and functors can't be used directly with a scheduler because they aren't properly defined processes with managed life cycles.<br />
This class helps in filling the gap and turning lambdas and functors into full-featured processes usable by a scheduler.</p>
<p>The function call operator has a signature similar to the one of the <code>update</code> function of a process but for the fact that it receives two extra callbacks to invoke whenever a process terminates with success or with an error:</p>
<div class="fragment"><div class="line">void(Delta delta, <span class="keywordtype">void</span> *data, <span class="keyword">auto</span> succeed, <span class="keyword">auto</span> fail);</div>
</div><!-- fragment --><p>Parameters have the following meaning:</p>
<ul>
<li><code>delta</code> is the elapsed time.</li>
<li><code>data</code> is an opaque pointer to user data if any, <code>nullptr</code> otherwise.</li>
<li><code>succeed</code> is a function to call when a process terminates with success.</li>
<li><code>fail</code> is a function to call when a process terminates with errors.</li>
</ul>
<p>Both <code>succeed</code> and <code>fail</code> accept no parameters at all.</p>
<p>Note that usually users shouldn't worry about creating adaptors at all. A scheduler creates them internally each and every time a lambda or a functor is used as a process.</p>
<h1><a class="anchor" id="autotoc_md182"></a>
The scheduler</h1>
<p>A cooperative scheduler runs different processes and helps managing their life cycles.</p>
<p>Each process is invoked once per tick. If it terminates, it's removed automatically from the scheduler and it's never invoked again. Otherwise, it's a good candidate to run one more time the next tick.<br />
A process can also have a <em>child</em>. In this case, the parent process is replaced with its child when it terminates and only if it returns with success. In case of errors, both the parent process and its child are discarded. This way, it's easy to create chain of processes to run sequentially.</p>
<p>Using a scheduler is straightforward. To create it, users must provide only the type for the elapsed times and no arguments at all:</p>
<div class="fragment"><div class="line"><a class="code hl_class" href="classentt_1_1basic__scheduler.html">entt::basic_scheduler&lt;std::uint64_t&gt;</a> scheduler;</div>
<div class="ttc" id="aclassentt_1_1basic__scheduler_html"><div class="ttname"><a href="classentt_1_1basic__scheduler.html">entt::basic_scheduler</a></div><div class="ttdoc">Cooperative scheduler for processes.</div><div class="ttdef"><b>Definition:</b> <a href="scheduler_8hpp_source.html#l00042">scheduler.hpp:42</a></div></div>
</div><!-- fragment --><p>Otherwise, the <code>scheduler</code> alias is also available for the most common cases. It uses <code>std::uint32_t</code> as a default type:</p>
<div class="fragment"><div class="line"><a class="code hl_class" href="classentt_1_1basic__scheduler.html">entt::scheduler</a> scheduler;</div>
</div><!-- fragment --><p>The class has member functions to query its internal data structures, like <code>empty</code> or <code>size</code>, as well as a <code>clear</code> utility to reset it to a clean state:</p>
<div class="fragment"><div class="line"><span class="comment">// checks if there are processes still running</span></div>
<div class="line"><span class="keyword">const</span> <span class="keyword">auto</span> empty = scheduler.empty();</div>
<div class="line"> </div>
<div class="line"><span class="comment">// gets the number of processes still running</span></div>
<div class="line"><a class="code hl_typedef" href="classentt_1_1basic__scheduler.html#a97932c77e05a736ed296c2b0d2d013f1">entt::scheduler::size_type</a> size = scheduler.size();</div>
<div class="line"> </div>
<div class="line"><span class="comment">// resets the scheduler to its initial state and discards all the processes</span></div>
<div class="line">scheduler.clear();</div>
<div class="ttc" id="aclassentt_1_1basic__scheduler_html_a97932c77e05a736ed296c2b0d2d013f1"><div class="ttname"><a href="classentt_1_1basic__scheduler.html#a97932c77e05a736ed296c2b0d2d013f1">entt::basic_scheduler::size_type</a></div><div class="ttdeci">std::size_t size_type</div><div class="ttdoc">Unsigned integer type.</div><div class="ttdef"><b>Definition:</b> <a href="scheduler_8hpp_source.html#l00111">scheduler.hpp:111</a></div></div>
</div><!-- fragment --><p>To attach a process to a scheduler there are mainly two ways:</p>
<ul>
<li><p class="startli">If the process inherits from the <code>process</code> class template, it's enough to indicate its type and submit all the parameters required to construct it to the <code>attach</code> member function:</p>
<div class="fragment"><div class="line">scheduler.attach&lt;my_process&gt;(1000u);</div>
</div><!-- fragment --></li>
<li><p class="startli">Otherwise, in case of a lambda or a functor, it's enough to provide an instance of the class to the <code>attach</code> member function:</p>
<div class="fragment"><div class="line">scheduler.attach([](<span class="keyword">auto</span>...){ <span class="comment">/* ... */</span> });</div>
</div><!-- fragment --></li>
</ul>
<p>In both cases, the return value is an opaque object that offers a <code>then</code> member function used to create chains of processes to run sequentially.<br />
As a minimal example of use:</p>
<div class="fragment"><div class="line"><span class="comment">// schedules a task in the form of a lambda function</span></div>
<div class="line">scheduler.attach([](<span class="keyword">auto</span> delta, <span class="keywordtype">void</span> *, <span class="keyword">auto</span> succeed, <span class="keyword">auto</span> fail) {</div>
<div class="line"> <span class="comment">// ...</span></div>
<div class="line">})</div>
<div class="line"><span class="comment">// appends a child in the form of another lambda function</span></div>
<div class="line">.then([](<span class="keyword">auto</span> delta, <span class="keywordtype">void</span> *, <span class="keyword">auto</span> succeed, <span class="keyword">auto</span> fail) {</div>
<div class="line"> <span class="comment">// ...</span></div>
<div class="line">})</div>
<div class="line"><span class="comment">// appends a child in the form of a process class</span></div>
<div class="line">.then&lt;my_process&gt;(1000u);</div>
</div><!-- fragment --><p>To update a scheduler and therefore all its processes, the <code>update</code> member function is the way to go:</p>
<div class="fragment"><div class="line"><span class="comment">// updates all the processes, no user data are provided</span></div>
<div class="line">scheduler.update(delta);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// updates all the processes and provides them with custom data</span></div>
<div class="line">scheduler.update(delta, &amp;data);</div>
</div><!-- fragment --><p>In addition to these functions, the scheduler offers an <code>abort</code> member function that is used to discard all the running processes at once:</p>
<div class="fragment"><div class="line"><span class="comment">// aborts all the processes abruptly ...</span></div>
<div class="line">scheduler.abort(<span class="keyword">true</span>);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// ... or gracefully during the next tick</span></div>
<div class="line">scheduler.abort();</div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- PageDoc -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.6
</small></address>
</body>
</html>