]> git.saurik.com Git - apple/libc.git/blob - stdlib/FreeBSD/atexit.c.patch
a75ede0f0671a6965e251cd8beb58128124c4c4b
[apple/libc.git] / stdlib / FreeBSD / atexit.c.patch
1 --- atexit.c.bsdnew 2009-11-13 14:11:47.000000000 -0800
2 +++ atexit.c 2009-11-13 14:11:47.000000000 -0800
3 @@ -41,14 +41,23 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <pthread.h>
7 +#if defined(__DYNAMIC__) || defined (__BLOCKS__)
8 +#include <dlfcn.h>
9 +#endif /* defined(__DYNAMIC__) */
10 #include "atexit.h"
11 #include "un-namespace.h"
12
13 +#ifdef __BLOCKS__
14 +#include <Block.h>
15 +#endif /* __BLOCKS__ */
16 #include "libc_private.h"
17
18 #define ATEXIT_FN_EMPTY 0
19 #define ATEXIT_FN_STD 1
20 #define ATEXIT_FN_CXA 2
21 +#ifdef __BLOCKS__
22 +#define ATEXIT_FN_BLK 3
23 +#endif /* __BLOCKS__ */
24
25 static pthread_mutex_t atexit_mutex = PTHREAD_MUTEX_INITIALIZER;
26
27 @@ -63,6 +72,9 @@ struct atexit {
28 union {
29 void (*std_func)(void);
30 void (*cxa_func)(void *);
31 +#ifdef __BLOCKS__
32 + void (^block)(void);
33 +#endif /* __BLOCKS__ */
34 } fn_ptr; /* function pointer */
35 void *fn_arg; /* argument for CXA callback */
36 void *fn_dso; /* shared module handle */
37 @@ -70,6 +82,7 @@ struct atexit {
38 };
39
40 static struct atexit *__atexit; /* points to head of LIFO stack */
41 +static int new_registration;
42
43 /*
44 * Register the function described by 'fptr' to be called at application
45 @@ -105,6 +118,7 @@ atexit_register(struct atexit_fn *fptr)
46 __atexit = p;
47 }
48 p->fns[p->ind++] = *fptr;
49 + new_registration = 1;
50 _MUTEX_UNLOCK(&atexit_mutex);
51 return 0;
52 }
53 @@ -116,17 +130,50 @@ int
54 atexit(void (*func)(void))
55 {
56 struct atexit_fn fn;
57 + struct dl_info info;
58 int error;
59
60 fn.fn_type = ATEXIT_FN_STD;
61 - fn.fn_ptr.std_func = func;;
62 + fn.fn_ptr.std_func = func;
63 fn.fn_arg = NULL;
64 +#if defined(__DYNAMIC__)
65 + if ( dladdr(func, &info) )
66 + fn.fn_dso = info.dli_fbase;
67 + else
68 + fn.fn_dso = NULL;
69 +#else /* ! defined(__DYNAMIC__) */
70 fn.fn_dso = NULL;
71 +#endif /* defined(__DYNAMIC__) */
72
73 error = atexit_register(&fn);
74 return (error);
75 }
76
77 +#ifdef __BLOCKS__
78 +int
79 +atexit_b(void (^block)(void))
80 +{
81 + struct atexit_fn fn;
82 + struct dl_info info;
83 + int error;
84 +
85 + fn.fn_type = ATEXIT_FN_BLK;
86 + fn.fn_ptr.block = Block_copy(block);
87 + fn.fn_arg = NULL;
88 +#if defined(__DYNAMIC__)
89 + if ( dladdr(block, &info) )
90 + fn.fn_dso = info.dli_fbase;
91 + else
92 + fn.fn_dso = NULL;
93 +#else /* ! defined(__DYNAMIC__) */
94 + fn.fn_dso = NULL;
95 +#endif /* defined(__DYNAMIC__) */
96 +
97 + error = atexit_register(&fn);
98 + return (error);
99 +}
100 +#endif /* __BLOCKS__ */
101 +
102 /*
103 * Register a function to be performed at exit or when an shared object
104 * with given dso handle is unloaded dynamically.
105 @@ -152,13 +199,14 @@ __cxa_atexit(void (*func)(void *), void
106 * handlers are called.
107 */
108 void
109 -__cxa_finalize(void *dso)
110 +__cxa_finalize(const void *dso)
111 {
112 struct atexit *p;
113 struct atexit_fn fn;
114 int n;
115
116 _MUTEX_LOCK(&atexit_mutex);
117 +restart:
118 for (p = __atexit; p; p = p->next) {
119 for (n = p->ind; --n >= 0;) {
120 if (p->fns[n].fn_type == ATEXIT_FN_EMPTY)
121 @@ -171,6 +219,7 @@ __cxa_finalize(void *dso)
122 has already been called.
123 */
124 p->fns[n].fn_type = ATEXIT_FN_EMPTY;
125 + new_registration = 0;
126 _MUTEX_UNLOCK(&atexit_mutex);
127
128 /* Call the function of correct type. */
129 @@ -178,7 +227,13 @@ __cxa_finalize(void *dso)
130 fn.fn_ptr.cxa_func(fn.fn_arg);
131 else if (fn.fn_type == ATEXIT_FN_STD)
132 fn.fn_ptr.std_func();
133 +#ifdef __BLOCKS__
134 + else if (fn.fn_type == ATEXIT_FN_BLK)
135 + fn.fn_ptr.block();
136 +#endif /* __BLOCKS__ */
137 _MUTEX_LOCK(&atexit_mutex);
138 + if (new_registration)
139 + goto restart;
140 }
141 }
142 _MUTEX_UNLOCK(&atexit_mutex);