ptlongjmp.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include <setjmp.h>
  17. #include "pthread.h"
  18. #include "internals.h"
  19. #include <bits/stackinfo.h>
  20. /* These functions are not declared anywhere since they shouldn't be
  21. used at another place but here. */
  22. extern __typeof(siglongjmp) __libc_siglongjmp attribute_noreturn;
  23. extern __typeof(longjmp) __libc_longjmp attribute_noreturn;
  24. static void pthread_cleanup_upto(__jmp_buf target)
  25. {
  26. pthread_descr self = thread_self();
  27. struct _pthread_cleanup_buffer * c;
  28. char *currentframe = CURRENT_STACK_FRAME;
  29. for (c = THREAD_GETMEM(self, p_cleanup);
  30. c != NULL && _JMPBUF_UNWINDS(target, c);
  31. c = c->__prev)
  32. {
  33. #ifdef _STACK_GROWS_DOWN
  34. if ((char *) c <= currentframe)
  35. {
  36. c = NULL;
  37. break;
  38. }
  39. #elif defined _STACK_GROWS_UP
  40. if ((char *) c >= currentframe)
  41. {
  42. c = NULL;
  43. break;
  44. }
  45. #else
  46. # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
  47. #endif
  48. c->__routine(c->__arg);
  49. }
  50. THREAD_SETMEM(self, p_cleanup, c);
  51. if (THREAD_GETMEM(self, p_in_sighandler)
  52. && _JMPBUF_UNWINDS(target, THREAD_GETMEM(self, p_in_sighandler)))
  53. THREAD_SETMEM(self, p_in_sighandler, NULL);
  54. }
  55. void attribute_noreturn siglongjmp(sigjmp_buf env, int val)
  56. {
  57. pthread_cleanup_upto(env->__jmpbuf);
  58. __libc_siglongjmp(env, val);
  59. }
  60. void attribute_noreturn longjmp(jmp_buf env, int val)
  61. {
  62. pthread_cleanup_upto(env->__jmpbuf);
  63. __libc_longjmp(env, val);
  64. }