~drizzle-trunk/drizzle/development

910.5.2 by Monty Taylor
Applied atomic patch to current tree.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program 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
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
2234 by Brian Aker
Mass removal of ifdef/endif in favor of pragma once.
20
#pragma once
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
21
22
#define _KERNEL
23
#include <atomic.h>
24
#undef _KERNEL
25
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
26
inline bool __sync_fetch_and_add(volatile bool* ptr, bool val)
27
{
28
  bool ret= *ptr;
29
  (val == true) ? atomic_inc_8((volatile uint8_t *)ptr) : atomic_add_8((volatile uint8_t *)ptr, (int8_t)val);
30
  return ret;
31
}
32
 
33
inline int8_t __sync_fetch_and_add(volatile int8_t* ptr, int8_t val)
34
{
35
  int8_t ret= *ptr; 
36
  (val == 1) ? atomic_inc_8((volatile uint8_t*)ptr) : atomic_add_8((volatile uint8_t*)ptr, val);
37
  return ret;
38
}
39
40
inline int16_t __sync_fetch_and_add(volatile int16_t* ptr, int16_t val)
41
{
42
  int16_t ret= *ptr;
43
  (val == 1) ? atomic_inc_16((volatile uint16_t*)ptr) : atomic_add_16((volatile uint16_t*)ptr, val);
44
  return ret;
45
}
46
47
inline int32_t __sync_fetch_and_add(volatile int32_t* ptr, int32_t val)
48
{
49
  int32_t ret= *ptr;
50
  (val == 1) ? atomic_inc_32((volatile uint32_t*)ptr) : atomic_add_32((volatile uint32_t*)ptr, val);
51
  return ret;
52
}
53
54
inline uint8_t __sync_fetch_and_add(volatile uint8_t* ptr, uint8_t val)
55
{
56
  uint8_t ret= *ptr;
57
  (val == 1) ? atomic_inc_8(ptr) : atomic_add_8(ptr, (int8_t)val);
58
  return ret;
59
}
60
61
inline uint16_t __sync_fetch_and_add(volatile uint16_t* ptr, uint16_t val)
62
{
63
  uint16_t ret= *ptr;
64
  (val == 1) ? atomic_inc_16(ptr) : atomic_add_16(ptr, (int16_t)val);
65
  return ret;
66
}
67
68
inline uint32_t __sync_fetch_and_add(volatile uint32_t* ptr, uint32_t val)
69
{
70
  uint32_t ret= *ptr;
71
  (val == 1) ? atomic_inc_32(ptr) : atomic_add_32(ptr, (int32_t)val);
72
  return ret;
73
}
74
75
# if defined(_KERNEL) || defined(_INT64_TYPE)
76
inline uint64_t __sync_fetch_and_add(volatile uint64_t* ptr, uint64_t val)
77
{
78
  uint64_t ret= *ptr;
79
  (val == 1) ? atomic_inc_64(ptr) : atomic_add_64(ptr, (int64_t)val);
80
  return ret;
81
}
82
83
inline int64_t __sync_fetch_and_add(volatile int64_t* ptr, int64_t val)
84
{
85
  int64_t ret= *ptr;
86
  (val == 1) ? atomic_inc_64((volatile uint64_t*)ptr) : atomic_add_64((volatile uint64_t*)ptr, val);
87
  return ret;
88
}
89
# endif /* defined(_KERNEL) || defined(_INT64_TYPE) */
90
91
inline uint8_t __sync_fetch_and_sub(volatile uint8_t* ptr, uint8_t val)
92
{
93
  uint8_t ret= *ptr;
94
  (val == 1) ? atomic_dec_8(ptr) : atomic_add_8(ptr, 0-(int8_t)val);
95
  return ret;
96
}
97
98
inline uint16_t __sync_fetch_and_sub(volatile uint16_t* ptr, uint16_t val)
99
{
100
  uint16_t ret= *ptr;
101
  (val == 1) ? atomic_dec_16(ptr) : atomic_add_16(ptr, 0-(int16_t)val);
102
  return ret;
103
}
104
105
inline uint32_t __sync_fetch_and_sub(volatile uint32_t* ptr, uint32_t val)
106
{
107
  uint32_t ret= *ptr;
108
  (val == 1) ? atomic_dec_32(ptr) : atomic_add_32(ptr, 0-(int32_t)val);
109
  return ret;
110
}
111
112
# if defined(_KERNEL) || defined(_INT64_TYPE)
113
inline uint64_t __sync_fetch_and_sub(volatile uint64_t* ptr, uint64_t val)
114
{
115
  uint64_t ret= *ptr;
116
  (val == 1) ? atomic_dec_64(ptr) : atomic_add_64(ptr, 0-(int64_t)val);
117
  return ret;
118
}
119
inline int64_t __sync_fetch_and_sub(volatile int64_t* ptr, uint64_t val)
120
{
121
  int64_t ret= *ptr;
122
  (val == 1) ? atomic_dec_64((volatile uint64_t *) ptr) : atomic_add_64((volatile uint64_t *) ptr, 0-(int64_t)val);
123
  return ret;
124
}
125
# endif /* defined(_KERNEL) || defined(_INT64_TYPE) */
126
1039.5.16 by Jay Pipes
Fixes to Solaris atomics for atomic<bool> and signed integer pointers.
127
inline bool __sync_add_and_fetch(volatile bool* ptr, bool val)
128
{
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
129
  return (val == true) ? atomic_inc_8_nv((volatile uint8_t *)ptr) : atomic_add_8_nv((volatile uint8_t *)ptr, (int8_t)val);
1039.5.16 by Jay Pipes
Fixes to Solaris atomics for atomic<bool> and signed integer pointers.
130
}
131
 
