This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

cleanup handlers and longjmp


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]