(root)/
glibc-2.38/
sysdeps/
unix/
sysv/
linux/
loongarch/
atomic-machine.h
       1  /* Atomic operations.
       2     Copyright (C) 2022-2023 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     <https://www.gnu.org/licenses/>.  */
      18  
      19  #ifndef _LINUX_LOONGARCH_BITS_ATOMIC_H
      20  #define _LINUX_LOONGARCH_BITS_ATOMIC_H 1
      21  
      22  #define atomic_full_barrier() __sync_synchronize ()
      23  
      24  #define __HAVE_64B_ATOMICS (__loongarch_grlen >= 64)
      25  #define USE_ATOMIC_COMPILER_BUILTINS 1
      26  #define ATOMIC_EXCHANGE_USES_CAS 0
      27  
      28  /* Compare and exchange.
      29     For all "bool" routines, we return FALSE if exchange successful.  */
      30  
      31  #define __arch_compare_and_exchange_bool_8_int(mem, newval, oldval, model) \
      32    ({ \
      33      typeof (*mem) __oldval = (oldval); \
      34      !__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, model, \
      35  				  __ATOMIC_RELAXED); \
      36    })
      37  
      38  #define __arch_compare_and_exchange_bool_16_int(mem, newval, oldval, model) \
      39    ({ \
      40      typeof (*mem) __oldval = (oldval); \
      41      !__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, model, \
      42  				  __ATOMIC_RELAXED); \
      43    })
      44  
      45  #define __arch_compare_and_exchange_bool_32_int(mem, newval, oldval, model) \
      46    ({ \
      47      typeof (*mem) __oldval = (oldval); \
      48      !__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, model, \
      49  				  __ATOMIC_RELAXED); \
      50    })
      51  
      52  #define __arch_compare_and_exchange_bool_64_int(mem, newval, oldval, model) \
      53    ({ \
      54      typeof (*mem) __oldval = (oldval); \
      55      !__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, model, \
      56  				  __ATOMIC_RELAXED); \
      57    })
      58  
      59  #define __arch_compare_and_exchange_val_8_int(mem, newval, oldval, model) \
      60    ({ \
      61      typeof (*mem) __oldval = (oldval); \
      62      __atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, model, \
      63  				 __ATOMIC_RELAXED); \
      64      __oldval; \
      65    })
      66  
      67  #define __arch_compare_and_exchange_val_16_int(mem, newval, oldval, model) \
      68    ({ \
      69      typeof (*mem) __oldval = (oldval); \
      70      __atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, model, \
      71  				 __ATOMIC_RELAXED); \
      72      __oldval; \
      73    })
      74  
      75  #define __arch_compare_and_exchange_val_32_int(mem, newval, oldval, model) \
      76    ({ \
      77      typeof (*mem) __oldval = (oldval); \
      78      __atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, model, \
      79  				 __ATOMIC_RELAXED); \
      80      __oldval; \
      81    })
      82  
      83  #define __arch_compare_and_exchange_val_64_int(mem, newval, oldval, model) \
      84    ({ \
      85      typeof (*mem) __oldval = (oldval); \
      86      __atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, model, \
      87  				 __ATOMIC_RELAXED); \
      88      __oldval; \
      89    })
      90  
      91  /* Atomic compare and exchange.  */
      92  
      93  #define atomic_compare_and_exchange_bool_acq(mem, new, old) \
      94    __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, mem, new, old, \
      95  			__ATOMIC_ACQUIRE)
      96  
      97  #define atomic_compare_and_exchange_val_acq(mem, new, old) \
      98    __atomic_val_bysize (__arch_compare_and_exchange_val, int, mem, new, old, \
      99  		       __ATOMIC_ACQUIRE)
     100  
     101  #define atomic_compare_and_exchange_val_rel(mem, new, old) \
     102    __atomic_val_bysize (__arch_compare_and_exchange_val, int, mem, new, old, \
     103  		       __ATOMIC_RELEASE)
     104  
     105  /* Atomic exchange (without compare).  */
     106  
     107  #define __arch_exchange_8_int(mem, newval, model) \
     108    __atomic_exchange_n (mem, newval, model)
     109  
     110  #define __arch_exchange_16_int(mem, newval, model) \
     111    __atomic_exchange_n (mem, newval, model)
     112  
     113  #define __arch_exchange_32_int(mem, newval, model) \
     114    __atomic_exchange_n (mem, newval, model)
     115  
     116  #define __arch_exchange_64_int(mem, newval, model) \
     117    __atomic_exchange_n (mem, newval, model)
     118  
     119  #define atomic_exchange_acq(mem, value) \
     120    __atomic_val_bysize (__arch_exchange, int, mem, value, __ATOMIC_ACQUIRE)
     121  
     122  #define atomic_exchange_rel(mem, value) \
     123    __atomic_val_bysize (__arch_exchange, int, mem, value, __ATOMIC_RELEASE)
     124  
     125  /* Atomically add value and return the previous (unincremented) value.  */
     126  
     127  #define __arch_exchange_and_add_8_int(mem, value, model) \
     128    __atomic_fetch_add (mem, value, model)
     129  
     130  #define __arch_exchange_and_add_16_int(mem, value, model) \
     131    __atomic_fetch_add (mem, value, model)
     132  
     133  #define __arch_exchange_and_add_32_int(mem, value, model) \
     134    __atomic_fetch_add (mem, value, model)
     135  
     136  #define __arch_exchange_and_add_64_int(mem, value, model) \
     137    __atomic_fetch_add (mem, value, model)
     138  
     139  #define atomic_exchange_and_add_acq(mem, value) \
     140    __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
     141  		       __ATOMIC_ACQUIRE)
     142  
     143  #define atomic_exchange_and_add_rel(mem, value) \
     144    __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
     145  		       __ATOMIC_RELEASE)
     146  
     147  #endif /* bits/atomic.h */