/project/jni/stlport/src/cxa.c

https://github.com/aichunyu/FFPlayer · C · 182 lines · 116 code · 35 blank · 31 comment · 29 complexity · 2cf330a3e960c1d352da8bfa71976549 MD5 · raw file

  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)
  7. /* Note: __cxa_finalize and __cxa_atexit present in libc in FreeBSD 5.3, but again absent in 6.0 */
  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. static pthread_mutex_t lock =
  75. { PTHREAD_MUTEX_RECURSIVE /* PTHREAD_MUTEX_DEFAULT */, PTHREAD_PRIO_NONE, {NULL,NULL},
  76. NULL, { NULL }, /* MUTEX_FLAGS_PRIVATE */ 0x1, 0, 0, 0, {NULL, NULL},
  77. { 0, 0, 0, 0 } };
  78. #endif
  79. #ifdef __sun
  80. static pthread_mutex_t lock =
  81. {{0, 0, 0, PTHREAD_MUTEX_RECURSIVE, _MUTEX_MAGIC}, {{{0}}}, 0};
  82. #endif
  83. static struct exit_function_list initial;
  84. struct exit_function_list *__exit_funcs = &initial;
  85. struct exit_function *__new_exitfn(void)
  86. {
  87. struct exit_function_list *l;
  88. size_t i = 0;
  89. pthread_mutex_lock( &lock );
  90. for (l = __exit_funcs; l != NULL; l = l->next) {
  91. for (i = 0; i < l->idx; ++i)
  92. if (l->fns[i].flavor == ef_free)
  93. break;
  94. if ( i < l->idx )
  95. break;
  96. if (l->idx < sizeof (l->fns) / sizeof (l->fns[0])) {
  97. i = l->idx++;
  98. break;
  99. }
  100. }
  101. if (l == NULL) {
  102. l = (struct exit_function_list *)malloc( sizeof(struct exit_function_list) );
  103. if (l != NULL) {
  104. l->next = __exit_funcs;
  105. __exit_funcs = l;
  106. l->idx = 1;
  107. i = 0;
  108. }
  109. }
  110. /* Mark entry as used, but we don't know the flavor now. */
  111. if ( l != NULL )
  112. l->fns[i].flavor = ef_us;
  113. pthread_mutex_unlock( &lock );
  114. return l == NULL ? NULL : &l->fns[i];
  115. }
  116. /* If D is non-NULL, call all functions registered with `__cxa_atexit'
  117. with the same dso handle. Otherwise, if D is NULL, call all of the
  118. registered handlers. */
  119. /*
  120. * Note, that original __cxa_finalize don't use lock, but use __exit_funcs
  121. * i.e. global data.
  122. */
  123. void __cxa_finalize(void *d)
  124. {
  125. struct exit_function_list *funcs;
  126. pthread_mutex_lock( &lock );
  127. for (funcs = __exit_funcs; funcs; funcs = funcs->next) {
  128. struct exit_function *f;
  129. for (f = &funcs->fns[funcs->idx - 1]; f >= &funcs->fns[0]; --f) {
  130. if ( (d == NULL || d == f->func.cxa.dso_handle) && (f->flavor == ef_cxa) ) {
  131. f->flavor = ef_free;
  132. (*f->func.cxa.fn) (f->func.cxa.arg, 0);
  133. }
  134. }
  135. }
  136. /* Remove the registered fork handlers. We do not have to
  137. unregister anything if the program is going to terminate anyway. */
  138. #ifdef UNREGISTER_ATFORK
  139. if (d != NULL)
  140. UNREGISTER_ATFORK (d);
  141. #endif
  142. pthread_mutex_unlock( &lock );
  143. }
  144. /* __asm__ (".symver " "__cxa_finalize" "," "__cxa_finalize" "@@" "STLPORT_5_0_0"); */
  145. /* void __cxa_finalize(void *d) __attribute__ ((weak)); */
  146. #endif /* OS name */
  147. #endif /* __unix */