Back to index

glibc  2.9
atomic.h
Go to the documentation of this file.
00001 /* Copyright (C) 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <stdint.h>
00020 #include <ia64intrin.h>
00021 
00022 typedef int8_t atomic8_t;
00023 typedef uint8_t uatomic8_t;
00024 typedef int_fast8_t atomic_fast8_t;
00025 typedef uint_fast8_t uatomic_fast8_t;
00026 
00027 typedef int16_t atomic16_t;
00028 typedef uint16_t uatomic16_t;
00029 typedef int_fast16_t atomic_fast16_t;
00030 typedef uint_fast16_t uatomic_fast16_t;
00031 
00032 typedef int32_t atomic32_t;
00033 typedef uint32_t uatomic32_t;
00034 typedef int_fast32_t atomic_fast32_t;
00035 typedef uint_fast32_t uatomic_fast32_t;
00036 
00037 typedef int64_t atomic64_t;
00038 typedef uint64_t uatomic64_t;
00039 typedef int_fast64_t atomic_fast64_t;
00040 typedef uint_fast64_t uatomic_fast64_t;
00041 
00042 typedef intptr_t atomicptr_t;
00043 typedef uintptr_t uatomicptr_t;
00044 typedef intmax_t atomic_max_t;
00045 typedef uintmax_t uatomic_max_t;
00046 
00047 
00048 #define __arch_compare_and_exchange_bool_8_acq(mem, newval, oldval) \
00049   (abort (), 0)
00050 
00051 #define __arch_compare_and_exchange_bool_16_acq(mem, newval, oldval) \
00052   (abort (), 0)
00053 
00054 #define __arch_compare_and_exchange_bool_32_acq(mem, newval, oldval) \
00055   (!__sync_bool_compare_and_swap ((mem), (int) (long) (oldval), \
00056                               (int) (long) (newval)))
00057 
00058 #define __arch_compare_and_exchange_bool_64_acq(mem, newval, oldval) \
00059   (!__sync_bool_compare_and_swap ((mem), (long) (oldval), \
00060                               (long) (newval)))
00061 
00062 #define __arch_compare_and_exchange_val_8_acq(mem, newval, oldval) \
00063   (abort (), (__typeof (*mem)) 0)
00064 
00065 #define __arch_compare_and_exchange_val_16_acq(mem, newval, oldval) \
00066   (abort (), (__typeof (*mem)) 0)
00067 
00068 #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
00069   __sync_val_compare_and_swap ((mem), (int) (long) (oldval), \
00070                             (int) (long) (newval))
00071 
00072 #define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
00073   __sync_val_compare_and_swap ((mem), (long) (oldval), (long) (newval))
00074 
00075 /* Atomically store newval and return the old value.  */
00076 #define atomic_exchange_acq(mem, value) \
00077   __sync_lock_test_and_set (mem, value)
00078 
00079 #define atomic_exchange_rel(mem, value) \
00080   (__sync_synchronize (), __sync_lock_test_and_set (mem, value))
00081 
00082 #define atomic_exchange_and_add(mem, value) \
00083   ({ __typeof (*mem) __result;                                              \
00084      __result = __sync_fetch_and_add ((mem), (int) (value));                \
00085      __result; })
00086 
00087 #define atomic_decrement_if_positive(mem) \
00088   ({ __typeof (*mem) __oldval, __val;                                       \
00089      __typeof (mem) __memp = (mem);                                         \
00090                                                                      \
00091      __val = (*__memp);                                                     \
00092      do                                                                     \
00093        {                                                             \
00094         __oldval = __val;                                            \
00095         if (__builtin_expect (__val <= 0, 0))                               \
00096           break;                                                     \
00097         __val = atomic_compare_and_exchange_val_acq (__memp,   __oldval - 1, \
00098                                                 __oldval);           \
00099        }                                                             \
00100      while (__builtin_expect (__val != __oldval, 0));                       \
00101      __oldval; })
00102 
00103 #define atomic_bit_test_set(mem, bit) \
00104   ({ __typeof (*mem) __oldval, __val;                                       \
00105      __typeof (mem) __memp = (mem);                                         \
00106      __typeof (*mem) __mask = ((__typeof (*mem)) 1 << (bit));               \
00107                                                                      \
00108      __val = (*__memp);                                                     \
00109      do                                                                     \
00110        {                                                             \
00111         __oldval = __val;                                            \
00112         __val = atomic_compare_and_exchange_val_acq (__memp,                \
00113                                                 __oldval | __mask,      \
00114                                                 __oldval);           \
00115        }                                                             \
00116      while (__builtin_expect (__val != __oldval, 0));                       \
00117      __oldval & __mask; })
00118 
00119 #define atomic_full_barrier() __sync_synchronize ()