1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2<HTML> 3<HEAD> 4<META http-equiv="Content-Type" content="text/html; charset=windows-1252"> 5<META name="GENERATOR" content="IBM WebSphere Studio Homepage Builder Version 7.0.1.0 for Windows"> 6<META http-equiv="Content-Style-Type" content="text/css"> 7<TITLE>Mutual Exclusion (Mutex): Overview </TITLE> 8<LINK rel="stylesheet" href="../../css/nitro.css" type="text/css"> 9</HEAD> 10<BODY> 11<H1 align="left">Mutual Exclusion (Mutex): Overview</H1> 12<P>A mutex (<I>mutual exclusion</I> service) is a mechanism to control thread execution. This mechanism can prevent multiple threads from executing simultaneously in particular places in the program and from simultaneously accessing data, registers, and other resources.</P> 13<H2>Initializing Mutexes</H2> 14<P>A mutex is initialized with the <a href="OS_InitMutex.html"><CODE>OS_InitMutex</CODE></a> function. One mutex can be configured for each OSMutex structure object.</P> 15<H2>Mutex Structure</H2> 16<P>The <CODE>OSMutex</CODE> structure is as follows.</P> 17<TABLE border="1"> 18 <TBODY> 19 <TR> 20 <TD> 21<pre>struct OSMutex 22{ 23 OSThreadQueue queue; 24 OSThread* thread; // the current owner 25 s32 count; // lock count (notice: use upper 1 byte as mutex type) 26 27 OSMutex* prev; // link for OSThread.queueMutex 28 OSMutex* next; // link for OSThread.queueMutex 29};</pre> 30 </TD> 31 </TR> 32 </TBODY> 33</TABLE> 34<P><BR>The members <CODE>prev</CODE> and <CODE>next</CODE> manage mutex lists, but <CODE>queue</CODE>, <CODE>thread</CODE>, and <CODE>count</CODE> compose the actual mutex synchronization feature.</P> 35<P><code>queue</code> is the thread queue where the thread that is being made to wait by mutex is registered.</P> 36<P><CODE>thread</CODE> is the member that registers the current thread that is using this mutex to block other threads.</P> 37<P><code>count</code> is the member that manages the lock nest for this mutex. It counts the number of times the lock has been set. However, the upper 1 byte is used to indicate the mutex type. The remaining 24 bits are used for the count value. The type is included in <CODE>count</CODE> instead of setting it to a separate member in order to retain compatibility with previous versions of mutex. (<B>Note:</B> Because the emphasis is on speed, the upper limit of the count value is not checked. One mutex can use 24 bits, which is equal to roughly 1.667 million nested levels. That limit will probably never be reached.)</P> 38<P>The following section explains mutex types.</P> 39<H2>Mutex Types</H2> 40<P>The mutex types are STD, R, and W. (These are defined in the header as <CODE>OS_MUTEX_TYPE_STD</CODE> and so on. In the following explanation, they are referred to as STD, R, and W.) If no type is specified, <I>NONE</I> is used.</P> 41<P>For normal thread synchronization, an STD mutex is used. With this type of mutex, other threads are not permitted unconditional entry. </P> 42<P>The R and W types are called read-lock and write-lock mutexes.</P> 43<P>The read-lock mutex does not allow other threads to perform write or standard mutex locks. This type can be used when data is read. Multiple threads can simultaneously read the data without problem, but a data-writing process at this point could compromise the data consistency.</P> 44<P>The write-lock mutex can be used when a thread is writing data. The data to be written cannot come from multiple threads. The W mutex does not permit another thread to read the data during the writing process. A W mutex has lock policy similar to that of a STD mutex. They are not exactly the same because there are functions to change between read-lock and write-lock mutexes.</P> 45<P>An R mutex can change to a W type, and a W mutex can change an R type, provided there is only one level of locking by the thread with the mutex. In the following example, after data is written, the process immediately changes to reading without releasing the lock (thus permitting reading by another thread).</P> 46 47<BLOCKQUOTE style="background-color:#ffffcc"><PRE>OSMutex mutex; 48OS_InitMutex( &mutex ); 49 50<B>void write_and_read()</B> 51{ 52 OS_LockMutexW( &mutex ); <FONT color="#ff0000"><- write lock</FONT> 53 writeData(); 54 OS_LockMutexFromWToR( &mutex ); <FONT color="#ff0000"><- Change from write lock to read lock</FONT> 55 readData(); 56 OS_UnlockMutexR(); <FONT color="#ff0000"><- Unlock with a read lock</FONT> 57} 58 59<B>void read()</B> 60{ 61 OS_LockMutexR( &mutex ); 62 readData(); 63 OS_UnlockMutexR( &mutex ); 64} 65 66 67<B>thread1()</B> 68{ 69 write_and_read(); 70} 71 72<B>thread2()</B> 73{ 74 read(); 75} 76</PRE></BLOCKQUOTE> 77 78<H2>Mutex Operations</H2> 79<P>The following operations can be performed on a mutex after it has been initialized.</P> 80<P> - Lock the mutex (<CODE>Lock</CODE> functions):<BR> 81<A href="OS_LockMutex.html"><CODE>OS_LockMutex</CODE></A>, <A href="OS_LockMutexRW.html"><CODE>OS_LockMutexR</CODE></A>, <A href="OS_LockMutexRW.html"><CODE>OS_LockMutexW</CODE></A></P> 82<P> - Unlock the mutex (<CODE>Unlock</CODE> functions):<BR> 83<A href="OS_UnlockMutex.html"><CODE>OS_UnlockMutex</CODE></A>, <A href="OS_UnlockMutexRW.html"><CODE>OS_UnlockMutexR</CODE></A>, <A href="OS_UnlockMutexRW.html"><CODE>OS_UnlockMutexW</CODE></A>, <A href="OS_UnlockMutexRW.html"><CODE>OS_UnlockMutexRW</CODE></A></P> 84<P> - Attempt to lock the mutex (<CODE>TryLock</CODE> functions):<BR> 85<A href="OS_TryLockMutex.html"><CODE>OS_TryLockMutex</CODE></A>, <A href="OS_TryLockMutexRW.html"><CODE>OS_TryLockMutexR</CODE></A>, <A href="OS_TryLockMutexRW.html"><CODE>OS_TryLockMutexW</CODE></A></P> 86<P> </P> 87<P>When you lock a mutex, other threads are blocked from locking that mutex. However, read-lock mutexes do not block each other. A blocked thread waits for the mutex to be unlocked.</P> 88<P><CODE>Lock</CODE> functions wait until the lock is completed, but the <CODE>TryLock</CODE> functions return immediately, regardless of whether the mutex can be locked. You can determine from the returned value whether the lock was successful.</P> 89<P>The following operations are available for read-lock and write-lock mutexes.</P> 90<P> - Change the mutex type: <BR> 91<A href="OS_LockMutexFromToRW.html"><CODE>OS_LockMutexFromRToW</CODE></A>, <A href="OS_LockMutexFromToRW.html"><CODE>OS_LockMutexFromWToR</CODE></A></P> 92<P> - Attempt to change the mutex type: <BR> 93<A href="OS_TryLockMutexFromToRW.html"><CODE>OS_TryLockMutexFromRToW</CODE></A>, <A href="OS_TryLockMutexFromToRW.html"><CODE>OS_TryLockMutexFromWToR</CODE></A></P> 94<P> </P> 95<H2>Internal Operations: Locking a Mutex</H2> 96<P>This section explains the internal operations when a mutex is locked.</P> 97<P>When the mutex is initialized by the <A href="OS_InitMutex.html"><CODE>OS_InitMutex</CODE></A> function, the owner of the <CODE>OSMutex</CODE> thread, the count value, the thread type, and the thread queue have the values shown in the following table. (The <CODE>count</CODE> member of <CODE>OSMutex</CODE> contains two pieces of information: the count value and the mutex type. This information is shown as <I>count</I> and <I>type</I>.)</P> 98<BLOCKQUOTE><IMG src="image_initial_member.gif" border="0"></BLOCKQUOTE> 99<P>This section explains locking with a standard mutex, not with a read-lock or write-lock mutex.</P> 100<P>If the <a href="OS_LockMutex.html"><CODE>OS_LockMutex</CODE></a> function is called in a program and the designated mutex is not being used in a lock at that point in time, <code>OS_LockMutex</code> records and locks the current thread.</P> 101<P>If a <a href="OS_LockMutex.html"><CODE>OS_LockMutex</CODE></a> call occurs for an already locked mutex, the mutex count is incremented, and the mutex is bypassed if it is being locked by the same thread as the current thread. If the thread is not the same, it goes into a pause state until the lock is released. Thread rescheduling occurs at this time.</P> 102<P>For example, consider the figure below when there is a <code>Mutex1</code> and a program that uses it for locking.</P> 103<BLOCKQUOTE><IMG src="image_mutex_l1.gif" border="0"></BLOCKQUOTE> 104<P><code>thread1</code> attempts to execute this program. <CODE>thread1</CODE> executes the mutex lock function <A href="OS_LockMutex.html"><CODE>OS_LockMutex( &Mutex1 )</CODE></A>, but because <CODE>Mutex1</CODE> has not yet been used (determined to be true if its owner thread is <CODE>NULL</CODE>), <CODE>thread1</CODE> is recorded as the <CODE>Mutex1</CODE> owner thread, the count is set to <CODE>1</CODE>, and the program returns from the lock function.</P> 105<BLOCKQUOTE><IMG src="image_mutex_l2.gif" border="0"></BLOCKQUOTE> 106<P>Next, consider what happens when a separate thread, <code>thread2</code>, attempts to execute the same program. <CODE>thread2</CODE> runs <A href="OS_LockMutex.html"><CODE>OS_LockMutex( &Mutex1 )</CODE></A>, but <CODE>Mutex1</CODE> is already being used in a lock. Because <code>thread2</code> is not the <code>mutex1</code> owner thread, <code>thread2</code> cannot proceed any further. As a result, <code>thread2</code> goes into a pause state. At this point, <code>thread2</code> is registered inside the <code>mutex1</code> thread queue as waiting for this lock to be released.<BLOCKQUOTE><IMG src="image_mutex_l3.gif" border="0"></BLOCKQUOTE> 107<P>Now consider a case where <CODE>thread1</CODE> once again runs <A href="OS_LockMutex.html"><CODE>OSLockMutex( &Mutex1 )</CODE></A> (on the same line or a different line). <code>mutex1</code> is already being used in a lock, but the <code>mutex1</code> owner thread is the same as the current thread (<code>thread1</code>), so it can be bypassed. When this happens, the <code>mutex1</code> count increments.<BLOCKQUOTE><IMG src="image_mutex_l4.gif" border="0"></BLOCKQUOTE> 108<P>There are also times when multiple threads are registered in a single mutex thread queue. These threads are all blocked by that mutex. However, it is not necessarily the case that all threads are in a pause state on the same line. If the mutex is unlocked, all registered threads go into an executable state.<BLOCKQUOTE><IMG src="image_mutex_l5.gif" border="0"></BLOCKQUOTE> 109<H2>Internal Operations: Unlocking a Mutex</H2> 110<P>This section explains the internal operations when a mutex lock is being released (unlocked). Here, too, the explanation is in regards to a normal mutex and not a read-lock or write-lock mutex.</P> 111<P>The function for unlocking with mutex is <a href="OS_UnlockMutex.html"><CODE>OS_UnlockMutex</CODE></a>. However, calling this function does not invariably unlock the mutex. Only when the <CODE>count</CODE> value inside the mutex has decremented to 0 is the lock released. If the value is not zero, <CODE>count</CODE> is simply decremented, and control returns from the function.</P> 112<P>For example, consider the case in the figure below where <code>thread1</code> runs <A href="OS_UnlockMutex.html"><CODE>OS_UnlockMutex( &Mutex1 )</CODE></A> while using <code>Mutex1</code> to block <code>thread2</code>. 113<BLOCKQUOTE><IMG src="image_mutex_u1.gif" border="0"></BLOCKQUOTE> 114<P>At this time, the count has been decremented to zero, so the thread registered in the thread queue is put into an executable state and is rescheduled. In this case, <code>thread2</code> goes into an executable state.</P> 115<BLOCKQUOTE><IMG src="image_mutex_u2.gif" border="0"></BLOCKQUOTE> 116<P>If <code>thread2</code> has higher priority than <code>thread1</code>, the threads are switched. In other words, <code>thread1</code> pauses, and <code>thread2</code> executes. In attempting to execute <A href="OS_LockMutex.html"><CODE>OS_LockMutex( &Mutex1 )</CODE></A>, <code>thread2</code> was blocked by <code>thread1</code>. But this time, because <code>Mutex1</code> is released and initialized, <code>thread2</code> uses <code>Mutex1</code> to block the other threads.<BLOCKQUOTE><IMG src="image_mutex_u3.gif" border="0"><BR> 117</BLOCKQUOTE> 118<P>When <code>thread1</code> runs the <a href="OS_UnlockMutex.html"><CODE>OS_UnlockMutex</CODE></a> function and the count value is not zero after being decremented, it can be considered that there are other locks that have not been unlocked, and this can be bypassed. If this is the case, no thread rescheduling occurs. In this way, mutex locks can take on a nested structure.<BLOCKQUOTE><IMG src="image_mutex_u4.gif" border="0"></BLOCKQUOTE> 119<H2>Attempting to Lock a Mutex</H2> 120<P>Until there is a lock, <a href="OS_LockMutex.html"><CODE>OS_LockMutex</CODE></a> does not return from the function. On the other hand, <a href="OS_TryLockMutex.html"><CODE>OS_TryLockMutex</CODE></a> is a function that locks if it can, but that returns immediately if it cannot lock. 121 122 123 124 125<P>With the <a href="OS_TryLockMutex.html"><CODE>OS_TryLockMutex</CODE></a> function, you can learn from its return value whether the lock was a success. 126 127 128 129 130<P>These other try functions have been prepared in the same way.<P> - For <A href="OS_LockMutexRW.html"><CODE>OS_LockMutexR</CODE></A>, there is the <A href="OS_TryLockMutexRW.html"><CODE>OS_TryLockMutexR</CODE></A> function 131<P> - For <A href="OS_LockMutexRW.html"><CODE>OS_LockMutexW</CODE></A>, there is the <A href="OS_TryLockMutexRW.html"><CODE>OS_TryLockMutexW</CODE></A> function 132<P> - For <A href="OS_LockMutexFromToRW.html"><CODE>OS_LockMutexFromRToW</CODE></A>, there is the <A href="OS_TryLockMutexFromToRW.html"><CODE>OS_TryLockMutexFromRToW</CODE></A> function 133 134<P> - For <A href="OS_LockMutexFromToRW.html"><CODE>OS_LockMutexFromWToR</CODE></A>, there is the <A href="OS_TryLockMutexFromToRW.html"><CODE>OS_TryLockMutexFromWToR</CODE></A> function 135 136<H2>When the Thread Ends</H2> 137<P>When the thread ends, every mutex locking that thread is unlocked. This is true not only for normal mutexes, but also for read-lock and write-lock mutexes.</P> 138<H2>Warning Messages</H2> 139<P>When a function specifies a pointer to a mutex that is <CODE>NULL</CODE> or the call is clearly unusual in some other way, the DEBUG build catches on the <A href="../debug/SDK_ASSERT.html"><CODE>SDK_ASSERT</CODE></A> function. If an unlock function is called for a mutex that has not been locked, "<FONT color="#ff0000">Illegal unlock mutex</FONT>" appears. If this message appears, verify that the lock and unlock levels accurately correspond. Note that this message is output by the <A href="../debug/OS_TWarning.html"><CODE>SDK_TWarning</CODE></A> function, so the program does not stop.</P> 140<H2>See Also</H2> 141<P><CODE><A href="../list_os.html#Mutex">Overview of OS Functions (Exclusion Control)<BR></A></CODE></P> 142<H2>Revision History</H2> 143<P>2009/03/11 Standardized terminology for "lock" and "block." Revised some of the text in the figures. <BR>2008/12/17 Revised contents to take read- and write-lock into account.<BR> 1442004/12/14 Revised terminology and word endings. <BR>2004/11/11 Initial version.</P> 145<hr><p>CONFIDENTIAL</p></body> 146</HTML>