glibc/include/allocate_once.h

96 lines
3.8 KiB
C
Raw Permalink Normal View History

/* Allocate and initialize an object once, in a thread-safe fashion.
Copyright (C) 2018-2022 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 07:40:42 +02:00
<https://www.gnu.org/licenses/>. */
#ifndef _ALLOCATE_ONCE_H
#define _ALLOCATE_ONCE_H
#include <atomic.h>
/* Slow path for allocate_once; see below. */
void *__libc_allocate_once_slow (void **__place,
void *(*__allocate) (void *__closure),
void (*__deallocate) (void *__closure,
void *__ptr),
void *__closure);
/* Return an a pointer to an allocated and initialized data structure.
If this function returns a non-NULL value, the caller can assume
that pointed-to data has been initialized according to the ALLOCATE
function.
It is expected that callers define an inline helper function which
adds type safety, like this.
struct foo { ... };
struct foo *global_foo;
static void *allocate_foo (void *closure);
static void *deallocate_foo (void *closure, void *ptr);
static inline struct foo *
get_foo (void)
{
return allocate_once (&global_foo, allocate_foo, free_foo, NULL);
}
(Note that the global_foo variable is initialized to zero.)
Usage of this helper function looks like this:
struct foo *local_foo = get_foo ();
if (local_foo == NULL)
report_allocation_failure ();
allocate_once first performs an acquire MO load on *PLACE. If the
result is not null, it is returned. Otherwise, ALLOCATE (CLOSURE)
is called, yielding a value RESULT. If RESULT equals NULL,
allocate_once returns NULL, and does not modify *PLACE (but another
thread may concurrently perform an allocation which succeeds,
updating *PLACE). If RESULT does not equal NULL, the function uses
a CAS with acquire-release MO to update the NULL value in *PLACE
with the RESULT value. If it turns out that *PLACE was updated
concurrently, allocate_once calls DEALLOCATE (CLOSURE, RESULT) to
undo the effect of ALLOCATE, and returns the new value of *PLACE
(after an acquire MO load). If DEALLOCATE is NULL, free (RESULT)
is called instead.
Compared to __libc_once, allocate_once has the advantage that it
does not need separate space for a control variable, and that it is
safe with regards to cancellation and other forms of exception
handling if the supplied callback functions are safe in that
regard. allocate_once passes a closure parameter to the allocation
function, too. */
static inline void *
allocate_once (void **__place, void *(*__allocate) (void *__closure),
void (*__deallocate) (void *__closure, void *__ptr),
void *__closure)
{
/* Synchronizes with the release MO CAS in
__allocate_once_slow. */
void *__result = atomic_load_acquire (__place);
if (__result != NULL)
return __result;
else
return __libc_allocate_once_slow (__place, __allocate, __deallocate,
__closure);
}
#ifndef _ISOMAC
libc_hidden_proto (__libc_allocate_once_slow)
#endif
#endif /* _ALLOCATE_ONCE_H */