vxwsem.html
来自「vxworks相关论文」· HTML 代码 · 共 749 行 · 第 1/3 页
HTML
749 行
a semaphore is given by a task that did not take it, mutualexclusion could be lost.<p></blockquote><h4>SYNCHRONIZATION</h4><blockquote><p>To use a binary semaphore as a means of synchronization, create itwith an initial state of empty. A task blocks by taking asemaphore at a synchronization point, and it remains blocked untilthe semaphore is given by another task or interrupt serviceroutine.<p>Synchronization with interrupt service routines is a particularlycommon need. Binary semaphores can be given, but not taken, frominterrupt level. Thus, a task can block at a synchronization pointwith <b><i><a href="./VXWSem.html#VXWSem::take">VXWSem::take</a></i>( )</b>, and an interrupt service routine can unblockthat task with <b><i><a href="./VXWSem.html#VXWSem::give">VXWSem::give</a></i>( )</b>.<p>A <b><i><a href="./semLib.html#semFlush">semFlush</a></i>( )</b> on a binary semaphore atomically unblocks all pendedtasks in the semaphore queue; that is, all tasks are unblocked at once,before any actually execute.<p></blockquote><h4>CAVEATS</h4><blockquote><p>There is no mechanism to give back or reclaim semaphores automatically whentasks are suspended or deleted. Such a mechanism, though desirable, is notcurrently feasible. Without explicit knowledge of the state of the guardedresource or region, reckless automatic reclamation of a semaphore couldleave the resource in a partial state. Thus, if a task ceases executionunexpectedly, as with a bus error, currently owned semaphores will not begiven back, effectively leaving a resource permanently unavailable. Themutual-exclusion semaphores provided by <b>VXWMSem</b> offer protection fromunexpected task deletion.<p></blockquote><h4>RETURNS</h4><blockquote><p>N/A</blockquote><h4>SEE ALSO</h4><blockquote><p><b><a href="./VXWSem.html#top">VXWSem</a></b><hr><a name="VXWMSem::VXWMSem"></a><p align=right><a href="rtnIndex.html"><i>Wind Foundation Classes : Methods</i></a></p></blockquote><h1><i>VXWMSem::VXWMSem</i>( )</h1> <blockquote></a></blockquote><h4>NAME</h4><blockquote> <p><strong><i>VXWMSem::VXWMSem</i>( )</strong> - create and initialize a mutual-exclusion semaphore</p></blockquote><h4>SYNOPSIS</h4><blockquote><p><pre>VXWMSem ( int opts )</pre></blockquote><h4>DESCRIPTION</h4><blockquote><p>This routine allocates and initializes a mutual-exclusion semaphore. Thesemaphore state is initialized to full.<p>Semaphore options include the following:<dl><dt><b>SEM_Q_PRIORITY</b><dd>Queue pended tasks on the basis of their priority.<p><dt><b>SEM_Q_FIFO</b><dd>Queue pended tasks on a first-in-first-out basis.<p><dt><b>SEM_DELETE_SAFE</b><dd>Protect a task that owns the semaphore from unexpected deletion.This option enables an implicit <b><i><a href="./taskLib.html#taskSafe">taskSafe</a></i>( )</b> for each <b><i><a href="./VXWSem.html#VXWSem::take">VXWSem::take</a></i>( )</b>,and an implicit <b><i><a href="./taskLib.html#taskUnsafe">taskUnsafe</a></i>( )</b> for each <b><i><a href="./VXWSem.html#VXWSem::give">VXWSem::give</a></i>( )</b>.<p><dt><b>SEM_INVERSION_SAFE</b><dd> Protect the system from priority inversion. With this option, thetask owning the semaphore executes at the highest priority of thetasks pended on the semaphore, if that is higher than its currentpriority. This option must be accompanied by the <b>SEM_Q_PRIORITY</b>queuing mode. </dl><p>Mutual-exclusion semaphores offer convenient options suited forsituations that require mutually exclusive access to resources.Typical applications include sharing devices and protecting datastructures. Mutual-exclusion semaphores are used by manyhigher-level VxWorks facilities.<p>The mutual-exclusion semaphore is a specialized version of thebinary semaphore, designed to address issues inherent in mutualexclusion, such as recursive access to resources, priorityinversion, and deletion safety. The fundamental behavior of themutual-exclusion semaphore is identical to the binary semaphore asdescribed for <b><i><a href="./VXWSem.html#VXWBSem::VXWBSem">VXWBSem::VXWBSem</a></i>( )</b>, except for the followingrestrictions:<p><ul><li> It can only be used for mutual exclusion.</li><li> It can only be given by the task that took it.</li><li> It may not be taken or given from interrupt level.</li><li> The <b><i><a href="./VXWSem.html#VXWSem::flush">VXWSem::flush</a></i>( )</b> operation is illegal. </ul><p><p>These last two operations have no meaning in mutual-exclusion situations.<p></blockquote><h4>RECURSIVE RESOURCE ACCESS</h4><blockquote><p><p>A special feature of the mutual-exclusion semaphore is that it maybe taken "recursively;" that is, it can be taken more than once bythe task that owns it before finally being released. Recursion isuseful for a set of routines that need mutually exclusive access toa resource, but may need to call each other.<p>Recursion is possible because the system keeps track of which taskcurrently owns a mutual-exclusion semaphore. Before beingreleased, a mutual-exclusion semaphore taken recursively must begiven the same number of times it has been taken; this is trackedby means of a count which increments with each <b><i><a href="./VXWSem.html#VXWSem::take">VXWSem::take</a></i>( )</b>and decrements with each <b><i><a href="./VXWSem.html#VXWSem::give">VXWSem::give</a></i>( )</b>.<p></blockquote><h4>PRIORITY-INVERSION SAFETY</h4><blockquote><p>If the option <b>SEM_INVERSION_SAFE</b> is selected, the library adopts apriority-inheritance protocol to resolve potential occurrences of"priority inversion," a problem stemming from the use semaphores formutual exclusion. Priority inversion arises when a higher-priority taskis forced to wait an indefinite period of time for the completion of alower-priority task.<p>Consider the following scenario: T1, T2, and T3 are tasks of high,medium, and low priority, respectively. T3 has acquired someresource by taking its associated semaphore. When T1 preempts T3and contends for the resource by taking the same semaphore, itbecomes blocked. If we could be assured that T1 would be blockedno longer than the time it normally takes T3 to finish with theresource, the situation would not be problematic. However, thelow-priority task is vulnerable to preemption by medium-prioritytasks; a preempting task, T2, could inhibit T3 from relinquishingthe resource. This condition could persist, blocking T1 for anindefinite period of time.<p>The priority-inheritance protocol solves the problem of priorityinversion by elevating the priority of T3 to the priority of T1during the time T1 is blocked on T3. This protects T3, andindirectly T1, from preemption by T2. Stated more generally, thepriority-inheritance protocol assures that a task which owns aresource executes at the priority of the highest priority taskblocked on that resource. When execution is complete, the taskgives up the resource and returns to its normal, or standard,priority. Hence, the "inheriting" task is protected frompreemption by any intermediate-priority tasks.<p>The priority-inheritance protocol also takes into consideration a task'sownership of more than one mutual-exclusion semaphore at a time. Such atask will execute at the priority of the highest priority task blocked onany of the resources it owns. The task returns to its normal priorityonly after relinquishing all of its mutual-exclusion semaphores that havethe inversion-safety option enabled.<p></blockquote><h4>SEMAPHORE DELETION</h4><blockquote><p>The <b><i><a href="./VXWSem.html#VXWSem::~VXWSem">VXWSem::~VXWSem</a></i>( )</b> destructor terminates a semaphore and deallocates anyassociated memory. The deletion of a semaphore unblocks tasks pendedon that semaphore; the routines which were pended return ERROR. Takespecial care when deleting mutual-exclusion semaphores to avoiddeleting a semaphore out from under a task that already owns (hastaken) that semaphore. Applications should adopt the protocol of onlydeleting semaphores that the deleting task owns.<p></blockquote><h4>TASK-DELETION SAFETY</h4><blockquote><p>If the option <b>SEM_DELETE_SAFE</b> is selected, the task owning the semaphoreis protected from deletion as long as it owns the semaphore. Thissolves another problem endemic to mutual exclusion. Deleting a taskexecuting in a critical region can be catastrophic. The resource could beleft in a corrupted state and the semaphore guarding the resource would beunavailable, effectively shutting off all access to the resource.<p>As discussed in <b><a href="./taskLib.html#top">taskLib</a></b>, the primitives <b><i><a href="./taskLib.html#taskSafe">taskSafe</a></i>( )</b> and <b><i><a href="./taskLib.html#taskUnsafe">taskUnsafe</a></i>( )</b>offer one solution, but as this type of protection goes hand inhand with mutual exclusion, the mutual-exclusion semaphore providesthe option <b>SEM_DELETE_SAFE</b>, which enables an implicit <b><i><a href="./taskLib.html#taskSafe">taskSafe</a></i>( )</b>with each <b><i><a href="./VXWSem.html#VXWSem::take">VXWSem::take</a></i>( )</b>, and a <b><i><a href="./taskLib.html#taskUnsafe">taskUnsafe</a></i>( )</b> with each<b><i><a href="./VXWSem.html#VXWSem::give">VXWSem::give</a></i>( )</b>. This convenience is also more efficient, as theresulting code requires fewer entrances to the kernel.<p></blockquote><h4>CAVEATS</h4><blockquote><p>There is no mechanism to give back or reclaim semaphores automatically whentasks are suspended or deleted. Such a mechanism, though desirable, is notcurrently feasible. Without explicit knowledge of the state of the guardedresource or region, reckless automatic reclamation of a semaphore couldleave the resource in a partial state. Thus if a task ceases executionunexpectedly, as with a bus error, currently owned semaphores will not begiven back, effectively leaving a resource permanently unavailable. The<b>SEM_DELETE_SAFE</b> option partially protects an application, to the extentthat unexpected deletions will be deferred until the resource is released.<p></blockquote><h4>RETURNS</h4><blockquote><p>N/A<p></blockquote><h4>SEE ALSO</h4><blockquote><p><b><a href="./VXWSem.html#top">VXWSem</a></b>, <b><i><a href="./taskLib.html#taskSafe">taskSafe</a></i>( )</b>, <b><i><a href="./taskLib.html#taskUnsafe">taskUnsafe</a></i>( )</b><hr><a name="VXWMSem::giveForce"></a><p align=right><a href="rtnIndex.html"><i>Wind Foundation Classes : Methods</i></a></p></blockquote><h1><i>VXWMSem::giveForce</i>( )</h1> <blockquote></a></blockquote><h4>NAME</h4><blockquote> <p><strong><i>VXWMSem::giveForce</i>( )</strong> - give a mutual-exclusion semaphore without restrictions</p></blockquote><h4>SYNOPSIS</h4><blockquote><p><pre> STATUS giveForce ()</pre></blockquote><h4>DESCRIPTION</h4><blockquote><p>This routine gives a mutual-exclusion semaphore, regardless of semaphoreownership. It is intended as a debugging aid only.<p>The routine is particularly useful when a task dies while holding somemutual-exclusion semaphore, because the semaphore can be resurrected. Theroutine gives the semaphore to the next task in the pend queue, or makesthe semaphore full if no tasks are pending. In effect, execution continues as if the task owning the semaphore had actually given thesemaphore.<p></blockquote><h4>CAVEATS</h4><blockquote><p>Use this routine should only as a debugging aid, when the condition ofthe semaphore is known.<p></blockquote><h4>RETURNS</h4><blockquote><p>OK.<p></blockquote><h4>SEE ALSO</h4><blockquote><p><b><i><a href="./VXWSem.html#VXWSem::give">VXWSem::give</a></i>( )</b></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?