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 
15 /* Redefine siglongjmp and longjmp so that they interact correctly
16    with cleanup handlers */
17 
18 #include <setjmp.h>
19 #include "pthread.h"
20 #include "internals.h"
21 #include <bits/stackinfo.h>
22 #include <jmpbuf-unwind.h>
23 
pthread_cleanup_upto(__jmp_buf target)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 
30   for (c = THREAD_GETMEM(self, p_cleanup);
31        c != NULL && _JMPBUF_UNWINDS(target, c);
32        c = c->__prev)
33     {
34 #ifdef _STACK_GROWS_DOWN
35       if ((char *) c <= currentframe)
36 	{
37 	  c = NULL;
38 	  break;
39 	}
40 #elif defined _STACK_GROWS_UP
41       if ((char *) c >= currentframe)
42 	{
43 	  c = NULL;
44 	  break;
45 	}
46 #else
47 # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
48 #endif
49       c->__routine(c->__arg);
50     }
51   THREAD_SETMEM(self, p_cleanup, c);
52   if (THREAD_GETMEM(self, p_in_sighandler)
53       && _JMPBUF_UNWINDS(target, THREAD_GETMEM(self, p_in_sighandler)))
54     THREAD_SETMEM(self, p_in_sighandler, NULL);
55 }
56 
siglongjmp(sigjmp_buf env,int val)57 void siglongjmp(sigjmp_buf env, int val)
58 {
59   pthread_cleanup_upto(env->__jmpbuf);
60   __libc_siglongjmp(env, val);
61 }
62 
longjmp(jmp_buf env,int val)63 void longjmp(jmp_buf env, int val)
64 {
65   pthread_cleanup_upto(env->__jmpbuf);
66   __libc_longjmp(env, val);
67 }
68