~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2009 Sun Microsystems, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#pragma once

#define _KERNEL
#include <atomic.h>
#undef _KERNEL

inline bool __sync_fetch_and_add(volatile bool* ptr, bool val)
{
  bool ret= *ptr;
  (val == true) ? atomic_inc_8((volatile uint8_t *)ptr) : atomic_add_8((volatile uint8_t *)ptr, (int8_t)val);
  return ret;
}
 
inline int8_t __sync_fetch_and_add(volatile int8_t* ptr, int8_t val)
{
  int8_t ret= *ptr; 
  (val == 1) ? atomic_inc_8((volatile uint8_t*)ptr) : atomic_add_8((volatile uint8_t*)ptr, val);
  return ret;
}

inline int16_t __sync_fetch_and_add(volatile int16_t* ptr, int16_t val)
{
  int16_t ret= *ptr;
  (val == 1) ? atomic_inc_16((volatile uint16_t*)ptr) : atomic_add_16((volatile uint16_t*)ptr, val);
  return ret;
}

inline int32_t __sync_fetch_and_add(volatile int32_t* ptr, int32_t val)
{
  int32_t ret= *ptr;
  (val == 1) ? atomic_inc_32((volatile uint32_t*)ptr) : atomic_add_32((volatile uint32_t*)ptr, val);
  return ret;
}

inline uint8_t __sync_fetch_and_add(volatile uint8_t* ptr, uint8_t val)
{
  uint8_t ret= *ptr;
  (val == 1) ? atomic_inc_8(ptr) : atomic_add_8(ptr, (int8_t)val);
  return ret;
}

inline uint16_t __sync_fetch_and_add(volatile uint16_t* ptr, uint16_t val)
{
  uint16_t ret= *ptr;
  (val == 1) ? atomic_inc_16(ptr) : atomic_add_16(ptr, (int16_t)val);
  return ret;
}

inline uint32_t __sync_fetch_and_add(volatile uint32_t* ptr, uint32_t val)
{
  uint32_t ret= *ptr;
  (val == 1) ? atomic_inc_32(ptr) : atomic_add_32(ptr, (int32_t)val);
  return ret;
}

# if defined(_KERNEL) || defined(_INT64_TYPE)
inline uint64_t __sync_fetch_and_add(volatile uint64_t* ptr, uint64_t val)
{
  uint64_t ret= *ptr;
  (val == 1) ? atomic_inc_64(ptr) : atomic_add_64(ptr, (int64_t)val);
  return ret;
}

inline int64_t __sync_fetch_and_add(volatile int64_t* ptr, int64_t val)
{
  int64_t ret= *ptr;
  (val == 1) ? atomic_inc_64((volatile uint64_t*)ptr) : atomic_add_64((volatile uint64_t*)ptr, val);
  return ret;
}
# endif /* defined(_KERNEL) || defined(_INT64_TYPE) */

inline uint8_t __sync_fetch_and_sub(volatile uint8_t* ptr, uint8_t val)
{
  uint8_t ret= *ptr;
  (val == 1) ? atomic_dec_8(ptr) : atomic_add_8(ptr, 0-(int8_t)val);
  return ret;
}

inline uint16_t __sync_fetch_and_sub(volatile uint16_t* ptr, uint16_t val)
{
  uint16_t ret= *ptr;
  (val == 1) ? atomic_dec_16(ptr) : atomic_add_16(ptr, 0-(int16_t)val);
  return ret;
}

inline uint32_t __sync_fetch_and_sub(volatile uint32_t* ptr, uint32_t val)
{
  uint32_t ret= *ptr;
  (val == 1) ? atomic_dec_32(ptr) : atomic_add_32(ptr, 0-(int32_t)val);
  return ret;
}

