ptlongjmp.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #include <jmpbuf-unwind.h>
  21. static void pthread_cleanup_upto(__jmp_buf target)
  22. {
  23. pthread_descr self = thread_self();
  24. struct _pthread_cleanup_buffer * c;
  25. char *currentframe = CURRENT_STACK_FRAME;
  26. for (c = THREAD_GETMEM(self, p_cleanup);
  27. c != NULL && _JMPBUF_UNWINDS(target, c);
  28. c = c->__prev)
  29. {
  30. #ifdef _STACK_GROWS_DOWN
  31. if ((char *) c <= currentframe)
  32. {
  33. c = NULL;
  34. break;
  35. }
  36. #elif defined _STACK_GROWS_UP
  37. if ((char *) c >= currentframe)
  38. {
  39. c = NULL;
  40. break;
  41. }
  42. #else
  43. # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
  44. #endif
  45. c->__routine(c->__arg);
  46. }
  47. THREAD_SETMEM(self, p_cleanup, c);
  48. if (THREAD_GETMEM(self, p_in_sighandler)
  49. && _JMPBUF_UNWINDS(target, THREAD_GETMEM(self, p_in_sighandler)))
  50. THREAD_SETMEM(self, p_in_sighandler, NULL);
  51. }
  52. void siglongjmp(sigjmp_buf env, int val)
  53. {
  54. pthread_cleanup_upto(env->__jmpbuf);
  55. __libc_siglongjmp(env, val);
  56. }
  57. void longjmp(jmp_buf env, int val)
  58. {
  59. pthread_cleanup_upto(env->__jmpbuf);
  60. __libc_longjmp(env, val);
  61. }