cxa.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "stlport_prefix.h"
  2. #if defined(__unix) && defined(__GNUC__)
  3. #ifdef __FreeBSD__
  4. # include <osreldate.h>
  5. #endif
  6. #if (defined(__FreeBSD__) && (__FreeBSD_version < 503001)) || defined(__sun) || defined (__hpux)
  7. /* Note: __cxa_finalize and __cxa_atexit present in libc in FreeBSD 5.3 */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <pthread.h>
  11. /* __asm__ (".symver " "__cxa_finalize" "," "__cxa_finalize" "@" "STLPORT_5_0_0"); */
  12. /* __asm__ (".symver " "__cxa_finalize" "," "__cxa_finalize" "@@" "STLPORT_5_0_0"); */
  13. /* Not atomic! */
  14. /* But we can use static mutexes here: I hope that performance issue isn't very
  15. significant on unloading (for only few calls, ~10) - ptr */
  16. /*
  17. #define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
  18. ({ __typeof (mem) __gmemp = (mem); \
  19. __typeof (*mem) __gnewval = (newval); \
  20. \
  21. *__gmemp == (oldval) ? (*__gmemp = __gnewval, 0) : 1; })
  22. */
  23. enum {
  24. ef_free, /* `ef_free' MUST be zero! */
  25. ef_us,
  26. ef_on,
  27. ef_at,
  28. ef_cxa
  29. };
  30. struct exit_function
  31. {
  32. /* `flavour' should be of type of the `enum' above but since we need
  33. this element in an atomic operation we have to use `long int'. */
  34. long int flavor;
  35. union {
  36. void (*at)(void);
  37. struct {
  38. void (*fn)(int status, void *arg);
  39. void *arg;
  40. } on;
  41. struct {
  42. void (*fn)(void *arg, int status);
  43. void *arg;
  44. void *dso_handle;
  45. } cxa;
  46. } func;
  47. };
  48. struct exit_function_list
  49. {
  50. struct exit_function_list *next;
  51. size_t idx;
  52. struct exit_function fns[32];
  53. };
  54. struct exit_function *__new_exitfn (void);
  55. /* Register a function to be called by exit or when a shared library
  56. is unloaded. This function is only called from code generated by
  57. the C++ compiler. */
  58. int __cxa_atexit(void (*func)(void *), void *arg, void *d)
  59. {
  60. struct exit_function *new = __new_exitfn ();
  61. if ( new == NULL )
  62. return -1;
  63. new->flavor = ef_cxa;
  64. new->func.cxa.fn = (void (*) (void *, int)) func;
  65. new->func.cxa.arg = arg;
  66. new->func.cxa.dso_handle = d;
  67. return 0;
  68. }
  69. /* We change global data, so we need locking. */
  70. #ifdef __linux__
  71. static pthread_mutex_t lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  72. #endif
  73. /* #ifdef __FreeBSD__ */
  74. #if 0
  75. static pthread_mutex_t lock =
  76. { PTHREAD_MUTEX_RECURSIVE /* PTHREAD_MUTEX_DEFAULT */, PTHREAD_PRIO_NONE, {NULL,NULL},
  77. NULL, { NULL }, /* MUTEX_FLAGS_PRIVATE */ 0x1, 0, 0, 0, {NULL, NULL},
  78. { 0, 0, 0, 0 } };
  79. #endif
  80. #ifdef __sun
  81. static pthread_mutex_t lock =
  82. {{0, 0, 0, PTHREAD_MUTEX_RECURSIVE, _MUTEX_MAGIC}, {{{0}}}, 0};
  83. #endif
  84. #ifdef __hpux
  85. static pthread_mutex_t lock = PTHREAD_MUTEX_RECURSIVE_INITIALIZER_NP;
  86. # ifdef __ia64
  87. void *__dso_handle = (void *) &__dso_handle;
  88. # endif
  89. #endif
  90. static struct exit_function_list initial;
  91. struct exit_function_list *__exit_funcs = &initial;
  92. struct exit_function *__new_exitfn(void)
  93. {
  94. struct exit_function_list *l;
  95. size_t i = 0;
  96. #ifndef __FreeBSD__
  97. pthread_mutex_lock( &lock );
  98. #endif
  99. for (l = __exit_funcs; l != NULL; l = l->next) {
  100. for (i = 0; i < l->idx; ++i)
  101. if (l->fns[i].flavor == ef_free)
  102. break;
  103. if ( i < l->idx )
  104. break;
  105. if (l->idx < sizeof (l->fns) / sizeof (l->fns[0])) {
  106. i = l->idx++;
  107. break;
  108. }
  109. }
  110. if (l == NULL) {
  111. l = (struct exit_function_list *)malloc( sizeof(struct exit_function_list) );
  112. if (l != NULL) {
  113. l->next = __exit_funcs;
  114. __exit_funcs = l;
  115. l->idx = 1;
  116. i = 0;
  117. }
  118. }
  119. /* Mark entry as used, but we don't know the flavor now. */
  120. if ( l != NULL )
  121. l->fns[i].flavor = ef_us;
  122. #ifndef __FreeBSD__
  123. pthread_mutex_unlock( &lock );
  124. #endif
  125. return l == NULL ? NULL : &l->fns[i];
  126. }
  127. /* If D is non-NULL, call all functions registered with `__cxa_atexit'
  128. with the same dso handle. Otherwise, if D is NULL, call all of the
  129. registered handlers. */
  130. /*
  131. * Note, that original __cxa_finalize don't use lock, but use __exit_funcs
  132. * i.e. global data.
  133. */
  134. void __cxa_finalize(void *d)
  135. {
  136. struct exit_function_list *funcs;
  137. #ifndef __FreeBSD__
  138. pthread_mutex_lock( &lock );
  139. #endif
  140. for (funcs = __exit_funcs; funcs; funcs = funcs->next) {
  141. struct exit_function *f;
  142. for (f = &funcs->fns[funcs->idx - 1]; f >= &funcs->fns[0]; --f) {
  143. if ( (d == NULL || d == f->func.cxa.dso_handle) && (f->flavor == ef_cxa) ) {
  144. f->flavor = ef_free;
  145. (*f->func.cxa.fn) (f->func.cxa.arg, 0);
  146. }
  147. }
  148. }
  149. /* Remove the registered fork handlers. We do not have to
  150. unregister anything if the program is going to terminate anyway. */
  151. #ifdef UNREGISTER_ATFORK
  152. if (d != NULL)
  153. UNREGISTER_ATFORK (d);
  154. #endif
  155. #ifndef __FreeBSD__
  156. pthread_mutex_unlock( &lock );
  157. #endif
  158. }
  159. /* __asm__ (".symver " "__cxa_finalize" "," "__cxa_finalize" "@@" "STLPORT_5_0_0"); */
  160. /* void __cxa_finalize(void *d) __attribute__ ((weak)); */
  161. #endif /* OS name */
  162. #endif /* __unix */