set-freeres.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Copyright (C) 1997-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <atomic.h>
  15. #include <stdlib.h>
  16. #include <set-hooks.h>
  17. #include <libc-internal.h>
  18. #include "../libio/libioP.h"
  19. DEFINE_HOOK (__libc_subfreeres, (void));
  20. symbol_set_define (__libc_freeres_ptrs);
  21. extern __attribute__ ((weak)) void __libdl_freeres (void);
  22. extern __attribute__ ((weak)) void __libpthread_freeres (void);
  23. void __libc_freeres_fn_section
  24. __libc_freeres (void)
  25. {
  26. /* This function might be called from different places. So better
  27. protect for multiple executions since these are fatal. */
  28. static long int already_called;
  29. if (!atomic_compare_and_exchange_bool_acq (&already_called, 1, 0))
  30. {
  31. void *const *p;
  32. _IO_cleanup ();
  33. /* We run the resource freeing after IO cleanup. */
  34. RUN_HOOK (__libc_subfreeres, ());
  35. /* Call the libdl list of cleanup functions
  36. (weak-ref-and-check). */
  37. if (&__libdl_freeres != NULL)
  38. __libdl_freeres ();
  39. /* Call the libpthread list of cleanup functions
  40. (weak-ref-and-check). */
  41. if (&__libpthread_freeres != NULL)
  42. __libpthread_freeres ();
  43. for (p = symbol_set_first_element (__libc_freeres_ptrs);
  44. !symbol_set_end_p (__libc_freeres_ptrs, p); ++p)
  45. free (*p);
  46. }
  47. }
  48. libc_hidden_def (__libc_freeres)