Initial revision
[kopensolaris-gnu/glibc.git] / nptl / atomic.h
1 /* Copyright (C) 2002 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #ifndef _ATOMIC_H
21 #define _ATOMIC_H       1
22
23 #include <stdlib.h>
24
25 #include <bits/atomic.h>
26
27
28 #ifndef atomic_compare_and_exchange_acq
29 # define atomic_compare_and_exchange_acq(mem, newval, oldval) \
30   ({ __typeof (__arch_compare_and_exchange_8_acq (mem, newval, oldval))       \
31          __result;                                                            \
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);   \
40      else                                                                     \
41        abort ();                                                              \
42      __result; })
43 #endif
44
45
46 #ifndef atomic_compare_and_exchange_rel
47 # define atomic_compare_and_exchange_rel(mem, oldval, newval) \
48   compare_and_exchange_acq (mem, oldval, newval)
49 #endif
50
51
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);                                       \
57                                                                               \
58      do                                                                       \
59        __oldval = (*__memp);                                                  \
60      while (compare_and_exchange_acq (__memp, __oldval + __value, __oldval)); \
61                                                                               \
62      __value; })
63 #endif
64
65
66 #ifndef atomic_add
67 # define atomic_add(mem, value) (void) exchange_and_add (mem, value)
68 #endif
69
70
71 #ifndef atomic_increment
72 # define atomic_increment(mem) atomic_add (mem, 1)
73 #endif
74
75
76 #ifndef atomic_decrement
77 # define atomic_decrement(mem) atomic_add (mem, -1)
78 #endif
79
80
81 #ifndef atomic_set_bit
82 # define atomic_set_bit(mem, bit) \
83   (void) ({ __typeof (mem) __memp = (mem);                                    \
84             while (1)                                                         \
85               {                                                               \
86                 __typeof (*mem) __oldval = *__memp;                           \
87                                                                               \
88                 if (compare_and_exchange_acq (__memp, __oldval | 1 << bit,    \
89                                               __oldval) == 0)                 \
90                   break;                                                      \
91               }})
92 #endif
93
94
95 #ifndef atomic_full_barrier
96 # define full_barrier() __asm ("" ::: "memory")
97 #endif
98
99
100 #ifndef atomic_read_barrier
101 # define read_barrier() full_barrier()
102 #endif
103
104
105 #ifndef atomic_write_barrier
106 # define write_barrier() full_barrier()
107 #endif
108
109 #endif  /* atomic.h */