/* atomic.c -- Support for atomic functions if not present.1Copyright (C) 2013-2021 Free Software Foundation, Inc.2Written by Ian Lance Taylor, Google.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are6met:78(1) Redistributions of source code must retain the above copyright9notice, this list of conditions and the following disclaimer.1011(2) Redistributions in binary form must reproduce the above copyright12notice, this list of conditions and the following disclaimer in13the documentation and/or other materials provided with the14distribution.1516(3) The name of the author may not be used to17endorse or promote products derived from this software without18specific prior written permission.1920THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR21IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,24INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES25(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR26SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,28STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING29IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE30POSSIBILITY OF SUCH DAMAGE. */3132#include "config.h"3334#include <sys/types.h>3536#include "backtrace.h"37#include "backtrace-supported.h"38#include "internal.h"3940/* This file holds implementations of the atomic functions that are41used if the host compiler has the sync functions but not the atomic42functions, as is true of versions of GCC before 4.7. */4344#if !defined (HAVE_ATOMIC_FUNCTIONS) && defined (HAVE_SYNC_FUNCTIONS)4546/* Do an atomic load of a pointer. */4748void *49backtrace_atomic_load_pointer (void *arg)50{51void **pp;52void *p;5354pp = (void **) arg;55p = *pp;56while (!__sync_bool_compare_and_swap (pp, p, p))57p = *pp;58return p;59}6061/* Do an atomic load of an int. */6263int64backtrace_atomic_load_int (int *p)65{66int i;6768i = *p;69while (!__sync_bool_compare_and_swap (p, i, i))70i = *p;71return i;72}7374/* Do an atomic store of a pointer. */7576void77backtrace_atomic_store_pointer (void *arg, void *p)78{79void **pp;80void *old;8182pp = (void **) arg;83old = *pp;84while (!__sync_bool_compare_and_swap (pp, old, p))85old = *pp;86}8788/* Do an atomic store of a size_t value. */8990void91backtrace_atomic_store_size_t (size_t *p, size_t v)92{93size_t old;9495old = *p;96while (!__sync_bool_compare_and_swap (p, old, v))97old = *p;98}99100/* Do an atomic store of a int value. */101102void103backtrace_atomic_store_int (int *p, int v)104{105size_t old;106107old = *p;108while (!__sync_bool_compare_and_swap (p, old, v))109old = *p;110}111112#endif113114115