ptcleanup.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Linuxthreads - a simple clone()-based implementation of Posix */
  2. /* threads for Linux. */
  3. /* Copyright (C) 1998 Xavier Leroy (Xavier.Leroy@inria.fr) */
  4. /* */
  5. /* This program is free software; you can redistribute it and/or */
  6. /* modify it under the terms of the GNU Library General Public License */
  7. /* as published by the Free Software Foundation; either version 2 */
  8. /* of the License, or (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU Library General Public License for more details. */
  14. /* Redefine siglongjmp and longjmp so that they interact correctly
  15. with cleanup handlers */
  16. #define NO_PTR_DEMANGLE
  17. #include <setjmp.h>
  18. #include "pthread.h"
  19. #include "internals.h"
  20. #include <sysdep.h>
  21. #ifndef NO_PTR_DEMANGLE
  22. #include <jmpbuf-unwind.h>
  23. #define __JMPBUF_UNWINDS(a,b,c) _JMPBUF_UNWINDS(a,b,c)
  24. #else
  25. #define __JMPBUF_UNWINDS(a,b,c) _JMPBUF_UNWINDS(a,b)
  26. #endif
  27. #ifndef NO_PTR_DEMANGLE
  28. static inline uintptr_t
  29. demangle_ptr (uintptr_t x)
  30. {
  31. #ifdef PTR_DEMANGLE
  32. PTR_DEMANGLE (x);
  33. #endif
  34. return x;
  35. }
  36. #else
  37. #define demangle_ptr(x) x
  38. #endif
  39. void __pthread_cleanup_upto (__jmp_buf target, char *targetframe)
  40. {
  41. pthread_descr self = thread_self();
  42. struct _pthread_cleanup_buffer * c;
  43. for (c = THREAD_GETMEM(self, p_cleanup);
  44. c != NULL && __JMPBUF_UNWINDS(target, c, demangle_ptr);
  45. c = c->__prev)
  46. {
  47. #if _STACK_GROWS_DOWN
  48. if ((char *) c <= targetframe)
  49. {
  50. c = NULL;
  51. break;
  52. }
  53. #elif _STACK_GROWS_UP
  54. if ((char *) c >= targetframe)
  55. {
  56. c = NULL;
  57. break;
  58. }
  59. #else
  60. # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
  61. #endif
  62. c->__routine(c->__arg);
  63. }
  64. THREAD_SETMEM(self, p_cleanup, c);
  65. if (THREAD_GETMEM(self, p_in_sighandler)
  66. && __JMPBUF_UNWINDS(target, THREAD_GETMEM(self, p_in_sighandler),
  67. demangle_ptr))
  68. THREAD_SETMEM(self, p_in_sighandler, NULL);
  69. }