# if defined(_KERNEL) || defined(_INT64_TYPE)
inline uint64_t __sync_fetch_and_sub(volatile uint64_t* ptr, uint64_t val)
{
  uint64_t ret= *ptr;
  (val == 1) ? atomic_dec_64(ptr) : atomic_add_64(ptr, 0-(int64_t)val);
  return ret;
}
inline int64_t __sync_fetch_and_sub(volatile int64_t* ptr, uint64_t val)
{
  int64_t ret= *ptr;
  (val == 1) ? atomic_dec_64((volatile uint64_t *) ptr) : atomic_add_64((volatile uint64_t *) ptr, 0-(int64_t)val);
  return ret;
}
# endif /* defined(_KERNEL) || defined(_INT64_TYPE) */

inline bool __sync_add_and_fetch(volatile bool* ptr, bool val)
{
  return (val == true) ? atomic_inc_8_nv((volatile uint8_t *)ptr) : atomic_add_8_nv((volatile uint8_t *)ptr, (int8_t)val);
}
 
inline int8_t __sync_add_and_fetch(volatile int8_t* ptr, int8_t val)
{
  return (val == 1) ? atomic_inc_8_nv((volatile uint8_t*)ptr) : atomic_add_8_nv((volatile uint8_t*)ptr, val);
}

inline int16_t __sync_add_and_fetch(volatile int16_t* ptr, int16_t val)
{
  return (val == 1) ? atomic_inc_16_nv((volatile uint16_t*)ptr) : atomic_add_16_nv((volatile uint16_t*)ptr, val);
}

inline int32_t __sync_add_and_fetch(volatile int32_t* ptr, int32_t val)
{
  return (val == 1) ? atomic_inc_32_nv((volatile uint32_t*)ptr) : atomic_add_32_nv((volatile uint32_t*)ptr, val);
}

inline uint8_t __sync_add_and_fetch(volatile uint8_t* ptr, uint8_t val)
{
  return (val == 1) ? atomic_inc_8_nv(ptr) : atomic_add_8_nv(ptr, (int8_t)val);
}

inline uint16_t __sync_add_and_fetch(volatile uint16_t* ptr, uint16_t val)
{
  return (val == 1) ? atomic_inc_16_nv(ptr) : atomic_add_16_nv(ptr, (int16_t)val);
}

inline uint32_t __sync_add_and_fetch(volatile uint32_t* ptr, uint32_t val)
{
  return (val == 1) ? atomic_inc_32_nv(ptr) : atomic_add_32_nv(ptr, (int32_t)val);
}

# if defined(_KERNEL) || defined(_INT64_TYPE)
inline uint64_t __sync_add_and_fetch(volatile uint64_t* ptr, uint64_t val)
{
  return (val == 1) ? atomic_inc_64_nv(ptr) : atomic_add_64_nv(ptr, (int64_t)val);
}

inline int64_t __sync_add_and_fetch(volatile int64_t* ptr, int64_t val)
{
  return (val == 1) ? atomic_inc_64_nv((volatile uint64_t*)ptr) : atomic_add_64_nv((volatile uint64_t*)ptr, val);
}
# endif /* defined(_KERNEL) || defined(_INT64_TYPE) */

inline uint8_t __sync_sub_and_fetch(volatile uint8_t* ptr, uint8_t val)
{
  return (val == 1) ? atomic_dec_8_nv(ptr) : atomic_add_8_nv(ptr, 0-(int8_t)val);
}

inline uint16_t __sync_sub_and_fetch(volatile uint16_t* ptr, uint16_t val)
{
  return (val == 1) ? atomic_dec_16_nv(ptr) : atomic_add_16_nv(ptr, 0-(int16_t)val);
}

inline uint32_t __sync_sub_and_fetch(volatile uint32_t* ptr, uint32_t val)
{
  return (val == 1) ? atomic_dec_32_nv(ptr) : atomic_add_32_nv(ptr, 0-(int32_t)val);
}

# if defined(_KERNEL) || defined(_INT64_TYPE)
inline uint64_t __sync_sub_and_fetch(volatile uint64_t* ptr, uint64_t val)
{
  return (val == 1) ? atomic_dec_64_nv(ptr) : atomic_add_64_nv(ptr, 0-(int64_t)val);
}
inline int64_t __sync_sub_and_fetch(volatile int64_t* ptr, uint64_t val)
{
  return (val == 1) ? atomic_dec_64_nv((volatile uint64_t *) ptr) : atomic_add_64_nv((volatile uint64_t *) ptr, 0-(int64_t)val);
}
# endif /* defined(_KERNEL) || defined(_INT64_TYPE) */

inline uint8_t __sync_lock_test_and_set(volatile uint8_t* ptr, uint8_t val)
{
  atomic_swap_8(ptr, val);
  return *ptr;
}

inline uint16_t __sync_lock_test_and_set(volatile uint16_t* ptr, uint16_t val)
{
  atomic_swap_16(ptr, val);
  return *ptr;
}

inline uint32_t __sync_lock_test_and_set(volatile uint32_t* ptr, uint32_t val)
{
  atomic_swap_32(ptr, val);
  return *ptr;
}

# if defined(_KERNEL) || defined(_INT64_TYPE)
inline uint64_t __sync_lock_test_and_set(volatile uint64_t* ptr, uint64_t val)
{
  atomic_swap_64(ptr, val);
  return *ptr;
}
#endif /* defined(_KERNEL) || defined(_INT64_TYPE) */

inline int8_t __sync_val_compare_and_swap(volatile int8_t* ptr,
                                           int8_t old_val, int8_t val)
{
  atomic_cas_8((volatile uint8_t *)ptr, old_val, val);
  return *ptr;
}

inline uint8_t __sync_val_compare_and_swap(volatile uint8_t* ptr,
                                           uint8_t old_val, uint8_t val)
{
  atomic_cas_8(ptr, old_val, val);
  return *ptr;
}

inline uint16_t __sync_val_compare_and_swap(volatile uint16_t* ptr,
                                            uint16_t old_val, uint16_t val)
{
  atomic_cas_16(ptr, old_val, val);
  return *ptr;
}

inline uint32_t __sync_val_compare_and_swap(volatile uint32_t* ptr,
                                            uint32_t old_val, uint32_t val)
{
  atomic_cas_32(ptr, old_val, val);
  return *ptr;
}

# if defined(_KERNEL) || defined(_INT64_TYPE)
inline uint64_t __sync_val_compare_and_swap(volatile uint64_t* ptr,
                                            uint64_t old_val, uint64_t val)
{
  atomic_cas_64(ptr, old_val, val);
  return *ptr;
}
#endif /* defined(_KERNEL) || defined(_INT64_TYPE) */

inline int8_t __sync_bool_compare_and_swap(volatile int8_t* ptr,
                                           int8_t old_val, int8_t val)
{
  int8_t orig= *ptr;
  return orig == atomic_cas_8((volatile uint8_t *)ptr, old_val, val);
}

inline uint8_t __sync_bool_compare_and_swap(volatile uint8_t* ptr,
                                           uint8_t old_val, uint8_t val)
{
  uint8_t orig= *ptr;
  return orig == atomic_cas_8(ptr, old_val, val);
}

inline uint16_t __sync_bool_compare_and_swap(volatile uint16_t* ptr,
                                            uint16_t old_val, uint16_t val)
{
  uint16_t orig= *ptr;
  return orig == atomic_cas_16(ptr, old_val, val);
}

inline uint32_t __sync_bool_compare_and_swap(volatile uint32_t* ptr,
                                            uint32_t old_val, uint32_t val)
{
  uint32_t orig= *ptr;
  return orig == atomic_cas_32(ptr, old_val, val);
}

# if defined(_KERNEL) || defined(_INT64_TYPE)
inline uint64_t __sync_bool_compare_and_swap(volatile uint64_t* ptr,
                                            uint64_t old_val, uint64_t val)
{
  uint64_t orig= *ptr;
  return orig == atomic_cas_64(ptr, old_val, val);
}
#endif /* defined(_KERNEL) || defined(_INT64_TYPE) */