pt-cleanup.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <setjmp.h>
  17. #include <stdlib.h>
  18. #include "pthreadP.h"
  19. #include <jmpbuf-unwind.h>
  20. void
  21. __pthread_cleanup_upto (__jmp_buf target, char *targetframe)
  22. {
  23. struct pthread *self = THREAD_SELF;
  24. struct _pthread_cleanup_buffer *cbuf;
  25. /* Adjust all pointers used in comparisons, so that top of thread's
  26. stack is at the top of address space. Without that, things break
  27. if stack is allocated above the main stack. */
  28. uintptr_t adj = (uintptr_t) self->stackblock + self->stackblock_size;
  29. uintptr_t targetframe_adj = (uintptr_t) targetframe - adj;
  30. for (cbuf = THREAD_GETMEM (self, cleanup);
  31. cbuf != NULL && _JMPBUF_UNWINDS_ADJ (target, cbuf, adj);
  32. cbuf = cbuf->__prev)
  33. {
  34. #if _STACK_GROWS_DOWN
  35. if ((uintptr_t) cbuf - adj <= targetframe_adj)
  36. {
  37. cbuf = NULL;
  38. break;
  39. }
  40. #elif _STACK_GROWS_UP
  41. if ((uintptr_t) cbuf - adj >= targetframe_adj)
  42. {
  43. cbuf = NULL;
  44. break;
  45. }
  46. #else
  47. # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
  48. #endif
  49. /* Call the cleanup code. */
  50. cbuf->__routine (cbuf->__arg);
  51. }
  52. THREAD_SETMEM (self, cleanup, cbuf);
  53. }
  54. hidden_def (__pthread_cleanup_upto)