{"id":4866,"date":"2022-12-20T18:36:55","date_gmt":"2022-12-20T21:36:55","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/pthread_cleanup_pop-man3p\/"},"modified":"2022-12-20T18:36:55","modified_gmt":"2022-12-20T21:36:55","slug":"pthread_cleanup_pop-man3p","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/pthread_cleanup_pop-man3p\/","title":{"rendered":"PTHREAD_CLEANUP_POP (man3p)"},"content":{"rendered":"<h1 align=\"center\">PTHREAD_CLEANUP_POP<\/h1>\n<p> <a href=\"#PROLOG\">PROLOG<\/a><br \/> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#APPLICATION USAGE\">APPLICATION USAGE<\/a><br \/> <a href=\"#RATIONALE\">RATIONALE<\/a><br \/> <a href=\"#FUTURE DIRECTIONS\">FUTURE DIRECTIONS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>PROLOG <a name=\"PROLOG\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This manual page is part of the POSIX Programmer\u2019s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">pthread_cleanup_pop, pthread_cleanup_push \u2014 establish cancellation handlers<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">#include <pthread.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">void pthread_cleanup_pop(int <i>execute<\/i>); <br \/> void pthread_cleanup_push(void (*<i>routine<\/i>)(void*), void *<i>arg<\/i>);<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>pthread_cleanup_pop<\/i>() function shall remove the routine at the top of the calling thread\u2019s cancellation cleanup stack and optionally invoke it (if <i>execute<\/i> is non-zero).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>pthread_cleanup_push<\/i>() function shall push the specified cancellation cleanup handler <i>routine<\/i> onto the calling thread\u2019s cancellation cleanup stack. The cancellation cleanup handler shall be popped from the cancellation cleanup stack and invoked with the argument <i>arg<\/i> when:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"12%\"><\/td>\n<td width=\"2%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p>The thread exits (that is, calls <i>pthread_exit<\/i>()).<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"12%\"><\/td>\n<td width=\"2%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p>The thread acts upon a cancellation request.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"12%\"><\/td>\n<td width=\"2%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"83%\">\n<p>The thread calls <i>pthread_cleanup_pop<\/i>() with a non-zero <i>execute<\/i> argument.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">It is unspecified whether <i>pthread_cleanup_push<\/i>() and <i>pthread_cleanup_pop<\/i>() are macros or functions. If a macro definition is suppressed in order to access an actual function, or a program defines an external identifier with any of these names, the behavior is undefined. 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 <b>\u2019{\u2019<\/b> with <i>pthread_cleanup_pop<\/i>() expanding to a token list whose last token is the corresponding <b>\u2019}\u2019<\/b>).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The effect of calling <i>longjmp<\/i>() or <i>siglongjmp<\/i>() 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 <i>longjmp<\/i>() or <i>siglongjmp<\/i>() from inside a cancellation cleanup handler is also undefined unless the jump buffer was also filled in the cancellation cleanup handler.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The effect of the use of <b>return<\/b>, <b>break<\/b>, <b>continue<\/b>, and <b>goto<\/b> to prematurely leave a code block described by a pair of <i>pthread_cleanup_push<\/i>() and <i>pthread_cleanup_pop<\/i>() functions calls is undefined.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>pthread_cleanup_push<\/i>() and <i>pthread_cleanup_pop<\/i>() functions shall not return a value.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">No errors are defined.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">These functions shall not return an error code of <b>[EINTR]<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>The following sections are informative.<\/i><\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The following is an example using thread primitives to implement a cancelable, writers-priority read-write lock:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">typedef struct { <br \/> pthread_mutex_t lock; <br \/> pthread_cond_t rcond, <br \/> wcond; <br \/> int lock_count; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var < 0 .. Held by writer. bodies\/ usr\/ <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var > 0 .. Held by lock_count readers. bodies\/ usr\/ <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var = 0 .. Held by nobody. bodies\/ usr\/ <br \/> int waiting_writers; \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var Count of waiting writers. bodies\/ usr\/ <br \/> } rwlock;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">void <br \/> waiting_reader_cleanup(void *arg) <br \/> { <br \/> rwlock *l;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">l = (rwlock *) arg; <br \/> pthread_mutex_unlock(&#038;l->lock); <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">void <br \/> lock_for_read(rwlock *l) <br \/> { <br \/> pthread_mutex_lock(&#038;l->lock); <br \/> pthread_cleanup_push(waiting_reader_cleanup, l); <br \/> while ((l->lock_count < 0) || (l->waiting_writers != 0)) <br \/> pthread_cond_wait(&#038;l->rcond, &#038;l->lock); <br \/> l->lock_count++; <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr Note the pthread_cleanup_pop executes <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr waiting_reader_cleanup. <br \/> bodies\/ usr\/ <br \/> pthread_cleanup_pop(1); <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">void <br \/> release_read_lock(rwlock *l) <br \/> { <br \/> pthread_mutex_lock(&#038;l->lock); <br \/> if (&#8211;l->lock_count == 0) <br \/> pthread_cond_signal(&#038;l->wcond); <br \/> pthread_mutex_unlock(&#038;l->lock); <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">void <br \/> waiting_writer_cleanup(void *arg) <br \/> { <br \/> rwlock *l;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">l = (rwlock *) arg; <br \/> if ((&#8211;l->waiting_writers == 0) &#038;&#038; (l->lock_count >= 0)) { <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr This only happens if we have been canceled. If the <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr lock is not held by a writer, there may be readers who <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr were blocked because waiting_writers was positive; they <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr can now be unblocked. <br \/> bodies\/ usr\/ <br \/> pthread_cond_broadcast(&#038;l->rcond); <br \/> } <br \/> pthread_mutex_unlock(&#038;l->lock); <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">void <br \/> lock_for_write(rwlock *l) <br \/> { <br \/> pthread_mutex_lock(&#038;l->lock); <br \/> l->waiting_writers++; <br \/> pthread_cleanup_push(waiting_writer_cleanup, l); <br \/> while (l->lock_count != 0) <br \/> pthread_cond_wait(&#038;l->wcond, &#038;l->lock); <br \/> l->lock_count = -1; <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr Note the pthread_cleanup_pop executes <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr waiting_writer_cleanup. <br \/> bodies\/ usr\/ <br \/> pthread_cleanup_pop(1); <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">void <br \/> release_write_lock(rwlock *l) <br \/> { <br \/> pthread_mutex_lock(&#038;l->lock); <br \/> l->lock_count = 0; <br \/> if (l->waiting_writers == 0) <br \/> pthread_cond_broadcast(&#038;l->rcond); <br \/> else <br \/> pthread_cond_signal(&#038;l->wcond); <br \/> pthread_mutex_unlock(&#038;l->lock); <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">\/* <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr This function is called to initialize the read\/write lock. <br \/> bodies\/ usr\/ <br \/> void <br \/> initialize_rwlock(rwlock *l) <br \/> { <br \/> pthread_mutex_init(&#038;l->lock, pthread_mutexattr_default); <br \/> pthread_cond_init(&#038;l->wcond, pthread_condattr_default); <br \/> pthread_cond_init(&#038;l->rcond, pthread_condattr_default); <br \/> l->lock_count = 0; <br \/> l->waiting_writers = 0; <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">reader_thread() <br \/> { <br \/> lock_for_read(&#038;lock); <br \/> pthread_cleanup_push(release_read_lock, &#038;lock); <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr Thread has read lock. <br \/> bodies\/ usr\/ <br \/> pthread_cleanup_pop(1); <br \/> }<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">writer_thread() <br \/> { <br \/> lock_for_write(&#038;lock); <br \/> pthread_cleanup_push(release_write_lock, &#038;lock); <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var <br \/> bodies manpages.csv script_extrae_body.sh script.sh usr Thread has write lock. <br \/> bodies\/ usr\/ <br \/> pthread_cleanup_pop(1); <br \/> }<\/p>\n<h2>APPLICATION USAGE <a name=\"APPLICATION USAGE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The two routines that push and pop cancellation 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>\n<h2>RATIONALE <a name=\"RATIONALE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The restriction that the two routines that push and pop cancellation 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>\n<p style=\"margin-left:17%; margin-top: 1em\">#define pthread_cleanup_push(rtn,arg) {  <br \/> struct _pthread_handler_rec __cleanup_handler, **__head;  <br \/> __cleanup_handler.rtn = rtn;  <br \/> __cleanup_handler.arg = arg;  <br \/> (void) pthread_getspecific(_pthread_handler_key, &#038;__head);  <br \/> __cleanup_handler.next = *__head;  <br \/> *__head = &#038;__cleanup_handler;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">#define pthread_cleanup_pop(ex)  <br \/> *__head = __cleanup_handler.next;  <br \/> if (ex) (*__cleanup_handler.rtn)(__cleanup_handler.arg);  <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A more ambitious implementation of these routines might do even better by allowing the compiler to note that the cancellation cleanup handler is a constant and can be expanded inline.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This volume of POSIX.1-2017 currently leaves unspecified the effect of calling <i>longjmp<\/i>() 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 <i>longjmp<\/i>() function has to call all cancellation cleanup handlers that have been pushed but not popped since the time <i>setjmp<\/i>() was called.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 <i>qsort<\/i>() and that handler were to call <i>longjmp<\/i>() (which, in turn, did <i>not<\/i> call the cancellation cleanup handlers) the helper threads created by the <i>qsort<\/i>() 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>\n<p style=\"margin-left:11%; margin-top: 1em\">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>\n<p style=\"margin-left:11%; margin-top: 1em\">There is no notion of a cancellation cleanup-safe function. If an application has no cancellation points in its signal handlers, blocks any signal whose handler may have cancellation points while calling async-unsafe functions, or disables cancellation while calling async-unsafe functions, all functions may be safely called from cancellation cleanup routines.<\/p>\n<h2>FUTURE DIRECTIONS <a name=\"FUTURE DIRECTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>pthread_cancel<\/i>(), <i>pthread_setcancelstate<\/i>()<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The Base Definitions volume of POSIX.1-2017, <b><pthread.h><\/b><\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology &#8212; Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http:\/\/www.opengroup.org\/unix\/online.html .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https:\/\/www.kernel.org\/doc\/man-pages\/reporting_bugs.html .<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  pthread_cleanup_pop, pthread_cleanup_push \u2014 establish cancellation handlers <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3779,1],"tags":[1594,1761],"class_list":["post-4866","post","type-post","status-publish","format-standard","hentry","category-3pm-perl-llamadas-de-bibliotecas","category-sin-categoria","tag-man3p","tag-pthread_cleanup_pop"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=4866"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4866\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=4866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=4866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=4866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}