ibm-information-center/dist/eclipse/plugins/i5OS.ic.apis_5.4.0.1/users_88.htm

256 lines
10 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<!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>pthread_rwlock_timedrdlock_np()--Get Shared Read Lock with Time-Out</title>
<!-- 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. -->
<!-- Begin Header Records ========================================== -->
<!-- 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>pthread_rwlock_timedrdlock_np()--Get Shared Read Lock with Time-Out</h2>
<div class="box" style="width: 60%;">
<br>
&nbsp;&nbsp;Syntax:
<pre> #include &lt;pthread.h&gt;
#include &lt;time.h&gt;
int pthread_rwlock_timedrdlock_np(pthread_rwlock_t *rwlock,
const struct timespec *deltatime);</pre>
&nbsp;&nbsp;Service Program Name: QP0WPTHR<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Default Public Authority: *USE <br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Threadsafe: Yes<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Signal Safe: Yes<br>
<!-- iddvc RMBR -->
<br>
</div>
<p>The <strong>pthread_rwlock_timedrdlock_np</strong>() function attempts to acquire a shared read lock on the read/write lock specified by <em>rwlock</em>. If the shared read lock cannot be acquired in the <em>deltatime</em> specific, <strong>pthread_rwlock_timedrdlock_np</strong>() returns the <strong>EBUSY</strong> error.</p>
<p>Any number of threads can hold shared read locks on the same read/write lock object. If any thread holds an exclusive write lock on a read/write lock object, no other threads are allowed to hold a shared read or exclusive write lock.</p>
<p>If no threads are holding an exclusive write lock on the read/write lock, the calling thread successfully acquires the shared read lock.</p>
<p>If the calling thread already holds a shared read lock on the read/write lock, another read lock can be successfully acquired by the calling thread. If more than one shared read lock is successfully acquired by a thread on a read/write lock object, that thread is required to successfully call <strong>pthread_rwlock_unlock</strong>() a matching number of times.</p>
<p>With a large number of readers and relatively few writers, there is the possibility of writer starvation. If threads are waiting for an exclusive write lock on the read/write lock and there are threads that currently hold a shared read lock, the shared read lock request is granted.</p>
<p>If the read/write lock is destroyed while <strong>pthread_rwlock_timedrdlock_np</strong>() is waiting for the shared read lock, the <strong>EDESTROYED</strong> error is returned.</p>
<p>If a signal is delivered to the thread while it is waiting for the lock, the signal handler (if any) runs, and the thread resumes waiting. For a timed wait, when the thread resumes waiting after the signal handler runs, the wait time is reset. For example, suppose a thread specifies that it should wait for a lock for 5 seconds, and a signal handler runs in that thread after 2.5 seconds. After returning from the signal handler, the thread will resume its wait for another 5 seconds. The resulting wait is longer than the specified 5 seconds.</p>
<br>
<h3>Read/Write Lock Deadlocks</h3>
<p>If a thread ends while holding a write lock, the attempt by another thread to acquire a shared read or exclusive write lock will not succeed. In this case, the attempt to acquire the lock will return the <strong>EBUSY</strong> error after the specified time elapses for the lock operation. If a thread ends while holding a read lock, the system automatically releases the read lock.</p>
<p>For the <strong>pthread_rwlock_timedrdlock_np</strong>() function, the pthreads run-time simulates the deadlock that has occurred in your application. When you are attempting to debug these deadlock scenarios, the CL command WRKJOB, option 20, shows the thread as in a condition wait. Displaying the call stack shows that the function <strong>timedDeadlockOnOrphanedRWLock</strong> is in the call stack.</p>
<br>
<h3>Upgrade / Downgrade a Lock</h3>
<p>If the calling thread currently holds an exclusive write lock on the read/write lock object, the shared read lock request is granted. After the shared read lock request is granted, the calling thread holds <strong>both</strong> the shared read <strong>and</strong> the exclusive write lock for the specified read/write lock object. If the thread calls <strong>pthread_rwlock_unlock</strong>() while holding one or more shared read locks <strong>and</strong> one or more exclusive write locks, the exclusive write locks are unlocked first. If more than one outstanding exclusive write lock was held by the thread, a matching number of successful calls to <strong>pthread_rwlock_unlock</strong>() must be done before all write locks are unlocked. At that time, subsequent calls to <strong>pthread_rwlock_unlock</strong>() will unlock the shared read locks.</p>
<p>You can use this behavior to allow your application to upgrade or downgrade one lock type to another. See <a href="concep26.htm#293561">Read/write locks can be upgraded/downgraded</a>.</p>
<br>
<h3>Authorities and Locks</h3>
<p>None.</p>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong>rwlock</strong></dt>
<dd>(Input) The address of the read/write lock</dd>
<dt><strong>deltatime</strong></dt>
<dd>(Input) The number of seconds and nanoseconds to wait for the lock before returning an error</dd>
</dl>
<br>
<h3>Return Value</h3>
<dl>
<dt><strong>0</strong></dt>
<dd><strong>pthread_rwlock_timedrdlock_np</strong>() was successful.</dd>
<dt><strong>value</strong></dt>
<dd><strong>pthread_rwlock_timedrdlock_np</strong>() was not successful. <em>value</em> is set to indicate the error condition.</dd>
</dl>
<br>
<h3>Error Conditions</h3>
<p>If <strong>pthread_rwlock_timedrdlock_np</strong>() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.</p>
<dl>
<dt><em>[EINVAL]</em></dt>
<dd><p>The value specified for the argument is not correct.</p></dd>
<dt><em>[EBUSY]</em></dt>
<dd><p>The lock could not be acquired in the time specified.</p></dd>
<dt><em>[EDESTROYED]</em></dt>
<dd><p>The lock was destroyed while waiting.</p></dd>
</dl>
<br>
<h3>Related Information</h3>
<ul>
<li>The &lt;<strong>pthread.h</strong>> header file. See <a href="rzah4hed.htm">Header files for Pthread functions</a>.<br><br></li>
<li><a href="users_86.htm">pthread_rwlock_init()</a>--Initialize Read/Write Lock<br><br></li>
<li><a href="users_87.htm">pthread_rwlock_rdlock()</a>--Get Shared Read Lock<br><br></li>
<li><a href="users_89.htm">pthread_rwlock_timedwrlock_np()</a>--Get Exclusive Write Lock with Time-Out<br><br></li>
<li><a href="users_90.htm">pthread_rwlock_tryrdlock()</a>--Get Shared Read Lock with No Wait<br><br></li>
<li><a href="users_91.htm">pthread_rwlock_trywrlock()</a>--Get Exclusive Write Lock with No Wait<br><br></li>
<li><a href="users_92.htm">pthread_rwlock_unlock()</a>--Unlock Exclusive Write or Shared Read Lock<br><br></li>
<li><a href="users_93.htm">pthread_rwlock_wrlock()</a>--Get Exclusive Write Lock</li>
</ul>
<br>
<h3><a name="372828">Example</a></h3>
<p>See <a href="../apiref/aboutapis.htm#codedisclaimer">Code disclaimer information</a>
for information pertaining to code examples.</p>
<pre>#define _MULTI_THREADED
#include &lt;pthread.h>
#include &lt;stdio.h>
#include &quot;check.h&quot;
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
void *rdlockThread(void *arg)
{
int rc;
int count=0;
struct timespec ts;
/* 1.5 seconds */
ts.tv_sec = 1;
ts.tv_nsec = 500000000;
printf(&quot;Entered thread, getting read lock with timeout\n&quot;);
Retry:
rc = pthread_rwlock_timedrdlock_np(&amp;rwlock, &amp;ts);
if (rc == EBUSY) {
if (count >= 10) {
printf(&quot;Retried too many times, failure!\n&quot;);
exit(EXIT_FAILURE);
}
++count;
printf(&quot;RETRY...\n&quot;);
goto Retry;
}
checkResults(&quot;pthread_rwlock_rdlock() 1\n&quot;, rc);
sleep(2);
printf(&quot;unlock the read lock\n&quot;);
rc = pthread_rwlock_unlock(&amp;rwlock);
checkResults(&quot;pthread_rwlock_unlock()\n&quot;, rc);
printf(&quot;Secondary thread complete\n&quot;);
return NULL;
}
int main(int argc, char **argv)
{
int rc=0;
pthread_t thread;
printf(&quot;Enter Testcase - %s\n&quot;, argv[0]);
printf(&quot;Main, get the write lock\n&quot;);
rc = pthread_rwlock_wrlock(&amp;rwlock);
checkResults(&quot;pthread_rwlock_wrlock()\n&quot;, rc);
printf(&quot;Main, create the timed rd lock thread\n&quot;);
rc = pthread_create(&amp;thread, NULL, rdlockThread, NULL);
checkResults(&quot;pthread_create\n&quot;, rc);
printf(&quot;Main, wait a bit holding the write lock\n&quot;);
sleep(5);
printf(&quot;Main, Now unlock the write lock\n&quot;);
rc = pthread_rwlock_unlock(&amp;rwlock);
checkResults(&quot;pthread_rwlock_unlock()\n&quot;, rc);
printf(&quot;Main, wait for the thread to end\n&quot;);
rc = pthread_join(thread, NULL);
checkResults(&quot;pthread_join\n&quot;, rc);
rc = pthread_rwlock_destroy(&amp;rwlock);
checkResults(&quot;pthread_rwlock_destroy()\n&quot;, rc);
printf(&quot;Main completed\n&quot;);
return 0;
}</pre>
<p><strong>Output:</strong></p>
<pre>Enter Testcase - QP0WTEST/TPRWLRD0
Main, get the write lock
Main, create the timed rd lock thread
Main, wait a bit
Entered thread, getting read lock with timeout
RETRY...
RETRY...
RETRY...
Main, Now unlock the write lock
Main, wait for the thread to end
unlock the read lock
Secondary thread complete
Main completed</pre>
<hr>
API introduced: V4R3
<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>