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