⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mutex2.htm

📁 ST20 Embedded Toolset R2.0.5用于开发基于ST20芯片机顶盒软件的开发平台,2.0.5版本,国内找不到的.在国外论坛上花了N天才找到!
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Quadralay WebWorks Publisher Professional Edition 6.0.5">
<meta name="TEMPLATEBASE" content="book_html">
<meta name="LASTUPDATED" content="12/01/03 14:15:00">
<title>7.1 Mutexes overview</title>

<STYLE TYPE="text/css">
<!--
	span.Signal { text-transform: uppercase; font-family: Verdana }
-->
</STYLE>

</head>

<body link="#3366CC" vlink="#9999CC" text="#000000" alink="#0000CC" bgcolor="#FFFFFF"
background="images/backgrnd.gif">

<p><img src="images/stlogo.gif" width="106" height="83" align="left"
alt="logo here!"> </p>

<table width="331" border="0" align="right" cellpadding="0" cellspacing="0">
  <tr>
    <td><a href="os20toc.htm"><img src="images/navtoc.gif" width="84" height="23"
    border="0" alt="TOC"> </a></td>
    <td><a href="mutex.htm"><img src="images/navprev.gif" width="80" height="23"
    border="0" alt="PREV"> </a></td>
    <td><a href="mutex3.htm"><img src="images/navnext.gif" width="83" height="23"
    border="0" alt="NEXT"> </a></td>
    <td><a href="os20ix.htm"><img src="images/navidx.gif" width="84" height="23"
    border="0" alt="INDEX"> </a></td>
  </tr>
</table>

<p><br clear="all">
</p>

<hr align="left">

<blockquote>
<h2>
  <a name="1106313"> </a><font color="#003366"  face="Verdana, Arial, Helvetica, sans-serif">7.1 	 Mutexes overview</font>
</h2><hr>


<p>
  <a name="1106314"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">A mutex structure <font size=2 face=Courier><strong>mutex_t</strong></font> contains several pieces of data including:</font>
</p>

<ul>
<p>  <font size=2  face="Verdana, Arial, Helvetica, sans-serif"><li ><a name="1106315"> </a>the current owning task,</font></p>
<p>  <font size=2  face="Verdana, Arial, Helvetica, sans-serif"><li ><a name="1106316"> </a>a queue of tasks waiting to take the mutex.</font></p>
</ul>

<p>
  <a name="1111170"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">A mutex can be owned by only one task at time. In this sense they are like OS20 semaphores initialized with a count of 1 (also known as <strong>binary semaphores</strong>). Unlike semaphores, once a task owns a mutex, it can re-take it as many times as necessary, provided that it also releases it an equal number of times. In this situation binary semaphores would deadlock.</font>
</p>


<p>
  <a name="1106317"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">Mutexes are created using one of the following functions:</font>
</p>


<a name="1106318"> </a><font size=2 face=Courier><strong>mutex_t* mutex_create_fifo(void);<br>void mutex_init_fifo(mutex_t *mutex);<br>mutex_t* mutex_create_priority(void);<br>void mutex_init_priority(mutex_t *mutex)<br></strong></font>



<p>
  <a name="1111178"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">The <font size=2 face=Courier><strong>create_</strong></font> version of these functions allocates memory for the mutex automatically, while the <font size=2 face=Courier><strong>init_</strong></font> versions enable the user to specify a pointer to the mutex, using the data structure <font size=2 face=Courier><strong>mutex_t</strong></font>.</font>
</p>


<p>
  <a name="1111184"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">The mutexes which OS20 provide differ in the way in which tasks are queued when waiting for it. For FIFO mutexes tasks are queued in the order in which they call <font size=2 face=Courier><strong>mutex_lock()</strong></font>. Mutexes of this type are created using <font size=2 face=Courier><strong>mutex_create_fifo()</strong></font> or <font size=2 face=Courier><strong>mutex_init_fifo()</strong></font>.</font>
</p>


<p>
  <a name="1106321"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">However, sometimes it is useful to allow higher priority tasks to jump the queue, so that they are blocked for a minimum amount of time. In this case, a second type of mutex can be used, a priority based mutex. For this type of mutex, tasks are queued based on their priority first, and the order which they call <font size=2 face=Courier><strong>mutex_lock()</strong></font> second. Mutexes of this type are created using <font size=2 face=Courier><strong>mutex_create_priority()</strong></font> or <font size=2 face=Courier><strong>mutex_init_priority()</strong></font>. </font>