132
inline int8_t __sync_add_and_fetch(volatile int8_t* ptr, int8_t val)
133
{
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
134
  return (val == 1) ? atomic_inc_8_nv((volatile uint8_t*)ptr) : atomic_add_8_nv((volatile uint8_t*)ptr, val);
1039.5.16 by Jay Pipes
Fixes to Solaris atomics for atomic<bool> and signed integer pointers.
135
}
136
137
inline int16_t __sync_add_and_fetch(volatile int16_t* ptr, int16_t val)
138
{
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
139
  return (val == 1) ? atomic_inc_16_nv((volatile uint16_t*)ptr) : atomic_add_16_nv((volatile uint16_t*)ptr, val);
1039.5.16 by Jay Pipes
Fixes to Solaris atomics for atomic<bool> and signed integer pointers.
140
}
141
142
inline int32_t __sync_add_and_fetch(volatile int32_t* ptr, int32_t val)
143
{
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
144
  return (val == 1) ? atomic_inc_32_nv((volatile uint32_t*)ptr) : atomic_add_32_nv((volatile uint32_t*)ptr, val);
1039.5.16 by Jay Pipes
Fixes to Solaris atomics for atomic<bool> and signed integer pointers.
145
}
146
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
147
inline uint8_t __sync_add_and_fetch(volatile uint8_t* ptr, uint8_t val)
148
{
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
149
  return (val == 1) ? atomic_inc_8_nv(ptr) : atomic_add_8_nv(ptr, (int8_t)val);
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
150
}
151
152
inline uint16_t __sync_add_and_fetch(volatile uint16_t* ptr, uint16_t val)
153
{
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
154
  return (val == 1) ? atomic_inc_16_nv(ptr) : atomic_add_16_nv(ptr, (int16_t)val);
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
155
}
156
157
inline uint32_t __sync_add_and_fetch(volatile uint32_t* ptr, uint32_t val)
158
{
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
159
  return (val == 1) ? atomic_inc_32_nv(ptr) : atomic_add_32_nv(ptr, (int32_t)val);
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
160
}
161
162
# if defined(_KERNEL) || defined(_INT64_TYPE)
163
inline uint64_t __sync_add_and_fetch(volatile uint64_t* ptr, uint64_t val)
164
{
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
165
  return (val == 1) ? atomic_inc_64_nv(ptr) : atomic_add_64_nv(ptr, (int64_t)val);
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
166
}
1039.5.16 by Jay Pipes
Fixes to Solaris atomics for atomic<bool> and signed integer pointers.
167
168
inline int64_t __sync_add_and_fetch(volatile int64_t* ptr, int64_t val)
169
{
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
170
  return (val == 1) ? atomic_inc_64_nv((volatile uint64_t*)ptr) : atomic_add_64_nv((volatile uint64_t*)ptr, val);
1039.5.16 by Jay Pipes
Fixes to Solaris atomics for atomic<bool> and signed integer pointers.
171
}
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
172
# endif /* defined(_KERNEL) || defined(_INT64_TYPE) */
173
174
inline uint8_t __sync_sub_and_fetch(volatile uint8_t* ptr, uint8_t val)
175
{
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
176
  return (val == 1) ? atomic_dec_8_nv(ptr) : atomic_add_8_nv(ptr, 0-(int8_t)val);
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
177
}
178
179
inline uint16_t __sync_sub_and_fetch(volatile uint16_t* ptr, uint16_t val)
180
{
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
181
  return (val == 1) ? atomic_dec_16_nv(ptr) : atomic_add_16_nv(ptr, 0-(int16_t)val);
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
182
}
183
184
inline uint32_t __sync_sub_and_fetch(volatile uint32_t* ptr, uint32_t val)
185
{
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
186
  return (val == 1) ? atomic_dec_32_nv(ptr) : atomic_add_32_nv(ptr, 0-(int32_t)val);
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
187
}
188
189
# if defined(_KERNEL) || defined(_INT64_TYPE)
190
inline uint64_t __sync_sub_and_fetch(volatile uint64_t* ptr, uint64_t val)
191
{
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
192
  return (val == 1) ? atomic_dec_64_nv(ptr) : atomic_add_64_nv(ptr, 0-(int64_t)val);
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
193
}
1039.5.16 by Jay Pipes
Fixes to Solaris atomics for atomic<bool> and signed integer pointers.
194
inline int64_t __sync_sub_and_fetch(volatile int64_t* ptr, uint64_t val)
195
{
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
196
  return (val == 1) ? atomic_dec_64_nv((volatile uint64_t *) ptr) : atomic_add_64_nv((volatile uint64_t *) ptr, 0-(int64_t)val);
1039.5.16 by Jay Pipes
Fixes to Solaris atomics for atomic<bool> and signed integer pointers.
197
}
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
198
# endif /* defined(_KERNEL) || defined(_INT64_TYPE) */
199
200
inline uint8_t __sync_lock_test_and_set(volatile uint8_t* ptr, uint8_t val)
201
{
202
  atomic_swap_8(ptr, val);
203
  return *ptr;
204
}
205
206
inline uint16_t __sync_lock_test_and_set(volatile uint16_t* ptr, uint16_t val)
207
{
208
  atomic_swap_16(ptr, val);
209
  return *ptr;
210
}
211
212
inline uint32_t __sync_lock_test_and_set(volatile uint32_t* ptr, uint32_t val)
213
{
214
  atomic_swap_32(ptr, val);
215
  return *ptr;
216
}
217
218
# if defined(_KERNEL) || defined(_INT64_TYPE)
219
inline uint64_t __sync_lock_test_and_set(volatile uint64_t* ptr, uint64_t val)
220
{
221
  atomic_swap_64(ptr, val);
222
  return *ptr;
223
}
224
#endif /* defined(_KERNEL) || defined(_INT64_TYPE) */
225
1039.5.16 by Jay Pipes
Fixes to Solaris atomics for atomic<bool> and signed integer pointers.
226
inline int8_t __sync_val_compare_and_swap(volatile int8_t* ptr,
227
                                           int8_t old_val, int8_t val)
