sl@0: #include "stlport_prefix.h" sl@0: sl@0: #if defined(__unix) && defined(__GNUC__) sl@0: sl@0: #ifdef __FreeBSD__ sl@0: # include sl@0: #endif sl@0: sl@0: #if (defined(__FreeBSD__) && (__FreeBSD_version < 503001)) || defined(__sun) sl@0: /* Note: __cxa_finalize and __cxa_atexit present in libc in FreeBSD 5.3, but again absent in 6.0 */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: /* __asm__ (".symver " "__cxa_finalize" "," "__cxa_finalize" "@" "STLPORT_5_0_0"); */ sl@0: /* __asm__ (".symver " "__cxa_finalize" "," "__cxa_finalize" "@@" "STLPORT_5_0_0"); */ sl@0: sl@0: /* Not atomic! */ sl@0: /* But we can use static mutexes here: I hope that performance issue isn't very sl@0: significant on unloading (for only few calls, ~10) - ptr */ sl@0: sl@0: /* sl@0: #define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \ sl@0: ({ __typeof (mem) __gmemp = (mem); \ sl@0: __typeof (*mem) __gnewval = (newval); \ sl@0: \ sl@0: *__gmemp == (oldval) ? (*__gmemp = __gnewval, 0) : 1; }) sl@0: */ sl@0: sl@0: enum { sl@0: ef_free, /* `ef_free' MUST be zero! */ sl@0: ef_us, sl@0: ef_on, sl@0: ef_at, sl@0: ef_cxa sl@0: }; sl@0: sl@0: struct exit_function sl@0: { sl@0: /* `flavour' should be of type of the `enum' above but since we need sl@0: this element in an atomic operation we have to use `long int'. */ sl@0: long int flavor; sl@0: union { sl@0: void (*at)(void); sl@0: struct { sl@0: void (*fn)(int status, void *arg); sl@0: void *arg; sl@0: } on; sl@0: struct { sl@0: void (*fn)(void *arg, int status); sl@0: void *arg; sl@0: void *dso_handle; sl@0: } cxa; sl@0: } func; sl@0: }; sl@0: sl@0: struct exit_function_list sl@0: { sl@0: struct exit_function_list *next; sl@0: size_t idx; sl@0: struct exit_function fns[32]; sl@0: }; sl@0: sl@0: struct exit_function *__new_exitfn (void); sl@0: sl@0: /* Register a function to be called by exit or when a shared library sl@0: is unloaded. This function is only called from code generated by sl@0: the C++ compiler. */ sl@0: int __cxa_atexit(void (*func)(void *), void *arg, void *d) sl@0: { sl@0: struct exit_function *new = __new_exitfn (); sl@0: sl@0: if ( new == NULL ) sl@0: return -1; sl@0: sl@0: new->flavor = ef_cxa; sl@0: new->func.cxa.fn = (void (*) (void *, int)) func; sl@0: new->func.cxa.arg = arg; sl@0: new->func.cxa.dso_handle = d; sl@0: return 0; sl@0: } sl@0: sl@0: sl@0: /* We change global data, so we need locking. */ sl@0: #ifdef __linux__ sl@0: static pthread_mutex_t lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; sl@0: #endif sl@0: #ifdef __FreeBSD__ sl@0: static pthread_mutex_t lock = sl@0: { PTHREAD_MUTEX_RECURSIVE /* PTHREAD_MUTEX_DEFAULT */, PTHREAD_PRIO_NONE, {NULL,NULL}, sl@0: NULL, { NULL }, /* MUTEX_FLAGS_PRIVATE */ 0x1, 0, 0, 0, {NULL, NULL}, sl@0: { 0, 0, 0, 0 } }; sl@0: #endif sl@0: #ifdef __sun sl@0: static pthread_mutex_t lock = sl@0: {{0, 0, 0, PTHREAD_MUTEX_RECURSIVE, _MUTEX_MAGIC}, {{{0}}}, 0}; sl@0: #endif sl@0: sl@0: sl@0: static struct exit_function_list initial; sl@0: struct exit_function_list *__exit_funcs = &initial; sl@0: sl@0: struct exit_function *__new_exitfn(void) sl@0: { sl@0: struct exit_function_list *l; sl@0: size_t i = 0; sl@0: sl@0: pthread_mutex_lock( &lock ); sl@0: sl@0: for (l = __exit_funcs; l != NULL; l = l->next) { sl@0: for (i = 0; i < l->idx; ++i) sl@0: if (l->fns[i].flavor == ef_free) sl@0: break; sl@0: if ( i < l->idx ) sl@0: break; sl@0: sl@0: if (l->idx < sizeof (l->fns) / sizeof (l->fns[0])) { sl@0: i = l->idx++; sl@0: break; sl@0: } sl@0: } sl@0: sl@0: if (l == NULL) { sl@0: l = (struct exit_function_list *)malloc( sizeof(struct exit_function_list) ); sl@0: if (l != NULL) { sl@0: l->next = __exit_funcs; sl@0: __exit_funcs = l; sl@0: sl@0: l->idx = 1; sl@0: i = 0; sl@0: } sl@0: } sl@0: sl@0: /* Mark entry as used, but we don't know the flavor now. */ sl@0: if ( l != NULL ) sl@0: l->fns[i].flavor = ef_us; sl@0: sl@0: pthread_mutex_unlock( &lock ); sl@0: sl@0: return l == NULL ? NULL : &l->fns[i]; sl@0: } sl@0: sl@0: /* If D is non-NULL, call all functions registered with `__cxa_atexit' sl@0: with the same dso handle. Otherwise, if D is NULL, call all of the sl@0: registered handlers. */ sl@0: sl@0: /* sl@0: * Note, that original __cxa_finalize don't use lock, but use __exit_funcs sl@0: * i.e. global data. sl@0: */ sl@0: void __cxa_finalize(void *d) sl@0: { sl@0: struct exit_function_list *funcs; sl@0: sl@0: pthread_mutex_lock( &lock ); sl@0: for (funcs = __exit_funcs; funcs; funcs = funcs->next) { sl@0: struct exit_function *f; sl@0: sl@0: for (f = &funcs->fns[funcs->idx - 1]; f >= &funcs->fns[0]; --f) { sl@0: if ( (d == NULL || d == f->func.cxa.dso_handle) && (f->flavor == ef_cxa) ) { sl@0: f->flavor = ef_free; sl@0: (*f->func.cxa.fn) (f->func.cxa.arg, 0); sl@0: } sl@0: } sl@0: } sl@0: sl@0: /* Remove the registered fork handlers. We do not have to sl@0: unregister anything if the program is going to terminate anyway. */ sl@0: #ifdef UNREGISTER_ATFORK sl@0: if (d != NULL) sl@0: UNREGISTER_ATFORK (d); sl@0: #endif sl@0: pthread_mutex_unlock( &lock ); sl@0: } sl@0: sl@0: /* __asm__ (".symver " "__cxa_finalize" "," "__cxa_finalize" "@@" "STLPORT_5_0_0"); */ sl@0: /* void __cxa_finalize(void *d) __attribute__ ((weak)); */ sl@0: sl@0: #endif /* OS name */ sl@0: #endif /* __unix */ sl@0: