PTHREAD_CLEANUP_POP(3)     POSIX Programmer's Manual    PTHREAD_CLEANUP_POP(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.

NNAAMMEE
       pthread_cleanup_pop, pthread_cleanup_push -- establish cancellation
       handlers

SSYYNNOOPPSSIISS
       #include <pthread.h>

       void pthread_cleanup_pop(int _e_x_e_c_u_t_e);
       void pthread_cleanup_push(void (*_r_o_u_t_i_n_e)(void*), void *_a_r_g);

DDEESSCCRRIIPPTTIIOONN
       The _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_o_p() function shall remove the routine at the top
       of the calling thread's cancellation cleanup stack and optionally
       invoke it (if _e_x_e_c_u_t_e is non-zero).

       The _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_u_s_h() function shall push the specified
       cancellation cleanup handler _r_o_u_t_i_n_e onto the calling thread's
       cancellation cleanup stack. The cancellation cleanup handler shall be
       popped from the cancellation cleanup stack and invoked with the
       argument _a_r_g when:

        *  The thread exits (that is, calls _p_t_h_r_e_a_d___e_x_i_t()).

        *  The thread acts upon a cancellation request.

        *  The thread calls _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_o_p() with a non-zero _e_x_e_c_u_t_e
           argument.

       It is unspecified whether _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_u_s_h() and
       _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_o_p() 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
       _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_u_s_h() macro may be thought to expand to a token list
       whose first token is ''{{'' with _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_o_p() expanding to a
       token list whose last token is the corresponding ''}}'').

       The effect of calling _l_o_n_g_j_m_p() or _s_i_g_l_o_n_g_j_m_p() is undefined if there
       have been any calls to _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_u_s_h() or _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_o_p()
       made without the matching call since the jump buffer was filled. The
       effect of calling _l_o_n_g_j_m_p() or _s_i_g_l_o_n_g_j_m_p() from inside a cancellation
       cleanup handler is also undefined unless the jump buffer was also
       filled in the cancellation cleanup handler.

       The effect of the use of rreettuurrnn, bbrreeaakk, ccoonnttiinnuuee, and ggoottoo to
       prematurely leave a code block described by a pair of
       _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_u_s_h() and _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_o_p() functions calls is
       undefined.

RREETTUURRNN VVAALLUUEE
       The _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_u_s_h() and _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_o_p() functions shall
       not return a value.

EERRRROORRSS
       No errors are defined.

       These functions shall not return an error code of [[EEIINNTTRR]].

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
       The following is an example using thread primitives to implement a
       cancelable, writers-priority read-write lock:


           typedef struct {
               pthread_mutex_t lock;
               pthread_cond_t rcond,
                   wcond;
               int lock_count; /* < 0 .. Held by writer. */
                               /* > 0 .. Held by lock_count readers. */
                               /* = 0 .. Held by nobody. */
               int waiting_writers; /* Count of waiting writers. */
           } rwlock;

           void
           waiting_reader_cleanup(void *arg)
           {
               rwlock *l;

               l = (rwlock *) arg;
               pthread_mutex_unlock(&l->lock);
           }

           void
           lock_for_read(rwlock *l)
           {
               pthread_mutex_lock(&l->lock);
               pthread_cleanup_push(waiting_reader_cleanup, l);
               while ((l->lock_count < 0) || (l->waiting_writers != 0))
                   pthread_cond_wait(&l->rcond, &l->lock);
               l->lock_count++;
              /*
               * Note the pthread_cleanup_pop executes
               * waiting_reader_cleanup.
               */
               pthread_cleanup_pop(1);
           }

           void
           release_read_lock(rwlock *l)
           {
               pthread_mutex_lock(&l->lock);
               if (--l->lock_count == 0)
                   pthread_cond_signal(&l->wcond);
               pthread_mutex_unlock(&l->lock);
           }

           void
           waiting_writer_cleanup(void *arg)
           {
               rwlock *l;

               l = (rwlock *) arg;
               if ((--l->waiting_writers == 0) && (l->lock_count >= 0)) {
                  /*
                   * This only happens if we have been canceled. If the
                   * lock is not held by a writer, there may be readers who
                   * were blocked because waiting_writers was positive; they
                   * can now be unblocked.
                   */
                   pthread_cond_broadcast(&l->rcond);
               }
               pthread_mutex_unlock(&l->lock);
           }

           void
           lock_for_write(rwlock *l)
           {
               pthread_mutex_lock(&l->lock);
               l->waiting_writers++;
               pthread_cleanup_push(waiting_writer_cleanup, l);
               while (l->lock_count != 0)
                   pthread_cond_wait(&l->wcond, &l->lock);
               l->lock_count = -1;
              /*
               * Note the pthread_cleanup_pop executes
               * waiting_writer_cleanup.
               */
               pthread_cleanup_pop(1);
           }

           void
           release_write_lock(rwlock *l)
           {
               pthread_mutex_lock(&l->lock);
               l->lock_count = 0;
               if (l->waiting_writers == 0)
                   pthread_cond_broadcast(&l->rcond);
               else
                   pthread_cond_signal(&l->wcond);
               pthread_mutex_unlock(&l->lock);
           }

           /*
            * This function is called to initialize the read/write lock.
            */
           void
           initialize_rwlock(rwlock *l)
           {
               pthread_mutex_init(&l->lock, pthread_mutexattr_default);
               pthread_cond_init(&l->wcond, pthread_condattr_default);
               pthread_cond_init(&l->rcond, pthread_condattr_default);
               l->lock_count = 0;
               l->waiting_writers = 0;
           }

           reader_thread()
           {
               lock_for_read(&lock);
               pthread_cleanup_push(release_read_lock, &lock);
              /*
               * Thread has read lock.
               */
               pthread_cleanup_pop(1);
           }

           writer_thread()
           {
               lock_for_write(&lock);
               pthread_cleanup_push(release_write_lock, &lock);
              /*
               * Thread has write lock.
               */
           pthread_cleanup_pop(1);
           }

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The two routines that push and pop cancellation cleanup handlers,
       _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_u_s_h() and _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_o_p(), can be thought of as
       left and right-parentheses. They always need to be matched.

RRAATTIIOONNAALLEE
       The restriction that the two routines that push and pop cancellation
       cleanup handlers, _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_u_s_h() and _p_t_h_r_e_a_d___c_l_e_a_n_u_p___p_o_p(),
       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:


           #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, &__head); \
               __cleanup_handler.next = *__head; \
               *__head = &__cleanup_handler;

           #define pthread_cleanup_pop(ex) \
               *__head = __cleanup_handler.next; \
               if (ex) (*__cleanup_handler.rtn)(__cleanup_handler.arg); \
           }

       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.

       This volume of POSIX.1-2017 currently leaves unspecified the effect of
       calling _l_o_n_g_j_m_p() 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 _l_o_n_g_j_m_p() function has to call
       all cancellation cleanup handlers that have been pushed but not popped
       since the time _s_e_t_j_m_p() was called.

       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 _q_s_o_r_t() and that handler were to call _l_o_n_g_j_m_p() (which, in
       turn, did _n_o_t call the cancellation cleanup handlers) the helper
       threads created by the _q_s_o_r_t() 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.

       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.

       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.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _p_t_h_r_e_a_d___c_a_n_c_e_l(), _p_t_h_r_e_a_d___s_e_t_c_a_n_c_e_l_s_t_a_t_e()

       The Base Definitions volume of POSIX.1-2017, <<pptthhrreeaadd..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       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 .

       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 .



IEEE/The Open Group                  2017               PTHREAD_CLEANUP_POP(3)
