158 lines
6.4 KiB
HTML
158 lines
6.4 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
|
|
<title>Shared read/write locks are released at thread termination</title>
|
|
<!-- Begin Header Records ========================================== -->
|
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
|
<!-- US Government Users Restricted Rights -->
|
|
<!-- Use, duplication or disclosure restricted by -->
|
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
|
<!-- Change History: -->
|
|
<!-- YYMMDD USERID Change description -->
|
|
<!-- NETMG2 SCRIPT A converted by B2H R4.1 (346) (CMS) by HOLTJM at -->
|
|
<!-- RCHVMW2 on 29 Jan 1999 at 10:01:37 -->
|
|
<!--File Edited November 2001 -->
|
|
<!--End Header Records -->
|
|
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
|
|
</head>
|
|
<body>
|
|
<!-- Java sync-link -->
|
|
<script language="Javascript" src="../rzahg/synch.js" type="text/javascript">
|
|
</script>
|
|
|
|
<a name="Top_Of_Page"></a>
|
|
|
|
<h2>Shared read/write locks are released at thread termination</h2>
|
|
|
|
<p>If a thread is the owner of one or more shared read locks acquired by
|
|
<strong>pthread_rwlock_unlock()</strong>, <strong>pthread_rwlock_tryrdlock()</strong>,
|
|
or <strong>pthread_rwlock_timedrdlock_np()</strong>, when that thread terminates, the
|
|
shared read locks are automatically released by the system. If a thread holds a
|
|
shared read lock, it does not modify the resources associated with that lock.
|
|
It is then safe for the runtime support to unlock the read lock without
|
|
indicating an error condition or causing the process to wait. For performance
|
|
reasons, your application should unlock all held locks before the thread
|
|
ends.</p>
|
|
|
|
<p>If a thread is the owner of one or more exclusive write locks acquired by
|
|
<strong>pthread_rwlock_wrlock()</strong>, <strong>pthread_rwlock_trywrlock()</strong>,
|
|
or <strong>pthread_rwlock_timedwrlock_np()</strong>, when that thread terminates, the
|
|
exclusive write locks are not automatically released by the system. This is an
|
|
error in the application and indicates that the data associated with the lock
|
|
is in an inconsistent state. If another thread attempts to get a shared read or
|
|
exclusive write lock on the lock, that thread blocks forever.</p>
|
|
|
|
<br>
|
|
<h2><a name="293561">Read/write locks can be upgraded/downgraded</a></h2>
|
|
|
|
<p>The i5/OS implementation of read/write locks allows a thread to effectively
|
|
change a read lock to a write lock, or change a write lock to a read lock,
|
|
without an intervening unlocked and unprotected section of code. The following
|
|
items describe read/write lock behavior that allows these changes. This
|
|
behavior is outside of the definition of the Single UNIX Specification. An
|
|
application written to be portable to the Single UNIX Specification should not
|
|
attempt to acquire a shared read lock and a shared write lock on the same
|
|
read/write lock at the same time.</p>
|
|
|
|
<ul>
|
|
<li>If a thread currently holds a shared read lock, an attempt by the same
|
|
thread to acquire an exclusive write lock succeeds if no other threads hold a
|
|
shared read lock. The thread then holds both an exclusive write lock and a
|
|
shared read lock.</li>
|
|
|
|
<li>If a thread currently holds an exclusive write lock, an attempt by the
|
|
thread to acquire a shared read lock succeeds. The thread then holds both an
|
|
exclusive write lock and a shared read lock.</li>
|
|
|
|
<li>If a thread holds one or more shared read locks and one or more exclusive
|
|
write locks on the same read/write lock object at the same time, a call to
|
|
<strong>pthread_rwlock_unlock</strong>() always unlocks the exclusive write
|
|
lock FIRST.</li>
|
|
|
|
<li>When multiple exclusive write locks and multiple exclusive read locks are
|
|
held by the same thread on the same read/write lock object, the behavior of
|
|
<strong>pthread_rwlock_unlock</strong>() is as follows:
|
|
|
|
<ul>
|
|
<li>A call to the <strong>pthread_rwlock_unlock</strong>() function always
|
|
unlocks the most recent exclusive write lock first.</li>
|
|
|
|
<li>Subsequent calls to <strong>pthread_rwlock_unlock</strong>() first reduce
|
|
the count of any outstanding exclusive write locks held by the thread until all
|
|
exclusive write locks are unlocked.</li>
|
|
|
|
<li>After all outstanding exclusive write locks are unlocked and the thread
|
|
holds only shared read locks on the read/write lock object, a call to <strong>
|
|
pthread_rwlock_unlock</strong>() function then unlocks the most recent shared
|
|
read lock.</li>
|
|
|
|
<li>Subsequent calls to <strong>pthread_rwlock_unlock</strong>() reduce the
|
|
count of any outstanding shared read locks held by the thread until all shared
|
|
read locks are unlocked.</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<p>For a thread to change a shared read lock to an exclusive write lock, the
|
|
thread should perform the following actions:</p>
|
|
|
|
<pre>
|
|
{
|
|
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
|
|
pthread_rwlock_rdlock(&rwlock);
|
|
...
|
|
/* Thread holding a read lock decides it needs to upgrade to a write lock */
|
|
/* Now Upgrade to write lock */
|
|
pthread_rwlock_wrlock(&rwlock);
|
|
...
|
|
/* write lock (and read lock) are held here.*/
|
|
/* We have effectively upgraded to a write lock */
|
|
...
|
|
/* `Downgrade' back to a only the read lock */
|
|
pthread_rwlock_unlock(&rwlock);
|
|
...
|
|
/* unlock the read lock */
|
|
pthread_rwlock_unlock(&rwlock);
|
|
}
|
|
</pre>
|
|
|
|
<p>For a thread to change an exclusive write lock to a shared read lock, the
|
|
thread should perform the following actions:</p>
|
|
|
|
<pre>
|
|
{
|
|
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
|
|
pthread_rwlock_wrlock(&rwlock);
|
|
..
|
|
/* Thread holding the write lock decides it needs to downgrade to a read lock */
|
|
/* Get the read lock, so we are holding BOTH read and write locks */
|
|
pthread_rwlock_rdlock(&rwlock);
|
|
...
|
|
/* An unlock always unlocks the write lock first */
|
|
pthread_wrlock_unlock(&rwlock);
|
|
...
|
|
/* At this point, we are only holding the read lock. */
|
|
/* We have effectively downgraded the write lock to a read lock */
|
|
...
|
|
/* Use unlock to unlock the last read lock. */
|
|
pthread_wrlock_unlock(&rwlock);
|
|
}
|
|
</pre>
|
|
|
|
<hr>
|
|
<center>
|
|
<table cellpadding="2" cellspacing="2">
|
|
<tr align="center">
|
|
<td valign="middle" align="center">
|
|
<a href="#Top_Of_Page">Top</a> |
|
|
<a href="rzah4mst.htm">Pthread APIs</a> |
|
|
<a href="aplist.htm">APIs by category</a></td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
</body>
|
|
</html>
|
|
|