228
{
229
  atomic_cas_8((volatile uint8_t *)ptr, old_val, val);
230
  return *ptr;
231
}
232
910.5.2 by Monty Taylor
Applied atomic patch to current tree.
233
inline uint8_t __sync_val_compare_and_swap(volatile uint8_t* ptr,
234
                                           uint8_t old_val, uint8_t val)
235
{
236
  atomic_cas_8(ptr, old_val, val);
237
  return *ptr;
238
}
239
240
inline uint16_t __sync_val_compare_and_swap(volatile uint16_t* ptr,
241
                                            uint16_t old_val, uint16_t val)
242
{
243
  atomic_cas_16(ptr, old_val, val);
244
  return *ptr;
245
}
246
247
inline uint32_t __sync_val_compare_and_swap(volatile uint32_t* ptr,
248
                                            uint32_t old_val, uint32_t val)
249
{
250
  atomic_cas_32(ptr, old_val, val);
251
  return *ptr;
252
}
253
254
# if defined(_KERNEL) || defined(_INT64_TYPE)
255
inline uint64_t __sync_val_compare_and_swap(volatile uint64_t* ptr,
256
                                            uint64_t old_val, uint64_t val)
257
{
258
  atomic_cas_64(ptr, old_val, val);
259
  return *ptr;
260
}
261
#endif /* defined(_KERNEL) || defined(_INT64_TYPE) */
262
1405.4.8 by Jay Pipes
Add Solaris atomics fixes and test files. Add replication.h header to makefile.
263
inline int8_t __sync_bool_compare_and_swap(volatile int8_t* ptr,
264
                                           int8_t old_val, int8_t val)
265
{
266
  int8_t orig= *ptr;
267
  return orig == atomic_cas_8((volatile uint8_t *)ptr, old_val, val);
268
}
269
270
inline uint8_t __sync_bool_compare_and_swap(volatile uint8_t* ptr,
271
                                           uint8_t old_val, uint8_t val)
272
{
273
  uint8_t orig= *ptr;
274
  return orig == atomic_cas_8(ptr, old_val, val);
275
}
276
277
inline uint16_t __sync_bool_compare_and_swap(volatile uint16_t* ptr,
278
                                            uint16_t old_val, uint16_t val)
279
{
280
  uint16_t orig= *ptr;
281
  return orig == atomic_cas_16(ptr, old_val, val);
282
}
283
284
inline uint32_t __sync_bool_compare_and_swap(volatile uint32_t* ptr,
285
                                            uint32_t old_val, uint32_t val)
286
{
287
  uint32_t orig= *ptr;
288
  return orig == atomic_cas_32(ptr, old_val, val);
289
}
290
291
# if defined(_KERNEL) || defined(_INT64_TYPE)
292
inline uint64_t __sync_bool_compare_and_swap(volatile uint64_t* ptr,
293
                                            uint64_t old_val, uint64_t val)
294
{
295
  uint64_t orig= *ptr;
296
  return orig == atomic_cas_64(ptr, old_val, val);
297
}
298
#endif /* defined(_KERNEL) || defined(_INT64_TYPE) */
299