pthread.h

In the following fragment from pthread.h, notice that there is no
closing ‘}’ for the pthread_cleanup_push macro and no opening ‘{’ for
the pthread_cleanup_pop macro! I couldn’t imagine how some of the
library files, like thread_pool_thread.c, could have been compiled using
this include file — until I noticed that thread_pool_thread.c doesn’t
include pthread.h, suggesting that the libraries must have functions for
the push and pop functions. It’s easy enough for a user to fix
pthread.h, but it should probably be put on somebody’s “to do” list for
the next release…

Murf

#define pthread_cleanup_push(__func, __arg)
{
struct __cleanup_handler __cleanup_handler;
__cleanup_handler.__routine = (__func);
__cleanup_handler.__save = (__arg);
__cleanup_handler.__next = (struct __cleanup_handler
*)__tls()->cleanup;
__tls()->cleanup = (void *)&__cleanup_handler;

#define pthread_cleanup_pop(__ex)
__tls()->cleanup = (void *)__cleanup_handler.__next;
((__ex) ? __cleanup_handler.__routine(__cleanup_handler.__save)
: (void)0);
}

John A. Murphy <murf@perftech.com> wrote:

In the following fragment from pthread.h, notice that there is no
closing ‘}’ for the pthread_cleanup_push macro and no opening ‘{’ for
the pthread_cleanup_pop macro!

This is correct. These routines effectively create a scope for the
enclosing cleanup; pthread_cleanup_push/pop MUST be treated in pairs;
as is explained in the docs … “Because the pthread_cleanup_push()
function is implemented as a macro it must be paired with
pthread_cleanup_pop() within the same lexical scope”; also refer to
POSIX 1003.1 Rational B.18.2.3 which says “The two routines that push
and pop cancellation cleanup handlers … can be thought of as left
and right parentheses. They always need to be matched. The restriction
that they appear in the same lexical scope allows for efficient …”.

Ah, makes sense! But the fellow here who had the problem says the compiler
won’t buy it… I’ll ask him to take another look at his code. Thanks for
the quick answer!

Murf

John Garvey wrote:

John A. Murphy <> murf@perftech.com> > wrote:
In the following fragment from pthread.h, notice that there is no
closing ‘}’ for the pthread_cleanup_push macro and no opening ‘{’ for
the pthread_cleanup_pop macro!

This is correct. These routines effectively create a scope for the
enclosing cleanup; pthread_cleanup_push/pop MUST be treated in pairs;
as is explained in the docs … “Because the pthread_cleanup_push()
function is implemented as a macro it must be paired with
pthread_cleanup_pop() within the same lexical scope”; also refer to
POSIX 1003.1 Rational B.18.2.3 which says “The two routines that push
and pop cancellation cleanup handlers … can be thought of as left
and right parentheses. They always need to be matched. The restriction
that they appear in the same lexical scope allows for efficient …”.