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/
7 +#if defined(__DYNAMIC__) || defined (__BLOCKS__)
9 +#endif /* defined(__DYNAMIC__) */
11 #include "un-namespace.h"
15 +#endif /* __BLOCKS__ */
16 #include "libc_private.h"
18 #define ATEXIT_FN_EMPTY 0
19 #define ATEXIT_FN_STD 1
20 #define ATEXIT_FN_CXA 2
22 +#define ATEXIT_FN_BLK 3
23 +#endif /* __BLOCKS__ */
25 static pthread_mutex_t atexit_mutex = PTHREAD_MUTEX_INITIALIZER;
27 @@ -63,6 +72,9 @@ struct atexit {
29 void (*std_func)(void);
30 void (*cxa_func)(void *);
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 {
40 static struct atexit *__atexit; /* points to head of LIFO stack */
41 +static int new_registration;
44 * Register the function described by 'fptr' to be called at application
45 @@ -105,6 +118,7 @@ atexit_register(struct atexit_fn *fptr)
48 p->fns[p->ind++] = *fptr;
49 + new_registration = 1;
50 _MUTEX_UNLOCK(&atexit_mutex);
53 @@ -116,17 +130,50 @@ int
54 atexit(void (*func)(void))
57 + struct dl_info info;
60 fn.fn_type = ATEXIT_FN_STD;
61 - fn.fn_ptr.std_func = func;;
62 + fn.fn_ptr.std_func = func;
64 +#if defined(__DYNAMIC__)
65 + if ( dladdr(func, &info) )
66 + fn.fn_dso = info.dli_fbase;
69 +#else /* ! defined(__DYNAMIC__) */
71 +#endif /* defined(__DYNAMIC__) */
73 error = atexit_register(&fn);
79 +atexit_b(void (^block)(void))
81 + struct atexit_fn fn;
82 + struct dl_info info;
85 + fn.fn_type = ATEXIT_FN_BLK;
86 + fn.fn_ptr.block = Block_copy(block);
88 +#if defined(__DYNAMIC__)
89 + if ( dladdr(block, &info) )
90 + fn.fn_dso = info.dli_fbase;
93 +#else /* ! defined(__DYNAMIC__) */
95 +#endif /* defined(__DYNAMIC__) */
97 + error = atexit_register(&fn);
100 +#endif /* __BLOCKS__ */
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.
109 -__cxa_finalize(void *dso)
110 +__cxa_finalize(const void *dso)
116 _MUTEX_LOCK(&atexit_mutex);
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.
124 p->fns[n].fn_type = ATEXIT_FN_EMPTY;
125 + new_registration = 0;
126 _MUTEX_UNLOCK(&atexit_mutex);
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();
134 + else if (fn.fn_type == ATEXIT_FN_BLK)
136 +#endif /* __BLOCKS__ */
137 _MUTEX_LOCK(&atexit_mutex);
138 + if (new_registration)
142 _MUTEX_UNLOCK(&atexit_mutex);