chiark / gitweb /
Apply https://sourceware.org/git/?p=glibc.git;a=commit;h=d5dd6189d506068ed11c8bfa1e1e...
[eglibc.git] / linuxthreads / ptcleanup.c
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 <sysdep.h>
22 #include <jmpbuf-unwind.h>
23
24 static inline uintptr_t
25 demangle_ptr (uintptr_t x)
26 {
27 #ifdef PTR_DEMANGLE
28   PTR_DEMANGLE (x);
29 #endif
30   return x;
31 }
32
33 void __pthread_cleanup_upto (__jmp_buf target, char *targetframe)
34 {
35   pthread_descr self = thread_self();
36   struct _pthread_cleanup_buffer * c;
37
38   for (c = THREAD_GETMEM(self, p_cleanup);
39        c != NULL && _JMPBUF_UNWINDS(target, c, demangle_ptr);
40        c = c->__prev)
41     {
42 #if _STACK_GROWS_DOWN
43       if ((char *) c <= targetframe)
44         {
45           c = NULL;
46           break;
47         }
48 #elif _STACK_GROWS_UP
49       if ((char *) c >= targetframe)
50         {
51           c = NULL;
52           break;
53         }
54 #else
55 # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
56 #endif
57       c->__routine(c->__arg);
58     }
59   THREAD_SETMEM(self, p_cleanup, c);
60   if (THREAD_GETMEM(self, p_in_sighandler)
61       && _JMPBUF_UNWINDS(target, THREAD_GETMEM(self, p_in_sighandler),
62                          demangle_ptr))
63     THREAD_SETMEM(self, p_in_sighandler, NULL);
64 }