</p>


<p>
  <a name="1106322"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">Mutex may be acquired by the functions:</font>
</p>


<a name="1106323"> </a><font size=2 face=Courier><strong>void mutex_lock(mutex_t* mutex);<br></strong></font>



<p>
  <a name="1106324"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">and</font>
</p>


<a name="1106325"> </a><font size=2 face=Courier><strong>int mutex_trylock(mutex_t* mutex);<br></strong></font>



<p>
  <a name="1106326"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">When a task wants to acquire a mutex, it calls <font size=2 face=Courier><strong>mutex_lock()</strong></font>. If the mutex is currently unowned, or already owned by the same task, then the task gets the mutex and continues. If however, the mutex is owned by another task, then the task adds itself to the queue of tasks waiting for the mutex and deschedules itself. Eventually another task should release the mutex, and the first waiting task gets the mutex and is able to continue. In this way, when the task returns from the function it has acquired the mutex.</font>
</p>


<p>
  <a name="1106327"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif"><em>Note:	 The same task can acquire a mutex any number of times without deadlock, but it must release it an equal number of times.</em></font>
</p>

<p>
  <a name="1106328"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">If you want to make certain that the task does not wait indefinitely for a mutex then use <font size=2 face=Courier><strong>mutex_trylock()</strong></font>, which attempts to gain ownership of the mutex, but fails immediately if it is not available.</font>
</p>


<p>
  <a name="1106329"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">A task is automatically made immortal while it has ownership of a mutex.</font>
</p>


<p>
  <a name="1106330"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">When a task wants to release the mutex, it calls <font size=2 face=Courier><strong>mutex_release()</strong></font>:</font>
</p>


<a name="1106331"> </a><font size=2 face=Courier><strong>int mutex_release(mutex_t* mutex);<br></strong></font>



<p>
  <a name="1106332"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">This looks at the queue of waiting tasks, and if the queue is not empty, removes the first task from the queue and, if it is not of a lower priority, assigns ownership of the mutex to that task, and makes it executable. If there are no tasks waiting, then the mutex becomes free.</font>
</p>


<p>
  <a name="1106333"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif"><em>Note:	 If a task exits whilst holding a mutex, the mutex remains locked, and a deadlock is inevitable.</em></font>
</p>

<h3>
  <a name="1106335"> </a><font color="#003366"  face="Verdana, Arial, Helvetica, sans-serif">7.1.1 	 Priority inversion</font>
</h3>


<p>
  <a name="1106339"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">Priority mutexes also provide protection against priority inversion. This can occur when a low priority task acquires a mutex, and then a high priority task tries to claim it. The high priority task is then forced to wait for the low priority task to release the mutex before it can proceed. If an intermediate priority task now becomes ready to run, it preempts the low priority task. A lower priority task (that is not holding the mutex in question) is therefore blocking the execution of a higher priority task, this is termed priority inversion. Priority mutexes are able to detect when this occurs, and correct the situation. This is done by temporarily boosting the low priority task's priority to be the same as the priority of the highest priority waiting task, all the while the low priority task owns the mutex. </font>
</p>


<p>
  <a name="1106340"> </a><font size=2  face="Verdana, Arial, Helvetica, sans-serif">Priority inversion detection occurs every time a task has to queue to get a priority mutex, every time a task releases a priority mutex, and every time a task changes priority.</font>
</p>
</blockquote>

<hr>



<table width="331" border="0" align="left" cellpadding="0" cellspacing="0">
  <tr>
    <td><a href="os20toc.htm"><img src="images/navtoc.gif" width="84" height="23"
    border="0" alt="TOC"> </a></td>
    <td><a href="mutex.htm"><img src="images/navprev.gif" width="80" height="23"
    border="0" alt="PREV"> </a></td>
    <td><a href="mutex3.htm"><img src="images/navnext.gif" width="83" height="23"
    border="0" alt="NEXT"> </a></td>
    <td><a href="os20ix.htm"><img src="images/navidx.gif" width="84" height="23"
    border="0" alt="INDEX"> </a></td>
  </tr>
</table>
<font size=1 face="Verdana, Arial, Helvetica, sans-serif">&nbsp; &copy; 2001, 2002, 2003 STMicroelectronics. All Rights Reserved.<br>
&nbsp; ADCS 7473749B</font>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -