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