📄 c-basic3.html
字号:
<th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="92592"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Description</font></b></div></th></tr><tr><td colspan="20"><hr class="tablerule2"></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="92595"> </a><b class="routine"><i class="routine">exit</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="92597"> </a>Terminate the calling task and free memory (task stacks and task control blocks only).<sup><b class="FootnoteMarker">1</b></sup> </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="92603"> </a><b class="routine"><i class="routine">taskDelete</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="92605"> </a>Terminate a specified task and free memory (task stacks and task control blocks only).<sup>*</sup> </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="92608"> </a><b class="routine"><i class="routine">taskSafe</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="92610"> </a>Protect the calling task from deletion. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="92613"> </a><b class="routine"><i class="routine">taskUnsafe</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="92615"> </a>Undo a <b class="routine"><i class="routine">taskSafe</i></b><b>( )</b> (make the calling task available for deletion). </div></td></tr><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><td colspan="20"><p class="FootnoteNumberMarker">1: <span class="TableFootnote"><a name="92600"> </a>Memory that is allocated by the task during its execution is <i class="emphasis">not</i> freed when the task is terminated.</span></p></td></tr></table></p></p><dd><p class="Body"><a name="84238"> </a>When a task is deleted, no other task is notified of this deletion. The routines <b class="routine"><i class="routine">taskSafe</i></b><b>( )</b> and <b class="routine"><i class="routine">taskUnsafe</i></b><b>( )</b> address problems that stem from unexpected deletion of tasks. The routine <b class="routine"><i class="routine">taskSafe</i></b><b>( )</b> protects a task from deletion by other tasks. This protection is often needed when a task executes in a critical region or engages a critical resource.</p><dd><p class="Body"><a name="84240"> </a>For example, a task might take a semaphore for exclusive access to some data structure. While executing inside the critical region, the task might be deleted by another task. Because the task is unable to complete the critical region, the data structure might be left in a corrupt or inconsistent state. Furthermore, because the semaphore can never be released by the task, the critical resource is now unavailable for use by any other task and is essentially frozen.</p><dd><p class="Body"><a name="84241"> </a>Using <b class="routine"><i class="routine">taskSafe</i></b><b>( )</b> to protect the task that took the semaphore prevents such an outcome. Any task that tries to delete a task protected with <b class="routine"><i class="routine">taskSafe</i></b><b>( )</b> is blocked. When finished with its critical resource, the protected task can make itself available for deletion by calling <b class="routine"><i class="routine">taskUnsafe</i></b><b>( )</b>, which readies any deleting task. To support nested deletion-safe regions, a count is kept of the number of times <b class="routine"><i class="routine">taskSafe</i></b><b>( )</b> and <b class="routine"><i class="routine">taskUnsafe</i></b><b>( )</b> are called. Deletion is allowed only when the count is zero, that is, there are as many "unsafes" as "safes." Protection operates only on the calling task. A task cannot make another task safe or unsafe from deletion.</p><dd><p class="Body"><a name="84244"> </a>The following code fragment shows how to use <b class="routine"><i class="routine">taskSafe</i></b><b>( )</b> and <b class="routine"><i class="routine">taskUnsafe</i></b><b>( )</b> to protect a critical region of code:</p></dl><dl class="margin"><dd><pre class="Code"><b><a name="84245"> taskSafe (); semTake (semId, WAIT_FOREVER); /* Block until semaphore available */ . . <i class="i">critical region </i> . semGive (semId); /* Release semaphore */ taskUnsafe ();</a></b></pre></dl><dl class="margin"><dd><p class="Body"><a name="84252"> </a>Deletion safety is often coupled closely with mutual exclusion, as in this example. For convenience and efficiency, a special kind of semaphore, the <i class="term">mutual-exclusion semaphore</i>, offers an option for deletion safety. For more information, see <a href="c-basic4.html#85252"><i class="title">Mutual-Exclusion Semaphores</i></a>.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="84257">Task Control</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84307"> </a>The routines listed in <a href="c-basic3.html#84265">Table 2-9</a> provide direct control over a task's execution.<p class="table"><h4 class="EntityTitle"><a name="84265"><font face="Helvetica, sans-serif" size="-1" class="sans">Table 2-9: Task Control Routines</font></a></h4><table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84269"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Call</font></b></div></th><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84271"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Description</font></b></div></th></tr><tr><td colspan="20"><hr class="tablerule2"></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84278"> </a><b class="routine"><i class="routine">taskSuspend</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84281"> </a>Suspend a task. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84284"> </a><b class="routine"><i class="routine">taskResume</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84286"> </a>Resume a task. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84289"> </a><b class="routine"><i class="routine">taskRestart</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84291"> </a>Restart a task. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84294"> </a><b class="routine"><i class="routine">taskDelay</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84297"> </a>Delay a task; delay units are ticks. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84300"> </a><b class="routine"><i class="routine">nanosleep</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84302"> </a>Delay a task; delay units are nanoseconds. </div></td></tr><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p></p><dd><p class="Body"><a name="84308"> </a>VxWorks debugging facilities require routines for suspending and resuming a task. They are used to freeze a task's state for examination.</p><dd><p class="Body"><a name="84309"> </a>Tasks may require restarting during execution in response to some catastrophic error. The restart mechanism, <b class="routine"><i class="routine">taskRestart</i></b><b>( )</b>, recreates a task with the original creation arguments. The Tornado shell also uses this mechanism to restart itself in response to a task-abort request; for information, see the <i class="title">Tornado User's Guide: Shell</i>.</p><dd><p class="Body"><a name="84310"> </a>Delay operations provide a simple mechanism for a task to sleep for a fixed duration. Task delays are often used for polling applications. For example, to delay a task for half a second without making assumptions about the clock rate, call:</p><dl class="margin"><dd><pre class="Code2"><b><a name="84311">taskDelay (sysClkRateGet ( ) / 2);</a></b></pre></dl><dd><p class="Body"><a name="88348"> </a>The<b class="routine"><i class="routine"> </i></b>routine <b class="routine"><i class="routine">sysClkRateGet</i></b><b>( )</b> returns the speed of the system clock in ticks per second. Instead of <b class="routine"><i class="routine">taskDelay</i></b><b>( )</b>, you can use the POSIX routine <b class="routine"><i class="routine">nanosleep</i></b><b>( )</b> to specify a delay directly in time units. Only the units are different; the resolution of both delay routines is the same, and depends on the system clock. For details, see <a href="c-basic7.html#86917"><i class="title">2.7 POSIX Clocks and Timers</i></a>.</p><dd><p class="Body"><a name="88352"> </a>As a side effect, <b class="routine"><i class="routine">taskDelay</i></b><b>( )</b> moves the calling task to the end of the ready queue for tasks of the same priority. In particular, you can yield the CPU to any other tasks of the same priority by "delaying" for zero clock ticks:</p><dl class="margin"><dd><pre class="Code2"><b><a name="84318">taskDelay (NO_WAIT); /* allow other tasks of same priority to run */</a></b></pre></dl><dd><p class="Body"><a name="84319"> </a>A "delay" of zero duration is only possible with <b class="routine"><i class="routine">taskDelay</i></b><b>( )</b>; <b class="routine"><i class="routine">nanosleep</i></b><b>( )</b> considers it an error.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="84321">2.3.5 Tasking Extensions</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84322"> </a>To allow additional task-related facilities to be added to the system without modifying the kernel, <i class="i">wind</i> provides <i class="term">task create</i>, <i class="term">switch</i>, and <i class="term">delete</i> <i class="term">hooks</i>, which allow additional routines to be invoked whenever a task is created, a task context switch occurs, or a task is deleted. There are spare fields in the task control block (TCB) available for application extension of a task's context. These hook routines are listed in <a href="c-basic3.html#84331">Table 2-10</a>; for more information, see the reference entry for <b class="library">taskHookLib</b>.<p class="table"><h4 class="EntityTitle"><a name="84331"><font face="Helvetica, sans-serif" size="-1" class="sans">Table 2-10: Task Create, Switch, and Delete Hooks</font></a></h4><table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84335"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Call</font></b></div></th><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="84337"> </a><font face="Helvetica, sans-serif" size="-1" class="sans"> Description</font></b></div></th></tr><tr><td colspan="20"><hr class="tablerule2"></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84344"> </a><b class="routine"><i class="routine">taskCreateHookAdd</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84346"> </a>Add a routine to be called at every task create. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84349"> </a><b class="routine"><i class="routine">taskCreateHookDelete</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84351"> </a>Delete a previously added task create routine. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84354"> </a><b class="routine"><i class="routine">taskSwitchHookAdd</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84356"> </a>Add a routine to be called at every task switch. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84359"> </a><b class="routine"><i class="routine">taskSwitchHookDelete</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84361"> </a>Delete a previously added task switch routine. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84364"> </a><b class="routine"><i class="routine">taskDeleteHookAdd</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84366"> </a>Add a routine to be called at every task delete. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="84369"> </a><b class="routine"><i class="routine">taskDeleteHookDelete</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="84371"> </a>Delete a previously added task delete routine. </div></td></tr><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p> </p><dd><p class="Body"><a name="84376"> </a>User-installed switch hooks are called within the kernel context. Thus, switch hooks do not have access to all VxWorks facilities. <a href="c-basic3.html#90532">Table 2-11</a> summarizes the routines that can be called from a task switch hook; in general, any routine that does not involve the kernel can be called.<p class="table"><h4 class="EntityTitle"><a name="90532"><font face="Helvetica, sans-serif" size="-1" class="sans">Table 2-11: Routines that Can Be Called by Task Switch Hooks</font></a></h4><table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="90536"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Library</font></b></div></th><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="90538"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Routines</font></b></div></th>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -