1/* atomic.c -- Support for atomic functions if not present.
2 Copyright (C) 2013-2023 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
8
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
16
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
20
21THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31POSSIBILITY OF SUCH DAMAGE. */
32
33#include "config.h"
34
35#include <sys/types.h>
36
37#include "backtrace.h"
38#include "backtrace-supported.h"
39#include "internal.h"
40
41/* This file holds implementations of the atomic functions that are
42 used if the host compiler has the sync functions but not the atomic
43 functions, as is true of versions of GCC before 4.7. */
44
45#if !defined (HAVE_ATOMIC_FUNCTIONS) && defined (HAVE_SYNC_FUNCTIONS)
46
47/* Do an atomic load of a pointer. */
48
49void *
50backtrace_atomic_load_pointer (void *arg)
51{
52 void **pp;
53 void *p;
54
55 pp = (void **) arg;
56 p = *pp;
57 while (!__sync_bool_compare_and_swap (pp, p, p))
58 p = *pp;
59 return p;
60}
61
62/* Do an atomic load of an int. */
63
64int
65backtrace_atomic_load_int (int *p)
66{
67 int i;
68
69 i = *p;
70 while (!__sync_bool_compare_and_swap (p, i, i))
71 i = *p;
72 return i;
73}
74
75/* Do an atomic store of a pointer. */
76
77void
78backtrace_atomic_store_pointer (void *arg, void *p)
79{
80 void **pp;
81 void *old;
82
83 pp = (void **) arg;
84 old = *pp;
85 while (!__sync_bool_compare_and_swap (pp, old, p))
86 old = *pp;
87}
88
89/* Do an atomic store of a size_t value. */
90
91void
92backtrace_atomic_store_size_t (size_t *p, size_t v)
93{
94 size_t old;
95
96 old = *p;
97 while (!__sync_bool_compare_and_swap (p, old, v))
98 old = *p;
99}
100
101/* Do an atomic store of a int value. */
102
103void
104backtrace_atomic_store_int (int *p, int v)
105{
106 size_t old;
107
108 old = *p;
109 while (!__sync_bool_compare_and_swap (p, old, v))
110 old = *p;
111}
112
113#endif
114

source code of libbacktrace/atomic.c