1 /* Concurrent allocation and initialization of a pointer.
2 Copyright (C) 2018-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <allocate_once.h>
24 __libc_allocate_once_slow (void **place
, void *(*allocate
) (void *closure
),
25 void (*deallocate
) (void *closure
, void *ptr
),
28 void *result
= allocate (closure
);
32 /* This loop implements a strong CAS on *place, with acquire-release
33 MO semantics, from a weak CAS with relaxed-release MO. */
36 /* Synchronizes with the acquire MO load in allocate_once. */
37 void *expected
= NULL
;
38 if (atomic_compare_exchange_weak_release (place
, &expected
, result
))
41 /* The failed CAS has relaxed MO semantics, so perform another
43 void *other_result
= atomic_load_acquire (place
);
44 if (other_result
== NULL
)
45 /* Spurious failure. Try again. */
48 /* We lost the race. Free what we allocated and return the
50 if (deallocate
== NULL
)
53 deallocate (closure
, result
);
59 libc_hidden_def (__libc_allocate_once_slow
)