1 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <bits/atomic.h>
28 #ifndef atomic_compare_and_exchange_acq
29 # define atomic_compare_and_exchange_acq(mem, newval, oldval) \
30 ({ __typeof (__arch_compare_and_exchange_32_acq (mem, newval, oldval)) \
32 if (sizeof (*mem) == 1) \
33 __result = __arch_compare_and_exchange_8_acq (mem, newval, oldval); \
34 else if (sizeof (*mem) == 2) \
35 __result = __arch_compare_and_exchange_16_acq (mem, newval, oldval); \
36 else if (sizeof (*mem) == 4) \
37 __result = __arch_compare_and_exchange_32_acq (mem, newval, oldval); \
38 else if (sizeof (*mem) == 8) \
39 __result = __arch_compare_and_exchange_64_acq (mem, newval, oldval); \
46 #ifndef atomic_compare_and_exchange_rel
47 # define atomic_compare_and_exchange_rel(mem, oldval, newval) \
48 atomic_compare_and_exchange_acq (mem, oldval, newval)
52 #ifndef atomic_exchange_and_add
53 # define atomic_exchange_and_add(mem, value) \
54 ({ __typeof (*mem) __oldval; \
55 __typeof (mem) __memp = (mem); \
56 __typeof (*mem) __value = (value); \
59 __oldval = (*__memp); \
60 while (atomic_compare_and_exchange_acq (__memp, __oldval + __value, \
63 __oldval + __value; })
68 # define atomic_add(mem, value) (void) atomic_exchange_and_add (mem, value)
72 #ifndef atomic_increment
73 # define atomic_increment(mem) atomic_add (mem, 1)
77 #ifndef atomic_increment_and_test
78 # define atomic_increment_and_test(mem) \
79 (atomic_exchange_and_add (mem, 1) == 0)
83 #ifndef atomic_decrement
84 # define atomic_decrement(mem) atomic_add (mem, -1)
88 #ifndef atomic_decrement_and_test
89 # define atomic_decrement_and_test(mem) \
90 (atomic_exchange_and_add (mem, -1) == 0)
94 #ifndef atomic_add_negative
95 # define atomic_add_negative(mem, value) \
96 (atomic_exchange_and_add (mem, value) < 0)
100 #ifndef atomic_add_zero
101 # define atomic_add_zero(mem, value) \
102 (atomic_exchange_and_add (mem, value) == 0)
106 #ifndef atomic_bit_set
107 # define atomic_bit_set(mem, bit) \
108 (void) atomic_bit_test_set(mem, bit)
112 #ifndef atomic_bit_test_set
113 # define atomic_bit_test_set(mem, bit) \
114 ({ __typeof (*mem) __oldval; \
115 __typeof (mem) __memp = (mem); \
116 __typeof (*mem) __mask = (1 << (bit)); \
119 __oldval = (*__memp); \
120 while (atomic_compare_and_exchange_acq (__memp, \
121 __oldval | __mask, __oldval)); \
123 __oldval & __mask; })
127 #ifndef atomic_full_barrier
128 # define atomic_full_barrier() __asm ("" ::: "memory")
132 #ifndef atomic_read_barrier
133 # define atomic_read_barrier() atomic_full_barrier()
137 #ifndef atomic_write_barrier
138 # define atomic_write_barrier() atomic_full_barrier()
141 #endif /* atomic.h */