Angelscript/docs/manual/doc_adv_coroutine.html

196 lines
17 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">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.18"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>AngelScript: Co-routines</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="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.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>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.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 style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">AngelScript
</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.18 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('doc_adv_coroutine.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- 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">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="PageDoc"><div class="header">
<div class="headertitle">
<div class="title">Co-routines </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>Co-routines is a way to allow multiple execution paths in parallel, but without the hazards of pre-emptive scheduling in <a class="el" href="doc_adv_concurrent.html">multithreading</a> where one thread can be suspended at any moment so another can resume. Because co-routines always voluntarily suspend themselves in favor of the next co-routine, there is no need to worry about atomic instructions and critical sections.</p>
<p>Co-routines are not natively built-into the AngelScript library, but it can easily be implemented from the application side. In fact, the <a class="el" href="doc_addon_ctxmgr.html">Context manager</a> add-on already provides a default implementation for this.</p>
<p>To implement your own version of co-routines you will need a couple of pieces:</p>
<ul>
<li>The co-routine itself, which is just an instance of the <a class="el" href="classas_i_script_context.html">asIScriptContext</a> object. Each co-routine will have its own context object that holds the callstack of the co-routine.</li>
<li>A function that will permit the script to create, or spawn, new co-routines. This function will need to be able to refer to starting function of the new co-routine. This reference can be done by name, or perhaps more elegantly with a <a class="el" href="doc_datatypes_funcptr.html">function pointer</a>. Once invoked this function will instanciate the new context, and prepare it with the starting function.</li>
<li>A function that will permit a co-routine to yield control to the next co-routine. This function will simply suspend the current context so the next co-routine can be resumed.</li>
<li>A simple control algorithm for the co-routines. This can just be a loop that will iterate over an array of co-routines, i.e. contexts, until all of them have finished executing. When a new co-routine is created it is simply appended to the array to be picked up when the current co-routine yields the control.</li>
</ul>
<p>A simple implementation of the function that spawns a new co-routine may look like this:</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> CreateCoRoutine(<span class="keywordtype">string</span> &amp;func)</div>
<div class="line">{</div>
<div class="line"> <a class="code" href="classas_i_script_context.html">asIScriptContext</a> *ctx = <a class="code" href="group__api__principal__functions.html#gad3a20dc58093b92a5a44c7b6ada34a10">asGetActiveContext</a>();</div>
<div class="line"> <span class="keywordflow">if</span>( ctx )</div>
<div class="line"> {</div>
<div class="line"> <a class="code" href="classas_i_script_engine.html">asIScriptEngine</a> *engine = ctx-&gt;<a class="code" href="classas_i_script_context.html#a07f12016c5435aec5b63449abb6e4d8d">GetEngine</a>();</div>
<div class="line"> <span class="keywordtype">string</span> mod = ctx-&gt;<a class="code" href="classas_i_script_context.html#a1c101300447f2909e5d188409a7180f6">GetFunction</a>()-&gt;<a class="code" href="classas_i_script_function.html#af03c30e4764f81c01400d7f77a8d0832">GetModuleName</a>();</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// We need to find the function that will be created as the co-routine</span></div>
<div class="line"> <span class="keywordtype">string</span> decl = <span class="stringliteral">&quot;void &quot;</span> + func + <span class="stringliteral">&quot;()&quot;</span>; </div>
<div class="line"> <a class="code" href="classas_i_script_function.html">asIScriptFunction</a> *funcPtr = engine-&gt;<a class="code" href="classas_i_script_engine.html#a9f7cdc52b59034e6e55eb8a56b427aa4">GetModule</a>(mod.c_str())-&gt;<a class="code" href="classas_i_script_module.html#ab4754d55d8667aefbed135b4794d461b">GetFunctionByDecl</a>(decl.c_str());</div>
<div class="line"> <span class="keywordflow">if</span>( funcPtr == 0 )</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// No function could be found, raise an exception</span></div>
<div class="line"> ctx-&gt;<a class="code" href="classas_i_script_context.html#a6f0f071215e0f7effc0b765043f01275">SetException</a>((<span class="stringliteral">&quot;Function &#39;&quot;</span> + decl + <span class="stringliteral">&quot;&#39; doesn&#39;t exist&quot;</span>).c_str());</div>
<div class="line"> <span class="keywordflow">return</span>;</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Create a new context for the co-routine</span></div>
<div class="line"> <a class="code" href="classas_i_script_context.html">asIScriptContext</a> *coctx = engine-&gt;<a class="code" href="classas_i_script_engine.html#a2630e1cd03ffab0fee9b820bf0afe42a">CreateContext</a>();</div>
<div class="line"> coctx-&gt;<a class="code" href="classas_i_script_context.html#a43976f42dfc6c1af23e132d36265173a">Prepare</a>(funcPtr);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Add the new co-routine context to the array of co-routines</span></div>
<div class="line"> coroutines.push_back(coctx);</div>
<div class="line"> }</div>
<div class="line">}</div>
</div><!-- fragment --><p>The yield function is even simpler:</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> Yield()</div>
<div class="line">{</div>
<div class="line"> <a class="code" href="classas_i_script_context.html">asIScriptContext</a> *ctx = <a class="code" href="group__api__principal__functions.html#gad3a20dc58093b92a5a44c7b6ada34a10">asGetActiveContext</a>();</div>
<div class="line"> <span class="keywordflow">if</span>( ctx )</div>
<div class="line"> { </div>
<div class="line"> <span class="comment">// Suspend the context so the next co-routine can be resumed</span></div>
<div class="line"> ctx-&gt;<a class="code" href="classas_i_script_context.html#ad4ac8be3586c46069b5870e40c86544a">Suspend</a>();</div>
<div class="line"> }</div>
<div class="line">}</div>
</div><!-- fragment --><p>A basic control algorithm might look like this:</p>
<div class="fragment"><div class="line">std::vector&lt;asIScriptContext *&gt; coroutines;</div>
<div class="line"><span class="keywordtype">void</span> Execute()</div>
<div class="line">{</div>
<div class="line"> <span class="keywordtype">int</span> n = 0;</div>
<div class="line"> <span class="keywordflow">while</span>( coroutines.size() &gt; 0 )</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// Resume the co-routine</span></div>
<div class="line"> <span class="keywordtype">int</span> r = coroutines[n]-&gt;Execute();</div>
<div class="line"> <span class="keywordflow">if</span>( r == <a class="code" href="angelscript_8h.html#a867f14b4137dd4602fda1e616b217a69a7b5644be315c46f2fa44f032731242c7">asEXECUTION_SUSPENDED</a> )</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// Resume the next co-routine</span></div>
<div class="line"> <span class="keywordflow">if</span>( ++n == coroutines.size() )</div>
<div class="line"> n = 0;</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">else</span></div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// The co-routine finished so let&#39;s remove it</span></div>
<div class="line"> coroutines[n]-&gt;Release();</div>
<div class="line"> coroutines.erase(n);</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line">}</div>
</div><!-- fragment --><dl class="section see"><dt>See also</dt><dd><a class="el" href="doc_addon_ctxmgr.html">Context manager</a>, <a class="el" href="doc_samples_corout.html">Co-routines</a>, <a class="el" href="doc_adv_concurrent.html">Concurrent scripts</a> </dd></dl>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<div class="ttc" id="aclassas_i_script_context_html"><div class="ttname"><a href="classas_i_script_context.html">asIScriptContext</a></div><div class="ttdoc">The interface to the virtual machine.</div><div class="ttdef"><b>Definition:</b> angelscript.h:2717</div></div>
<div class="ttc" id="aclassas_i_script_context_html_a1c101300447f2909e5d188409a7180f6"><div class="ttname"><a href="classas_i_script_context.html#a1c101300447f2909e5d188409a7180f6">asIScriptContext::GetFunction</a></div><div class="ttdeci">virtual asIScriptFunction * GetFunction(asUINT stackLevel=0)=0</div><div class="ttdoc">Returns the function at the specified callstack level.</div></div>
<div class="ttc" id="aangelscript_8h_html_a867f14b4137dd4602fda1e616b217a69a7b5644be315c46f2fa44f032731242c7"><div class="ttname"><a href="angelscript_8h.html#a867f14b4137dd4602fda1e616b217a69a7b5644be315c46f2fa44f032731242c7">asEXECUTION_SUSPENDED</a></div><div class="ttdeci">@ asEXECUTION_SUSPENDED</div><div class="ttdoc">The execution is suspended and can be resumed.</div><div class="ttdef"><b>Definition:</b> angelscript.h:400</div></div>
<div class="ttc" id="aclassas_i_script_module_html_ab4754d55d8667aefbed135b4794d461b"><div class="ttname"><a href="classas_i_script_module.html#ab4754d55d8667aefbed135b4794d461b">asIScriptModule::GetFunctionByDecl</a></div><div class="ttdeci">virtual asIScriptFunction * GetFunctionByDecl(const char *decl) const =0</div><div class="ttdoc">Returns the function by its declaration.</div></div>
<div class="ttc" id="aclassas_i_script_engine_html_a9f7cdc52b59034e6e55eb8a56b427aa4"><div class="ttname"><a href="classas_i_script_engine.html#a9f7cdc52b59034e6e55eb8a56b427aa4">asIScriptEngine::GetModule</a></div><div class="ttdeci">virtual asIScriptModule * GetModule(const char *module, asEGMFlags flag=asGM_ONLY_IF_EXISTS)=0</div><div class="ttdoc">Return an interface pointer to the module.</div></div>
<div class="ttc" id="aclassas_i_script_engine_html"><div class="ttname"><a href="classas_i_script_engine.html">asIScriptEngine</a></div><div class="ttdoc">The engine interface.</div><div class="ttdef"><b>Definition:</b> angelscript.h:1092</div></div>
<div class="ttc" id="aclassas_i_script_function_html_af03c30e4764f81c01400d7f77a8d0832"><div class="ttname"><a href="classas_i_script_function.html#af03c30e4764f81c01400d7f77a8d0832">asIScriptFunction::GetModuleName</a></div><div class="ttdeci">virtual const char * GetModuleName() const =0</div><div class="ttdoc">Returns the name of the module where the function was implemented.</div></div>
<div class="ttc" id="aclassas_i_script_context_html_a07f12016c5435aec5b63449abb6e4d8d"><div class="ttname"><a href="classas_i_script_context.html#a07f12016c5435aec5b63449abb6e4d8d">asIScriptContext::GetEngine</a></div><div class="ttdeci">virtual asIScriptEngine * GetEngine() const =0</div><div class="ttdoc">Returns a pointer to the engine.</div></div>
<div class="ttc" id="aclassas_i_script_context_html_ad4ac8be3586c46069b5870e40c86544a"><div class="ttname"><a href="classas_i_script_context.html#ad4ac8be3586c46069b5870e40c86544a">asIScriptContext::Suspend</a></div><div class="ttdeci">virtual int Suspend()=0</div><div class="ttdoc">Suspends the execution, which can then be resumed by calling Execute again.</div></div>
<div class="ttc" id="aclassas_i_script_engine_html_a2630e1cd03ffab0fee9b820bf0afe42a"><div class="ttname"><a href="classas_i_script_engine.html#a2630e1cd03ffab0fee9b820bf0afe42a">asIScriptEngine::CreateContext</a></div><div class="ttdeci">virtual asIScriptContext * CreateContext()=0</div><div class="ttdoc">Creates a new script context.</div></div>
<div class="ttc" id="aclassas_i_script_function_html"><div class="ttname"><a href="classas_i_script_function.html">asIScriptFunction</a></div><div class="ttdoc">The interface for a script function description.</div><div class="ttdef"><b>Definition:</b> angelscript.h:3823</div></div>
<div class="ttc" id="aclassas_i_script_context_html_a6f0f071215e0f7effc0b765043f01275"><div class="ttname"><a href="classas_i_script_context.html#a6f0f071215e0f7effc0b765043f01275">asIScriptContext::SetException</a></div><div class="ttdeci">virtual int SetException(const char *info, bool allowCatch=true)=0</div><div class="ttdoc">Sets an exception, which aborts the execution.</div></div>
<div class="ttc" id="aclassas_i_script_context_html_a43976f42dfc6c1af23e132d36265173a"><div class="ttname"><a href="classas_i_script_context.html#a43976f42dfc6c1af23e132d36265173a">asIScriptContext::Prepare</a></div><div class="ttdeci">virtual int Prepare(asIScriptFunction *func)=0</div><div class="ttdoc">Prepares the context for execution of the function.</div></div>
<div class="ttc" id="agroup__api__principal__functions_html_gad3a20dc58093b92a5a44c7b6ada34a10"><div class="ttname"><a href="group__api__principal__functions.html#gad3a20dc58093b92a5a44c7b6ada34a10">asGetActiveContext</a></div><div class="ttdeci">AS_API asIScriptContext * asGetActiveContext()</div><div class="ttdoc">Returns the currently active context.</div></div>
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated on Sat Dec 5 2020 23:20:24 for AngelScript by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
</ul>
</div>
</body>
</html>