cleanup handlers and longjmp

Momchil Velikov velco@fadata.bg
Mon Dec 11 01:07:00 GMT 2000


Hi there,

I'd appreciate if anyone could kindly explain the following comment
(and the relevant code):

/* Redefine siglongjmp and longjmp so that they interact correctly
   with cleanup handlers */

in linuxthreads/ptlongjmp.c.

What does it mean to "interact correctly" ? Both POSIX and SUS state
that:

"The effect of calling longjmp() or siglongjmp() is undefined if there
have been any calls to pthread_cleanup_push() or pthread_cleanup_pop()
made without the matching call since the jump buffer was filled."

IMHO, trying to run the cleanup handlers to the point, where setjmp
has been called, contradicts to the specifications, as this (or other)
behaviour is not left as implementation defined.

Moreover, it doesn't seem to work, the following proggie:

-- 8< -----------
#include <stdio.h>
#include <pthread.h>
#include <setjmp.h>
#include <stdlib.h>

jmp_buf buf;

void
handler (void *arg)
{
  printf ("%s\n", arg);
}

void
foo()
{
  printf ("foo\n");
  pthread_cleanup_push (handler, "handler2");
  longjmp (buf, 1);
  pthread_cleanup_pop (0);
}

int
main()
{
  if (setjmp (buf) == 0)
  {
    pthread_cleanup_push (handler, "handler1");
    foo ();
    pthread_cleanup_pop (0);
  }
  else
  {
    printf ("longjmp\n");
  }
  return 0;
}
--- 8< -----------

outputs:

  foo
  handler2
  longjmp


Regards,
-velco

PS. Debian GNU/Linux, glibc 2.1.3, gcc 2.95.2


More information about the Libc-alpha mailing list