ptcleanup.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #ifndef NO_PTR_DEMANGLE
  21. #include <jmpbuf-unwind.h>
  22. #define __JMPBUF_UNWINDS(a,b,c) _JMPBUF_UNWINDS(a,b,c)
  23. #else
  24. #define __JMPBUF_UNWINDS(a,b,c) _JMPBUF_UNWINDS(a,b)
  25. #endif
  26. #ifndef NO_PTR_DEMANGLE
  27. static __inline__ uintptr_t
  28. demangle_ptr (uintptr_t x)
  29. {
  30. #ifdef PTR_DEMANGLE
  31. PTR_DEMANGLE (x);
  32. #endif
  33. return x;
  34. }
  35. #else
  36. #define demangle_ptr(x) x
  37. #endif
  38. void __pthread_cleanup_upto (__jmp_buf target, char *targetframe)
  39. {
  40. pthread_descr self = thread_self();
  41. struct _pthread_cleanup_buffer * c;
  42. for (c = THREAD_GETMEM(self, p_cleanup);
  43. c != NULL && __JMPBUF_UNWINDS(target, c, demangle_ptr);
  44. c = c->__prev)
  45. {
  46. #if _STACK_GROWS_DOWN
  47. if ((char *) c <= targetframe)
  48. {
  49. c = NULL;
  50. break;
  51. }
  52. #elif _STACK_GROWS_UP
  53. if ((char *) c >= targetframe)
  54. {
  55. c = NULL;
  56. break;
  57. }
  58. #else
  59. # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
  60. #endif
  61. c->__routine(c->__arg);
  62. }
  63. THREAD_SETMEM(self, p_cleanup, c);
  64. if (THREAD_GETMEM(self, p_in_sighandler)
  65. && __JMPBUF_UNWINDS(target, THREAD_GETMEM(self, p_in_sighandler),
  66. demangle_ptr))
  67. THREAD_SETMEM(self, p_in_sighandler, NULL);
  68. }