Files
oldlinux-files/study/Ref-docs/POSIX/susv3/functions/pthread_cleanup_pop.html
2024-02-19 00:25:23 -05:00

321 lines
12 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>pthread_cleanup_pop</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="pthread_cleanup_pop"></a> <a name="tag_03_514"></a><!-- pthread_cleanup_pop -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_514_01"></a>NAME</h4>
<blockquote>pthread_cleanup_pop, pthread_cleanup_push - establish cancelation handlers</blockquote>
<h4><a name="tag_03_514_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<div class="box"><code><tt><sup>[<a href="javascript:open_code('THR')">THR</a>]</sup> <img src="../images/opt-start.gif" alt=
"[Option Start]" border="0"> #include &lt;<a href="../basedefs/pthread.h.html">pthread.h</a>&gt;<br>
<br>
void pthread_cleanup_pop(int</tt> <i>execute</i><tt>);<br>
void pthread_cleanup_push(void (*</tt><i>routine</i><tt>)(void*), void *</tt><i>arg</i><tt>); <img src="../images/opt-end.gif"
alt="[Option End]" border="0"></tt></code></div>
<tt><br>
</tt></blockquote>
<h4><a name="tag_03_514_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>pthread_cleanup_pop</i>() function shall remove the routine at the top of the calling thread's cancelation cleanup stack
and optionally invoke it (if <i>execute</i> is non-zero).</p>
<p>The <i>pthread_cleanup_push</i>() function shall push the specified cancelation cleanup handler <i>routine</i> onto the calling
thread's cancelation cleanup stack. The cancelation cleanup handler shall be popped from the cancelation cleanup stack and invoked
with the argument <i>arg</i> when:</p>
<ul>
<li>
<p>The thread exits (that is, calls <a href="../functions/pthread_exit.html"><i>pthread_exit</i>()</a>).</p>
</li>
<li>
<p>The thread acts upon a cancelation request.</p>
</li>
<li>
<p>The thread calls <i>pthread_cleanup_pop</i>() with a non-zero <i>execute</i> argument.</p>
</li>
</ul>
<p>These functions may be implemented as macros. The application shall ensure that they appear as statements, and in pairs within
the same lexical scope (that is, the <i>pthread_cleanup_push</i>() macro may be thought to expand to a token list whose first token
is <tt>'{'</tt> with <i>pthread_cleanup_pop</i>() expanding to a token list whose last token is the corresponding <tt>'}'</tt>
).</p>
<p>The effect of calling <a href="../functions/longjmp.html"><i>longjmp</i>()</a> or <a href=
"../functions/siglongjmp.html"><i>siglongjmp</i>()</a> is undefined if there have been any calls to <i>pthread_cleanup_push</i>()
or <i>pthread_cleanup_pop</i>() made without the matching call since the jump buffer was filled. The effect of calling <a href=
"../functions/longjmp.html"><i>longjmp</i>()</a> or <a href="../functions/siglongjmp.html"><i>siglongjmp</i>()</a> from inside a
cancelation cleanup handler is also undefined unless the jump buffer was also filled in the cancelation cleanup handler.</p>
</blockquote>
<h4><a name="tag_03_514_04"></a>RETURN VALUE</h4>
<blockquote>
<p>The <i>pthread_cleanup_push</i>() and <i>pthread_cleanup_pop</i>() functions shall not return a value.</p>
</blockquote>
<h4><a name="tag_03_514_05"></a>ERRORS</h4>
<blockquote>
<p>No errors are defined.</p>
<p>These functions shall not return an error code of [EINTR].</p>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_514_06"></a>EXAMPLES</h4>
<blockquote>
<p>The following is an example using thread primitives to implement a cancelable, writers-priority read-write lock:</p>
<pre>
<tt>typedef struct {
pthread_mutex_t lock;
pthread_cond_t rcond,
wcond;
int lock_count; /* &lt; 0 .. Held by writer. */
/* &gt; 0 .. Held by lock_count readers. */
/* = 0 .. Held by nobody. */
int waiting_writers; /* Count of waiting writers. */
} rwlock;
<br>
void
waiting_reader_cleanup(void *arg)
{
rwlock *l;
<br>
l = (rwlock *) arg;
pthread_mutex_unlock(&amp;l-&gt;lock);
}
<br>
void
lock_for_read(rwlock *l)
{
pthread_mutex_lock(&amp;l-&gt;lock);
pthread_cleanup_push(waiting_reader_cleanup, l);
while ((l-&gt;lock_count &lt; 0) &amp;&amp; (l-&gt;waiting_writers != 0))
pthread_cond_wait(&amp;l-&gt;rcond, &amp;l-&gt;lock);
l-&gt;lock_count++;
/*
* Note the pthread_cleanup_pop executes
* waiting_reader_cleanup.
*/
pthread_cleanup_pop(1);
}
<br>
void
release_read_lock(rwlock *l)
{
pthread_mutex_lock(&amp;l-&gt;lock);
if (--l-&gt;lock_count == 0)
pthread_cond_signal(&amp;l-&gt;wcond);
pthread_mutex_unlock(l);
}
<br>
void
waiting_writer_cleanup(void *arg)
{
rwlock *l;
<br>
l = (rwlock *) arg;
if ((--l-&gt;waiting_writers == 0) &amp;&amp; (l-&gt;lock_count &gt;= 0)) {
/*
* This only happens if we have been canceled.
*/
pthread_cond_broadcast(&amp;l-&gt;wcond);
}
pthread_mutex_unlock(&amp;l-&gt;lock);
}
<br>
void
lock_for_write(rwlock *l)
{
pthread_mutex_lock(&amp;l-&gt;lock);
l-&gt;waiting_writers++;
pthread_cleanup_push(waiting_writer_cleanup, l);
while (l-&gt;lock_count != 0)
pthread_cond_wait(&amp;l-&gt;wcond, &amp;l-&gt;lock);
l-&gt;lock_count = -1;
/*
* Note the pthread_cleanup_pop executes
* waiting_writer_cleanup.
*/
pthread_cleanup_pop(1);
}
<br>
void
release_write_lock(rwlock *l)
{
pthread_mutex_lock(&amp;l-&gt;lock);
l-&gt;lock_count = 0;
if (l-&gt;waiting_writers == 0)
pthread_cond_broadcast(&amp;l-&gt;rcond)
else
pthread_cond_signal(&amp;l-&gt;wcond);
pthread_mutex_unlock(&amp;l-&gt;lock);
}
<br>
/*
* This function is called to initialize the read/write lock.
*/
void
initialize_rwlock(rwlock *l)
{
pthread_mutex_init(&amp;l-&gt;lock, pthread_mutexattr_default);
pthread_cond_init(&amp;l-&gt;wcond, pthread_condattr_default);
pthread_cond_init(&amp;l-&gt;rcond, pthread_condattr_default);
l-&gt;lock_count = 0;
l-&gt;waiting_writers = 0;
}
<br>
reader_thread()
{
lock_for_read(&amp;lock);
pthread_cleanup_push(release_read_lock, &amp;lock);
/*
* Thread has read lock.
*/
pthread_cleanup_pop(1);
}
<br>
writer_thread()
{
lock_for_write(&amp;lock);
pthread_cleanup_push(release_write_lock, &amp;lock);
/*
* Thread has write lock.
*/
pthread_cleanup_pop(1);
}
</tt>
</pre>
</blockquote>
<h4><a name="tag_03_514_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>The two routines that push and pop cancelation cleanup handlers, <i>pthread_cleanup_push</i>() and <i>pthread_cleanup_pop</i>(),
can be thought of as left and right parentheses. They always need to be matched.</p>
</blockquote>
<h4><a name="tag_03_514_08"></a>RATIONALE</h4>
<blockquote>
<p>The restriction that the two routines that push and pop cancelation cleanup handlers, <i>pthread_cleanup_push</i>() and
<i>pthread_cleanup_pop</i>(), have to appear in the same lexical scope allows for efficient macro or compiler implementations and
efficient storage management. A sample implementation of these routines as macros might look like this:</p>
<pre>
<tt>#define pthread_cleanup_push(rtn,arg) { \
struct _pthread_handler_rec __cleanup_handler, **__head; \
__cleanup_handler.rtn = rtn; \
__cleanup_handler.arg = arg; \
(void) pthread_getspecific(_pthread_handler_key, &amp;__head); \
__cleanup_handler.next = *__head; \
*__head = &amp;__cleanup_handler;
<br>
#define pthread_cleanup_pop(ex) \
*__head = __cleanup_handler.next; \
if (ex) (*__cleanup_handler.rtn)(__cleanup_handler.arg); \
}
</tt>
</pre>
<p>A more ambitious implementation of these routines might do even better by allowing the compiler to note that the cancelation
cleanup handler is a constant and can be expanded inline.</p>
<p>This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 currently leaves unspecified the effect of calling <a href=
"../functions/longjmp.html"><i>longjmp</i>()</a> from a signal handler executing in a POSIX System Interfaces function. If an
implementation wants to allow this and give the programmer reasonable behavior, the <a href=
"../functions/longjmp.html"><i>longjmp</i>()</a> function has to call all cancelation cleanup handlers that have been pushed but
not popped since the time <a href="../functions/setjmp.html"><i>setjmp</i>()</a> was called.</p>
<p>Consider a multi-threaded function called by a thread that uses signals. If a signal were delivered to a signal handler during
the operation of <a href="../functions/qsort.html"><i>qsort</i>()</a> and that handler were to call <a href=
"../functions/longjmp.html"><i>longjmp</i>()</a> (which, in turn, did <i>not</i> call the cancelation cleanup handlers) the helper
threads created by the <a href="../functions/qsort.html"><i>qsort</i>()</a> function would not be canceled. Instead, they would
continue to execute and write into the argument array even though the array might have been popped off the stack.</p>
<p>Note that the specified cleanup handling mechanism is especially tied to the C language and, while the requirement for a uniform
mechanism for expressing cleanup is language-independent, the mechanism used in other languages may be quite different. In
addition, this mechanism is really only necessary due to the lack of a real exception mechanism in the C language, which would be
the ideal solution.</p>
<p>There is no notion of a cancelation cleanup-safe function. If an application has no cancelation points in its signal handlers,
blocks any signal whose handler may have cancelation points while calling async-unsafe functions, or disables cancelation while
calling async-unsafe functions, all functions may be safely called from cancelation cleanup routines.</p>
</blockquote>
<h4><a name="tag_03_514_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_514_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="pthread_cancel.html"><i>pthread_cancel</i>()</a> , <a href=
"pthread_setcancelstate.html"><i>pthread_setcancelstate</i>()</a> , the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001,
<a href="../basedefs/pthread.h.html"><i>&lt;pthread.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_514_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 5. Included for alignment with the POSIX Threads Extension.</p>
</blockquote>
<h4><a name="tag_03_514_12"></a>Issue 6</h4>
<blockquote>
<p>The <i>pthread_cleanup_pop</i>() and <i>pthread_cleanup_push</i>() functions are marked as part of the Threads option.</p>
<p>The APPLICATION USAGE section is added.</p>
<p>The DESCRIPTION is updated to avoid use of the term &quot;must&quot; for application requirements.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; is a registered Trademark of The IEEE.<br>
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
]</font></center>
<!--footer end-->
<hr size="2" noshade>
</body>
</html>