]>
Commit | Line | Data |
---|---|---|
5f7b841d FW |
1 | /* Concurrent allocation and initialization of a pointer. |
2 | Copyright (C) 2018 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
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 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <allocate_once.h> | |
20 | #include <stdlib.h> | |
21 | #include <stdbool.h> | |
22 | ||
23 | void * | |
24 | __libc_allocate_once_slow (void **place, void *(*allocate) (void *closure), | |
25 | void (*deallocate) (void *closure, void *ptr), | |
26 | void *closure) | |
27 | { | |
28 | void *result = allocate (closure); | |
29 | if (result == NULL) | |
30 | return NULL; | |
31 | ||
32 | /* This loop implements a strong CAS on *place, with acquire-release | |
33 | MO semantics, from a weak CAS with relaxed-release MO. */ | |
34 | while (true) | |
35 | { | |
36 | /* Synchronizes with the acquire MO load in allocate_once. */ | |
37 | void *expected = NULL; | |
38 | if (atomic_compare_exchange_weak_release (place, &expected, result)) | |
39 | return result; | |
40 | ||
41 | /* The failed CAS has relaxed MO semantics, so perform another | |
42 | acquire MO load. */ | |
43 | void *other_result = atomic_load_acquire (place); | |
44 | if (other_result == NULL) | |
45 | /* Spurious failure. Try again. */ | |
46 | continue; | |
47 | ||
48 | /* We lost the race. Free what we allocated and return the | |
49 | other result. */ | |
50 | if (deallocate == NULL) | |
51 | free (result); | |
52 | else | |
53 | deallocate (closure, result); | |
54 | return other_result; | |
55 | } | |
56 | ||
57 | return result; | |
58 | } | |
59 | libc_hidden_def (__libc_allocate_once_slow) |