1 #include "stlport_prefix.h"
3 #if defined(__unix) && defined(__GNUC__)
6 # include <osreldate.h>
9 #if (defined(__FreeBSD__) && (__FreeBSD_version < 503001)) || defined(__sun)
10 /* Note: __cxa_finalize and __cxa_atexit present in libc in FreeBSD 5.3, but again absent in 6.0 */
16 /* __asm__ (".symver " "__cxa_finalize" "," "__cxa_finalize" "@" "STLPORT_5_0_0"); */
17 /* __asm__ (".symver " "__cxa_finalize" "," "__cxa_finalize" "@@" "STLPORT_5_0_0"); */
20 /* But we can use static mutexes here: I hope that performance issue isn't very
21 significant on unloading (for only few calls, ~10) - ptr */
24 #define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
25 ({ __typeof (mem) __gmemp = (mem); \
26 __typeof (*mem) __gnewval = (newval); \
28 *__gmemp == (oldval) ? (*__gmemp = __gnewval, 0) : 1; })
32 ef_free, /* `ef_free' MUST be zero! */
41 /* `flavour' should be of type of the `enum' above but since we need
42 this element in an atomic operation we have to use `long int'. */
47 void (*fn)(int status, void *arg);
51 void (*fn)(void *arg, int status);
58 struct exit_function_list
60 struct exit_function_list *next;
62 struct exit_function fns[32];
65 struct exit_function *__new_exitfn (void);
67 /* Register a function to be called by exit or when a shared library
68 is unloaded. This function is only called from code generated by
70 int __cxa_atexit(void (*func)(void *), void *arg, void *d)
72 struct exit_function *new = __new_exitfn ();
78 new->func.cxa.fn = (void (*) (void *, int)) func;
79 new->func.cxa.arg = arg;
80 new->func.cxa.dso_handle = d;
85 /* We change global data, so we need locking. */
87 static pthread_mutex_t lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
90 static pthread_mutex_t lock =
91 { PTHREAD_MUTEX_RECURSIVE /* PTHREAD_MUTEX_DEFAULT */, PTHREAD_PRIO_NONE, {NULL,NULL},
92 NULL, { NULL }, /* MUTEX_FLAGS_PRIVATE */ 0x1, 0, 0, 0, {NULL, NULL},
96 static pthread_mutex_t lock =
97 {{0, 0, 0, PTHREAD_MUTEX_RECURSIVE, _MUTEX_MAGIC}, {{{0}}}, 0};
101 static struct exit_function_list initial;
102 struct exit_function_list *__exit_funcs = &initial;
104 struct exit_function *__new_exitfn(void)
106 struct exit_function_list *l;
109 pthread_mutex_lock( &lock );
111 for (l = __exit_funcs; l != NULL; l = l->next) {
112 for (i = 0; i < l->idx; ++i)
113 if (l->fns[i].flavor == ef_free)
118 if (l->idx < sizeof (l->fns) / sizeof (l->fns[0])) {
125 l = (struct exit_function_list *)malloc( sizeof(struct exit_function_list) );
127 l->next = __exit_funcs;
135 /* Mark entry as used, but we don't know the flavor now. */
137 l->fns[i].flavor = ef_us;
139 pthread_mutex_unlock( &lock );
141 return l == NULL ? NULL : &l->fns[i];
144 /* If D is non-NULL, call all functions registered with `__cxa_atexit'
145 with the same dso handle. Otherwise, if D is NULL, call all of the
146 registered handlers. */
149 * Note, that original __cxa_finalize don't use lock, but use __exit_funcs
152 void __cxa_finalize(void *d)
154 struct exit_function_list *funcs;
156 pthread_mutex_lock( &lock );
157 for (funcs = __exit_funcs; funcs; funcs = funcs->next) {
158 struct exit_function *f;
160 for (f = &funcs->fns[funcs->idx - 1]; f >= &funcs->fns[0]; --f) {
161 if ( (d == NULL || d == f->func.cxa.dso_handle) && (f->flavor == ef_cxa) ) {
163 (*f->func.cxa.fn) (f->func.cxa.arg, 0);
168 /* Remove the registered fork handlers. We do not have to
169 unregister anything if the program is going to terminate anyway. */
170 #ifdef UNREGISTER_ATFORK
172 UNREGISTER_ATFORK (d);
174 pthread_mutex_unlock( &lock );
177 /* __asm__ (".symver " "__cxa_finalize" "," "__cxa_finalize" "@@" "STLPORT_5_0_0"); */
178 /* void __cxa_finalize(void *d) __attribute__ ((weak)); */