allocate_once.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Allocate and initialize an object once, in a thread-safe fashion.
  2. Copyright (C) 2018-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _ALLOCATE_ONCE_H
  16. #define _ALLOCATE_ONCE_H
  17. #include <atomic.h>
  18. /* Slow path for allocate_once; see below. */
  19. void *__libc_allocate_once_slow (void **__place,
  20. void *(*__allocate) (void *__closure),
  21. void (*__deallocate) (void *__closure,
  22. void *__ptr),
  23. void *__closure);
  24. #ifndef _ISOMAC
  25. libc_hidden_proto (__libc_allocate_once_slow)
  26. #endif
  27. /* Return an a pointer to an allocated and initialized data structure.
  28. If this function returns a non-NULL value, the caller can assume
  29. that pointed-to data has been initialized according to the ALLOCATE
  30. function.
  31. It is expected that callers define an inline helper function which
  32. adds type safety, like this.
  33. struct foo { ... };
  34. struct foo *global_foo;
  35. static void *allocate_foo (void *closure);
  36. static void *deallocate_foo (void *closure, void *ptr);
  37. static inline struct foo *
  38. get_foo (void)
  39. {
  40. return allocate_once (&global_foo, allocate_foo, free_foo, NULL);
  41. }
  42. (Note that the global_foo variable is initialized to zero.)
  43. Usage of this helper function looks like this:
  44. struct foo *local_foo = get_foo ();
  45. if (local_foo == NULL)
  46. report_allocation_failure ();
  47. allocate_once first performs an acquire MO load on *PLACE. If the
  48. result is not null, it is returned. Otherwise, ALLOCATE (CLOSURE)
  49. is called, yielding a value RESULT. If RESULT equals NULL,
  50. allocate_once returns NULL, and does not modify *PLACE (but another
  51. thread may concurrently perform an allocation which succeeds,
  52. updating *PLACE). If RESULT does not equal NULL, the function uses
  53. a CAS with acquire-release MO to update the NULL value in *PLACE
  54. with the RESULT value. If it turns out that *PLACE was updated
  55. concurrently, allocate_once calls DEALLOCATE (CLOSURE, RESULT) to
  56. undo the effect of ALLOCATE, and returns the new value of *PLACE
  57. (after an acquire MO load). If DEALLOCATE is NULL, free (RESULT)
  58. is called instead.
  59. Compared to __libc_once, allocate_once has the advantage that it
  60. does not need separate space for a control variable, and that it is
  61. safe with regards to cancellation and other forms of exception
  62. handling if the supplied callback functions are safe in that
  63. regard. allocate_once passes a closure parameter to the allocation
  64. function, too. */
  65. static inline void *
  66. allocate_once (void **__place, void *(*__allocate) (void *__closure),
  67. void (*__deallocate) (void *__closure, void *__ptr),
  68. void *__closure)
  69. {
  70. /* Synchronizes with the release MO CAS in
  71. __allocate_once_slow. */
  72. void *__result = atomic_load_acquire (__place);
  73. if (__result != NULL)
  74. return __result;
  75. else
  76. return __libc_allocate_once_slow (__place, __allocate, __deallocate,
  77. __closure);
  78. }
  79. #endif /* _ALLOCATE_ONCE_H */