osAtomic.c 28.0 KB
Newer Older
S
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * 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.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

wafwerar's avatar
wafwerar 已提交
16
#define ALLOW_FORBID_FUNC
S
slguan 已提交
17
#include "os.h"
S
Shengliang Guan 已提交
18

wafwerar's avatar
wafwerar 已提交
19
#ifdef WINDOWS
S
slguan 已提交
20 21

// add
wafwerar's avatar
wafwerar 已提交
22
int8_t interlocked_add_fetch_8(int8_t volatile* ptr, int8_t val) {
23
  return _InterlockedExchangeAdd8(ptr, val) + val;
S
slguan 已提交
24 25
}

wafwerar's avatar
wafwerar 已提交
26
int16_t interlocked_add_fetch_16(int16_t volatile* ptr, int16_t val) {
27
  return _InterlockedExchangeAdd16(ptr, val) + val;
S
slguan 已提交
28 29
}

wafwerar's avatar
wafwerar 已提交
30
int32_t interlocked_add_fetch_32(int32_t volatile* ptr, int32_t val) {
S
slguan 已提交
31 32 33
  return _InterlockedExchangeAdd(ptr, val) + val;
}

wafwerar's avatar
wafwerar 已提交
34
int64_t interlocked_add_fetch_64(int64_t volatile* ptr, int64_t val) {
35
  return InterlockedExchangeAdd64(ptr, val) + val;
S
slguan 已提交
36 37
}

wafwerar's avatar
wafwerar 已提交
38
void* interlocked_add_fetch_ptr(void* volatile* ptr, void*  val) {
39
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
40 41 42 43 44 45 46
  return (void*)(_InterlockedExchangeAdd((int32_t volatile*)(ptr), (int32_t)val) + (int32_t)val);
#else
  return (void*)(InterlockedExchangeAdd64((int64_t volatile*)(ptr), (int64_t)val) + (int64_t)val);
#endif
}

int8_t interlocked_and_fetch_8(int8_t volatile* ptr, int8_t val) {
S
slguan 已提交
47 48 49
  return _InterlockedAnd8(ptr, val) & val;
}

wafwerar's avatar
wafwerar 已提交
50
int16_t interlocked_and_fetch_16(int16_t volatile* ptr, int16_t val) {
S
slguan 已提交
51 52 53
  return _InterlockedAnd16(ptr, val) & val;
}

wafwerar's avatar
wafwerar 已提交
54
int32_t interlocked_and_fetch_32(int32_t volatile* ptr, int32_t val) {
S
slguan 已提交
55 56 57
  return _InterlockedAnd(ptr, val) & val;
}

wafwerar's avatar
wafwerar 已提交
58
int64_t interlocked_and_fetch_64(int64_t volatile* ptr, int64_t val) {
59
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
60
  int64_t old, res;
S
slguan 已提交
61 62 63
  do {
    old = *ptr;
    res = old & val;
64
  } while (_InterlockedCompareExchange64(ptr, res, old) != old);
S
slguan 已提交
65
  return res;
wafwerar's avatar
wafwerar 已提交
66 67
#else
  return _InterlockedAnd64(ptr, val) & val;
68
#endif
S
slguan 已提交
69 70
}

wafwerar's avatar
wafwerar 已提交
71
void* interlocked_and_fetch_ptr(void* volatile* ptr, void* val) {
72
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
73 74 75 76 77 78 79
  return (void*)interlocked_and_fetch_32((int32_t volatile*)ptr, (int32_t)val);
#else
  return (void*)interlocked_and_fetch_64((int64_t volatile*)ptr, (int64_t)val);
#endif
}

int64_t interlocked_fetch_and_64(int64_t volatile* ptr, int64_t val) {
80
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
81
  int64_t old;
S
slguan 已提交
82 83
  do {
    old = *ptr;
84
  } while (_InterlockedCompareExchange64(ptr, old & val, old) != old);
S
slguan 已提交
85
  return old;
86
#else
wafwerar's avatar
wafwerar 已提交
87
  return _InterlockedAnd64((int64_t volatile*)(ptr), (int64_t)(val));
S
slguan 已提交
88
#endif
89
}
S
slguan 已提交
90

wafwerar's avatar
wafwerar 已提交
91
void* interlocked_fetch_and_ptr(void* volatile* ptr, void* val) {
92
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
93 94 95 96 97 98 99
  return (void*)_InterlockedAnd((int32_t volatile*)(ptr), (int32_t)(val));
#else
  return (void*)_InterlockedAnd64((int64_t volatile*)(ptr), (int64_t)(val));
#endif
}

int8_t interlocked_or_fetch_8(int8_t volatile* ptr, int8_t val) {
S
slguan 已提交
100 101 102
  return _InterlockedOr8(ptr, val) | val;
}

wafwerar's avatar
wafwerar 已提交
103
int16_t interlocked_or_fetch_16(int16_t volatile* ptr, int16_t val) {
S
slguan 已提交
104 105
  return _InterlockedOr16(ptr, val) | val;
}
106

wafwerar's avatar
wafwerar 已提交
107
int32_t interlocked_or_fetch_32(int32_t volatile* ptr, int32_t val) {
S
slguan 已提交
108 109 110
  return _InterlockedOr(ptr, val) | val;
}

wafwerar's avatar
wafwerar 已提交
111
int64_t interlocked_or_fetch_64(int64_t volatile* ptr, int64_t val) {
112
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
113
  int64_t old, res;
S
slguan 已提交
114 115 116 117 118
  do {
    old = *ptr;
    res = old | val;
  } while(_InterlockedCompareExchange64(ptr, res, old) != old);
  return res;
119 120 121
#else
  return _InterlockedOr64(ptr, val) & val;
#endif
S
slguan 已提交
122 123
}

wafwerar's avatar
wafwerar 已提交
124
void* interlocked_or_fetch_ptr(void* volatile* ptr, void* val) {
125
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
126 127 128 129 130 131 132
  return (void*)interlocked_or_fetch_32((int32_t volatile*)ptr, (int32_t)val);
#else
  return (void*)interlocked_or_fetch_64((int64_t volatile*)ptr, (int64_t)val);
#endif
}

int64_t interlocked_fetch_or_64(int64_t volatile* ptr, int64_t val) {
133
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
134
  int64_t old;
S
slguan 已提交
135 136 137 138
  do {
    old = *ptr;
  } while(_InterlockedCompareExchange64(ptr, old | val, old) != old);
  return old;
139
#else
wafwerar's avatar
wafwerar 已提交
140 141 142 143 144
  return _InterlockedOr64((int64_t volatile*)(ptr), (int64_t)(val));
#endif
}

void* interlocked_fetch_or_ptr(void* volatile* ptr, void* val) {
145
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
146 147 148
  return (void*)_InterlockedOr((int32_t volatile*)(ptr), (int32_t)(val));
#else
  return (void*)interlocked_fetch_or_64((int64_t volatile*)(ptr), (int64_t)(val));
S
slguan 已提交
149
#endif
150
}
S
slguan 已提交
151

wafwerar's avatar
wafwerar 已提交
152
int8_t interlocked_xor_fetch_8(int8_t volatile* ptr, int8_t val) {
S
slguan 已提交
153 154 155
  return _InterlockedXor8(ptr, val) ^ val;
}

wafwerar's avatar
wafwerar 已提交
156
int16_t interlocked_xor_fetch_16(int16_t volatile* ptr, int16_t val) {
S
slguan 已提交
157 158
  return _InterlockedXor16(ptr, val) ^ val;
}
159

wafwerar's avatar
wafwerar 已提交
160
int32_t interlocked_xor_fetch_32(int32_t volatile* ptr, int32_t val) {
S
slguan 已提交
161 162 163
  return _InterlockedXor(ptr, val) ^ val;
}

wafwerar's avatar
wafwerar 已提交
164
int64_t interlocked_xor_fetch_64(int64_t volatile* ptr, int64_t val) {
165
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
166
  int64_t old, res;
S
slguan 已提交
167 168 169 170 171
  do {
    old = *ptr;
    res = old ^ val;
  } while(_InterlockedCompareExchange64(ptr, res, old) != old);
  return res;
172 173 174
#else
  return _InterlockedXor64(ptr, val) ^ val;
#endif
S
slguan 已提交
175 176
}

wafwerar's avatar
wafwerar 已提交
177
void* interlocked_xor_fetch_ptr(void* volatile* ptr, void* val) {
178
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
179 180 181 182 183 184 185
  return (void*)interlocked_xor_fetch_32((int32_t volatile*)(ptr), (int32_t)(val));
#else
  return (void*)interlocked_xor_fetch_64((int64_t volatile*)(ptr), (int64_t)(val));
#endif
}

int64_t interlocked_fetch_xor_64(int64_t volatile* ptr, int64_t val) {
186
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
187
  int64_t old;
S
slguan 已提交
188 189
  do {
    old = *ptr;
190
  } while (_InterlockedCompareExchange64(ptr, old ^ val, old) != old);
S
slguan 已提交
191
  return old;
192
#else
wafwerar's avatar
wafwerar 已提交
193 194 195 196 197
  return _InterlockedXor64((int64_t volatile*)(ptr), (int64_t)(val));
#endif
}

void* interlocked_fetch_xor_ptr(void* volatile* ptr, void* val) {
198
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
  return (void*)_InterlockedXor((int32_t volatile*)(ptr), (int32_t)(val));
#else
  return (void*)interlocked_fetch_xor_64((int64_t volatile*)(ptr), (int64_t)(val));
#endif
}

int32_t interlocked_sub_fetch_32(int32_t volatile* ptr, int32_t val) {
  return interlocked_add_fetch_32(ptr, -val);
}

int64_t interlocked_sub_fetch_64(int64_t volatile* ptr, int64_t val) {
  return interlocked_add_fetch_64(ptr, -val);
}

void* interlocked_sub_fetch_ptr(void* volatile* ptr, void* val) {
214
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228
  return (void*)interlocked_sub_fetch_32((int32_t volatile*)ptr, (int32_t)val);
#else
  return (void*)interlocked_add_fetch_64((int64_t volatile*)ptr, (int64_t)val);
#endif
}
int32_t interlocked_fetch_sub_32(int32_t volatile* ptr, int32_t val) {
  return _InterlockedExchangeAdd(ptr, -val);
}

int64_t interlocked_fetch_sub_64(int64_t volatile* ptr, int64_t val) {
  return _InterlockedExchangeAdd64(ptr, -val);
}

void* interlocked_fetch_sub_ptr(void* volatile* ptr, void* val) {
229
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
230 231 232
  return (void*)interlocked_fetch_sub_32((int32_t volatile*)ptr, (int32_t)val);
#else
  return (void*)interlocked_fetch_sub_64((int64_t volatile*)ptr, (int64_t)val);
S
slguan 已提交
233
#endif
234
}
S
slguan 已提交
235

S
Shengliang Guan 已提交
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
#endif

#ifdef _TD_NINGSI_60
void* atomic_exchange_ptr_impl(void** ptr, void* val ) {
  void *old;
  do {
    old = *ptr;
  } while( !__sync_bool_compare_and_swap(ptr, old, val) );
  return old;
}
int8_t atomic_exchange_8_impl(int8_t* ptr, int8_t val ) {
  int8_t old;
  do {
    old = *ptr;
  } while( !__sync_bool_compare_and_swap(ptr, old, val) );
  return old;
}
int16_t atomic_exchange_16_impl(int16_t* ptr, int16_t val ) {
  int16_t old;
  do {
    old = *ptr;
  } while( !__sync_bool_compare_and_swap(ptr, old, val) );
  return old;
}
int32_t atomic_exchange_32_impl(int32_t* ptr, int32_t val ) {
  int32_t old;
  do {
    old = *ptr;
  } while( !__sync_bool_compare_and_swap(ptr, old, val) );
  return old;
}
int64_t atomic_exchange_64_impl(int64_t* ptr, int64_t val ) {
  int64_t old;
  do {
    old = *ptr;
  } while( !__sync_bool_compare_and_swap(ptr, old, val) );
  return old;
}
#endif
wafwerar's avatar
wafwerar 已提交
275 276 277


int8_t atomic_load_8(int8_t volatile *ptr) {
wafwerar's avatar
wafwerar 已提交
278
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
279 280 281 282 283 284 285 286 287
  return (*(int8_t volatile*)(ptr));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_add((ptr), 0);
#else
  return __atomic_load_n((ptr), __ATOMIC_SEQ_CST);
#endif
}

int16_t atomic_load_16(int16_t volatile *ptr) {
wafwerar's avatar
wafwerar 已提交
288
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
289 290 291 292 293 294 295 296 297
  return (*(int16_t volatile*)(ptr));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_add((ptr), 0);
#else
  return __atomic_load_n((ptr), __ATOMIC_SEQ_CST);
#endif
}

int32_t atomic_load_32(int32_t volatile *ptr) {
wafwerar's avatar
wafwerar 已提交
298
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
299 300 301 302 303 304 305 306 307
  return (*(int32_t volatile*)(ptr));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_add((ptr), 0);
#else
  return __atomic_load_n((ptr), __ATOMIC_SEQ_CST);
#endif
}

int64_t atomic_load_64(int64_t volatile *ptr) {
wafwerar's avatar
wafwerar 已提交
308
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
309 310 311 312 313 314 315 316 317
  return (*(int64_t volatile*)(ptr));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_add((ptr), 0);
#else
  return __atomic_load_n((ptr), __ATOMIC_SEQ_CST);
#endif
}

void* atomic_load_ptr(void *ptr) {
wafwerar's avatar
wafwerar 已提交
318
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
319 320 321 322 323 324 325 326 327
  return (*(void* volatile*)(ptr));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_add((ptr), 0);
#else
  return __atomic_load_n((void**)(ptr), __ATOMIC_SEQ_CST);
#endif
}

void atomic_store_8(int8_t volatile *ptr, int8_t val) {
wafwerar's avatar
wafwerar 已提交
328
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
329 330 331 332 333 334 335 336 337
  ((*(int8_t volatile*)(ptr)) = (int8_t)(val));
#elif defined(_TD_NINGSI_60)
  (*(ptr)=(val));
#else
  __atomic_store_n((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

void atomic_store_16(int16_t volatile *ptr, int16_t val) {
wafwerar's avatar
wafwerar 已提交
338
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
339 340 341 342 343 344 345 346 347
  ((*(int16_t volatile*)(ptr)) = (int16_t)(val));
#elif defined(_TD_NINGSI_60)
  (*(ptr)=(val));
#else
  __atomic_store_n((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

void atomic_store_32(int32_t volatile *ptr, int32_t val) {
wafwerar's avatar
wafwerar 已提交
348
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
349 350 351 352 353 354 355 356 357
  ((*(int32_t volatile*)(ptr)) = (int32_t)(val));
#elif defined(_TD_NINGSI_60)
  (*(ptr)=(val));
#else
  __atomic_store_n((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

void atomic_store_64(int64_t volatile *ptr, int64_t val) {
wafwerar's avatar
wafwerar 已提交
358
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
359 360 361 362 363 364 365 366 367
  ((*(int64_t volatile*)(ptr)) = (int64_t)(val));
#elif defined(_TD_NINGSI_60)
  (*(ptr)=(val));
#else
  __atomic_store_n((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

void atomic_store_ptr(void *ptr, void *val) {
wafwerar's avatar
wafwerar 已提交
368
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
369 370 371 372 373 374 375 376 377
  ((*(void* volatile*)(ptr)) = (void*)(val));
#elif defined(_TD_NINGSI_60)
  (*(ptr)=(val));
#else
  __atomic_store_n((void **)(ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int8_t atomic_exchange_8(int8_t volatile *ptr, int8_t val) {
wafwerar's avatar
wafwerar 已提交
378
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
379 380 381 382 383 384 385 386 387
  return _InterlockedExchange8((int8_t volatile*)(ptr), (int8_t)(val));
#elif defined(_TD_NINGSI_60)
  return atomic_exchange_8_impl((int8_t*)ptr, (int8_t)val);
#else
  return __atomic_exchange_n((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int16_t atomic_exchange_16(int16_t volatile *ptr, int16_t val) {
wafwerar's avatar
wafwerar 已提交
388
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
389 390 391 392 393 394 395 396 397
  return _InterlockedExchange16((int16_t volatile*)(ptr), (int16_t)(val));
#elif defined(_TD_NINGSI_60)
  return atomic_exchange_16_impl((int16_t*)ptr, (int16_t)val);
#else
  return __atomic_exchange_n((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int32_t atomic_exchange_32(int32_t volatile *ptr, int32_t val) {
wafwerar's avatar
wafwerar 已提交
398
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
399 400 401 402 403 404 405 406 407
  return _InterlockedExchange((int32_t volatile*)(ptr), (int32_t)(val));
#elif defined(_TD_NINGSI_60)
  return atomic_exchange_32_impl((int32_t*)ptr, (int32_t)val);
#else
  return __atomic_exchange_n((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int64_t atomic_exchange_64(int64_t volatile *ptr, int64_t val) {
wafwerar's avatar
wafwerar 已提交
408
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
409 410 411 412 413 414 415 416 417
  return _InterlockedExchange64((int64_t volatile*)(ptr), (int64_t)(val));
#elif defined(_TD_NINGSI_60)
  return atomic_exchange_64_impl((int64_t*)ptr, (int64_t)val);
#else
  return __atomic_exchange_n((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

void* atomic_exchange_ptr(void *ptr, void *val) {
wafwerar's avatar
wafwerar 已提交
418
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431
  #ifdef _WIN64 
  return _InterlockedExchangePointer((void* volatile*)(ptr), (void*)(val));
  #else
  return _InlineInterlockedExchangePointer((void* volatile*)(ptr), (void*)(val));
  #endif
#elif defined(_TD_NINGSI_60)
  return atomic_exchange_ptr_impl((void *)ptr, (void*)val);
#else
  return __atomic_exchange_n((void **)(ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int8_t atomic_val_compare_exchange_8(int8_t volatile *ptr, int8_t oldval, int8_t newval) {
wafwerar's avatar
wafwerar 已提交
432
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
433 434 435 436 437 438 439 440 441
  return _InterlockedCompareExchange8((int8_t volatile*)(ptr), (int8_t)(newval), (int8_t)(oldval));
#elif defined(_TD_NINGSI_60)
  return __sync_val_compare_and_swap(ptr, oldval, newval);
#else
  return __sync_val_compare_and_swap(ptr, oldval, newval);
#endif
}

int16_t atomic_val_compare_exchange_16(int16_t volatile *ptr, int16_t oldval, int16_t newval) {
wafwerar's avatar
wafwerar 已提交
442
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
443 444 445 446 447 448 449 450 451
  return _InterlockedCompareExchange16((int16_t volatile*)(ptr), (int16_t)(newval), (int16_t)(oldval));
#elif defined(_TD_NINGSI_60)
  return __sync_val_compare_and_swap(ptr, oldval, newval);
#else
  return __sync_val_compare_and_swap(ptr, oldval, newval);
#endif
}

int32_t atomic_val_compare_exchange_32(int32_t volatile *ptr, int32_t oldval, int32_t newval) {
wafwerar's avatar
wafwerar 已提交
452
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
453 454 455 456 457 458 459 460 461
  return _InterlockedCompareExchange((int32_t volatile*)(ptr), (int32_t)(newval), (int32_t)(oldval));
#elif defined(_TD_NINGSI_60)
  return __sync_val_compare_and_swap(ptr, oldval, newval);
#else
  return __sync_val_compare_and_swap(ptr, oldval, newval);
#endif
}

int64_t atomic_val_compare_exchange_64(int64_t volatile *ptr, int64_t oldval, int64_t newval) {
wafwerar's avatar
wafwerar 已提交
462
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
463 464 465 466 467 468 469 470 471
  return _InterlockedCompareExchange64((int64_t volatile*)(ptr), (int64_t)(newval), (int64_t)(oldval));
#elif defined(_TD_NINGSI_60)
  return __sync_val_compare_and_swap(ptr, oldval, newval);
#else
  return __sync_val_compare_and_swap(ptr, oldval, newval);
#endif
}

void* atomic_val_compare_exchange_ptr(void *ptr, void *oldval, void *newval) {
wafwerar's avatar
wafwerar 已提交
472
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
473 474 475 476 477 478 479 480 481
  return _InterlockedCompareExchangePointer((void* volatile*)(ptr), (void*)(newval), (void*)(oldval));
#elif defined(_TD_NINGSI_60)
  return __sync_val_compare_and_swap(ptr, oldval, newval);
#else
  return __sync_val_compare_and_swap((void **)ptr, oldval, newval);
#endif
}

int8_t atomic_add_fetch_8(int8_t volatile *ptr, int8_t val) {
wafwerar's avatar
wafwerar 已提交
482
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
483 484 485 486 487 488 489 490 491
  return interlocked_add_fetch_8((int8_t volatile*)(ptr), (int8_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_add_and_fetch((ptr), (val));
#else
  return __atomic_add_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int16_t atomic_add_fetch_16(int16_t volatile *ptr, int16_t val) {
wafwerar's avatar
wafwerar 已提交
492
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
493 494 495 496 497 498 499 500 501
  return interlocked_add_fetch_16((int16_t volatile*)(ptr), (int16_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_add_and_fetch((ptr), (val));
#else
  return __atomic_add_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int32_t atomic_add_fetch_32(int32_t volatile *ptr, int32_t val) {
wafwerar's avatar
wafwerar 已提交
502
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
503 504 505 506 507 508 509 510 511
  return interlocked_add_fetch_32((int32_t volatile*)(ptr), (int32_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_add_and_fetch((ptr), (val));
#else
  return __atomic_add_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int64_t atomic_add_fetch_64(int64_t volatile *ptr, int64_t val) {
wafwerar's avatar
wafwerar 已提交
512
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
513 514 515 516 517 518 519 520
  return interlocked_add_fetch_64((int64_t volatile*)(ptr), (int64_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_add_and_fetch((ptr), (val));
#else
  return __atomic_add_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

wafwerar's avatar
wafwerar 已提交
521 522
void* atomic_add_fetch_ptr(void *ptr, void *val) {
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
523 524 525 526
  return interlocked_add_fetch_ptr((void* volatile*)(ptr), (void*)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_add_and_fetch((ptr), (val));
#else
wafwerar's avatar
wafwerar 已提交
527
  return __atomic_add_fetch((void **)(ptr), (size_t)(val), __ATOMIC_SEQ_CST);
wafwerar's avatar
wafwerar 已提交
528 529 530 531
#endif
}

int8_t atomic_fetch_add_8(int8_t volatile *ptr, int8_t val) {
wafwerar's avatar
wafwerar 已提交
532
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
533 534 535 536 537 538 539 540 541
  return _InterlockedExchangeAdd8((int8_t volatile*)(ptr), (int8_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_add((ptr), (val));
#else
  return __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int16_t atomic_fetch_add_16(int16_t volatile *ptr, int16_t val) {
wafwerar's avatar
wafwerar 已提交
542
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
543 544 545 546 547 548 549 550 551
  return _InterlockedExchangeAdd16((int16_t volatile*)(ptr), (int16_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_add((ptr), (val));
#else
  return __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int32_t atomic_fetch_add_32(int32_t volatile *ptr, int32_t val) {
wafwerar's avatar
wafwerar 已提交
552
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
553 554 555 556 557 558 559 560 561
  return _InterlockedExchangeAdd((int32_t volatile*)(ptr), (int32_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_add((ptr), (val));
#else
  return __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int64_t atomic_fetch_add_64(int64_t volatile *ptr, int64_t val) {
wafwerar's avatar
wafwerar 已提交
562
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
563 564 565 566 567 568 569 570
  return _InterlockedExchangeAdd64((int64_t volatile*)(ptr), (int64_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_add((ptr), (val));
#else
  return __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

wafwerar's avatar
wafwerar 已提交
571 572 573
void* atomic_fetch_add_ptr(void *ptr, void *val) {
#ifdef WINDOWS
  return _InterlockedExchangePointer((void* volatile*)(ptr), (void*)(val));
wafwerar's avatar
wafwerar 已提交
574 575 576
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_add((ptr), (val));
#else
wafwerar's avatar
wafwerar 已提交
577
  return __atomic_fetch_add((void **)(ptr), (size_t)(val), __ATOMIC_SEQ_CST);
wafwerar's avatar
wafwerar 已提交
578 579 580 581
#endif
}

int8_t atomic_sub_fetch_8(int8_t volatile *ptr, int8_t val) {
wafwerar's avatar
wafwerar 已提交
582
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
583 584 585 586 587 588 589 590 591
  return interlocked_add_fetch_8((int8_t volatile*)(ptr), -(int8_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_sub_and_fetch((ptr), (val));
#else
  return __atomic_sub_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int16_t atomic_sub_fetch_16(int16_t volatile *ptr, int16_t val) {
wafwerar's avatar
wafwerar 已提交
592
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
593 594 595 596 597 598 599 600 601
  return interlocked_add_fetch_16((int16_t volatile*)(ptr), -(int16_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_sub_and_fetch((ptr), (val));
#else
  return __atomic_sub_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int32_t atomic_sub_fetch_32(int32_t volatile *ptr, int32_t val) {
wafwerar's avatar
wafwerar 已提交
602 603
#ifdef WINDOWS
  return interlocked_sub_fetch_32(ptr, val);
wafwerar's avatar
wafwerar 已提交
604 605 606 607 608 609 610 611
#elif defined(_TD_NINGSI_60)
  return __sync_sub_and_fetch((ptr), (val));
#else
  return __atomic_sub_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int64_t atomic_sub_fetch_64(int64_t volatile *ptr, int64_t val) {
wafwerar's avatar
wafwerar 已提交
612 613
#ifdef WINDOWS
  return interlocked_sub_fetch_64(ptr, val);
wafwerar's avatar
wafwerar 已提交
614 615 616 617 618 619 620
#elif defined(_TD_NINGSI_60)
  return __sync_sub_and_fetch((ptr), (val));
#else
  return __atomic_sub_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

wafwerar's avatar
wafwerar 已提交
621 622 623
void* atomic_sub_fetch_ptr(void *ptr, void* val) {
#ifdef WINDOWS
  return interlocked_sub_fetch_ptr(ptr, val);
wafwerar's avatar
wafwerar 已提交
624 625 626 627 628 629 630 631
#elif defined(_TD_NINGSI_60)
  return __sync_sub_and_fetch((ptr), (val));
#else
  return __atomic_sub_fetch((void **)(ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int8_t atomic_fetch_sub_8(int8_t volatile *ptr, int8_t val) {
wafwerar's avatar
wafwerar 已提交
632
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
633 634 635 636 637 638 639 640 641
  return _InterlockedExchangeAdd8((int8_t volatile*)(ptr), -(int8_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_sub((ptr), (val));
#else
  return __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int16_t atomic_fetch_sub_16(int16_t volatile *ptr, int16_t val) {
wafwerar's avatar
wafwerar 已提交
642
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
643 644 645 646 647 648 649 650 651
  return _InterlockedExchangeAdd16((int16_t volatile*)(ptr), -(int16_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_sub((ptr), (val));
#else
  return __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int32_t atomic_fetch_sub_32(int32_t volatile *ptr, int32_t val) {
wafwerar's avatar
wafwerar 已提交
652 653
#ifdef WINDOWS
  return interlocked_fetch_sub_32(ptr, val);
wafwerar's avatar
wafwerar 已提交
654
#elif defined(_TD_NINGSI_60)
wafwerar's avatar
wafwerar 已提交
655
  return __sync_fetch_and_sub(ptr, val);
wafwerar's avatar
wafwerar 已提交
656 657 658 659 660 661
#else
  return __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int64_t atomic_fetch_sub_64(int64_t volatile *ptr, int64_t val) {
wafwerar's avatar
wafwerar 已提交
662
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
663 664 665 666 667 668 669 670
  return _InterlockedExchangeAdd64((int64_t volatile*)(ptr), -(int64_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_sub((ptr), (val));
#else
  return __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

wafwerar's avatar
wafwerar 已提交
671 672 673
void* atomic_fetch_sub_ptr(void *ptr, void* val) {
#ifdef WINDOWS
  return interlocked_fetch_sub_ptr(ptr, val);
wafwerar's avatar
wafwerar 已提交
674 675 676 677 678 679 680 681
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_sub((ptr), (val));
#else
  return __atomic_fetch_sub((void **)(ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int8_t atomic_and_fetch_8(int8_t volatile *ptr, int8_t val) {
wafwerar's avatar
wafwerar 已提交
682
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
683 684 685 686 687 688 689 690 691
  return interlocked_and_fetch_8((int8_t volatile*)(ptr), (int8_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_and_and_fetch((ptr), (val));
#else
  return __atomic_and_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int16_t atomic_and_fetch_16(int16_t volatile *ptr, int16_t val) {
wafwerar's avatar
wafwerar 已提交
692
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
693 694 695 696 697 698 699 700 701
  return interlocked_and_fetch_16((int16_t volatile*)(ptr), (int16_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_and_and_fetch((ptr), (val));
#else
  return __atomic_and_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int32_t atomic_and_fetch_32(int32_t volatile *ptr, int32_t val) {
wafwerar's avatar
wafwerar 已提交
702
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
703 704 705 706 707 708 709 710 711
  return interlocked_and_fetch_32((int32_t volatile*)(ptr), (int32_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_and_and_fetch((ptr), (val));
#else
  return __atomic_and_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int64_t atomic_and_fetch_64(int64_t volatile *ptr, int64_t val) {
wafwerar's avatar
wafwerar 已提交
712
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
713 714 715 716 717 718 719 720 721
  return interlocked_and_fetch_64((int64_t volatile*)(ptr), (int64_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_and_and_fetch((ptr), (val));
#else
  return __atomic_and_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

void* atomic_and_fetch_ptr(void *ptr, void *val) {
wafwerar's avatar
wafwerar 已提交
722
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
723 724 725 726 727 728 729 730 731
  return interlocked_and_fetch_ptr((void* volatile*)(ptr), (void*)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_and_and_fetch((ptr), (val));
#else
  return __atomic_and_fetch((void **)(ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int8_t atomic_fetch_and_8(int8_t volatile *ptr, int8_t val) {
wafwerar's avatar
wafwerar 已提交
732
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
733 734 735 736 737 738 739 740 741
  return _InterlockedAnd8((int8_t volatile*)(ptr), (int8_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_and((ptr), (val));
#else
  return __atomic_fetch_and((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int16_t atomic_fetch_and_16(int16_t volatile *ptr, int16_t val) {
wafwerar's avatar
wafwerar 已提交
742
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
743 744 745 746 747 748 749 750 751
  return _InterlockedAnd16((int16_t volatile*)(ptr), (int16_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_and((ptr), (val));
#else
  return __atomic_fetch_and((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int32_t atomic_fetch_and_32(int32_t volatile *ptr, int32_t val) {
wafwerar's avatar
wafwerar 已提交
752
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
753 754 755 756 757 758 759 760 761
  return _InterlockedAnd((int32_t volatile*)(ptr), (int32_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_and((ptr), (val));
#else
  return __atomic_fetch_and((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int64_t atomic_fetch_and_64(int64_t volatile *ptr, int64_t val) {
wafwerar's avatar
wafwerar 已提交
762
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
763 764 765 766 767 768 769 770 771
  return interlocked_fetch_and_64((int64_t volatile*)(ptr), (int64_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_and((ptr), (val));
#else
  return __atomic_fetch_and((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

void* atomic_fetch_and_ptr(void *ptr, void *val) {
wafwerar's avatar
wafwerar 已提交
772
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
773 774 775 776 777 778 779 780 781
  return interlocked_fetch_and_ptr((void* volatile*)(ptr), (void*)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_and((ptr), (val));
#else
  return __atomic_fetch_and((void **)(ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int8_t atomic_or_fetch_8(int8_t volatile *ptr, int8_t val) {
wafwerar's avatar
wafwerar 已提交
782
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
783 784 785 786 787 788 789 790 791
  return interlocked_or_fetch_8((int8_t volatile*)(ptr), (int8_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_or_and_fetch((ptr), (val));
#else
  return __atomic_or_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int16_t atomic_or_fetch_16(int16_t volatile *ptr, int16_t val) {
wafwerar's avatar
wafwerar 已提交
792
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
793 794 795 796 797 798 799 800 801
  return interlocked_or_fetch_16((int16_t volatile*)(ptr), (int16_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_or_and_fetch((ptr), (val));
#else
  return __atomic_or_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int32_t atomic_or_fetch_32(int32_t volatile *ptr, int32_t val) {
wafwerar's avatar
wafwerar 已提交
802
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
803 804 805 806 807 808 809 810 811
  return interlocked_or_fetch_32((int32_t volatile*)(ptr), (int32_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_or_and_fetch((ptr), (val));
#else
  return __atomic_or_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int64_t atomic_or_fetch_64(int64_t volatile *ptr, int64_t val) {
wafwerar's avatar
wafwerar 已提交
812
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
813 814 815 816 817 818 819 820 821
  return interlocked_or_fetch_64((int64_t volatile*)(ptr), (int64_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_or_and_fetch((ptr), (val));
#else
  return __atomic_or_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

void* atomic_or_fetch_ptr(void *ptr, void *val) {
wafwerar's avatar
wafwerar 已提交
822
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
823 824 825 826 827 828 829 830 831
  return interlocked_or_fetch_ptr((void* volatile*)(ptr), (void*)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_or_and_fetch((ptr), (val));
#else
  return __atomic_or_fetch((void **)(ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int8_t atomic_fetch_or_8(int8_t volatile *ptr, int8_t val) {
wafwerar's avatar
wafwerar 已提交
832
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
833 834 835 836 837 838 839 840 841
  return _InterlockedOr8((int8_t volatile*)(ptr), (int8_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_or((ptr), (val));
#else
  return __atomic_fetch_or((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int16_t atomic_fetch_or_16(int16_t volatile *ptr, int16_t val) {
wafwerar's avatar
wafwerar 已提交
842
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
843 844 845 846 847 848 849 850 851
  return _InterlockedOr16((int16_t volatile*)(ptr), (int16_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_or((ptr), (val));
#else
  return __atomic_fetch_or((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int32_t atomic_fetch_or_32(int32_t volatile *ptr, int32_t val) {
wafwerar's avatar
wafwerar 已提交
852
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
853 854 855 856 857 858 859 860 861
  return _InterlockedOr((int32_t volatile*)(ptr), (int32_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_or((ptr), (val));
#else
  return __atomic_fetch_or((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int64_t atomic_fetch_or_64(int64_t volatile *ptr, int64_t val) {
wafwerar's avatar
wafwerar 已提交
862
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
863 864 865 866 867 868 869 870 871
  return interlocked_fetch_or_64((int64_t volatile*)(ptr), (int64_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_or((ptr), (val));
#else
  return __atomic_fetch_or((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

void* atomic_fetch_or_ptr(void *ptr, void *val) {
wafwerar's avatar
wafwerar 已提交
872
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
873 874 875 876 877 878 879 880 881
  return interlocked_fetch_or_ptr((void* volatile*)(ptr), (void*)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_or((ptr), (val));
#else
  return __atomic_fetch_or((void **)(ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int8_t atomic_xor_fetch_8(int8_t volatile *ptr, int8_t val) {
wafwerar's avatar
wafwerar 已提交
882
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
883 884 885 886 887 888 889 890 891
  return interlocked_xor_fetch_8((int8_t volatile*)(ptr), (int8_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_xor_and_fetch((ptr), (val));
#else
  return __atomic_xor_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int16_t atomic_xor_fetch_16(int16_t volatile *ptr, int16_t val) {
wafwerar's avatar
wafwerar 已提交
892
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
893 894 895 896 897 898 899 900 901
  return interlocked_xor_fetch_16((int16_t volatile*)(ptr), (int16_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_xor_and_fetch((ptr), (val));
#else
  return __atomic_xor_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int32_t atomic_xor_fetch_32(int32_t volatile *ptr, int32_t val) {
wafwerar's avatar
wafwerar 已提交
902
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
903 904 905 906 907 908 909 910 911
  return interlocked_xor_fetch_32((int32_t volatile*)(ptr), (int32_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_xor_and_fetch((ptr), (val));
#else
  return __atomic_xor_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int64_t atomic_xor_fetch_64(int64_t volatile *ptr, int64_t val) {
wafwerar's avatar
wafwerar 已提交
912
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
913 914 915 916 917 918 919 920 921
  return interlocked_xor_fetch_64((int64_t volatile*)(ptr), (int64_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_xor_and_fetch((ptr), (val));
#else
  return __atomic_xor_fetch((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

void* atomic_xor_fetch_ptr(void *ptr, void *val) {
wafwerar's avatar
wafwerar 已提交
922
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
923 924 925 926 927 928 929 930 931
  return interlocked_xor_fetch_ptr((void* volatile*)(ptr), (void*)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_xor_and_fetch((ptr), (val));
#else
  return __atomic_xor_fetch((void **)(ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int8_t atomic_fetch_xor_8(int8_t volatile *ptr, int8_t val) {
wafwerar's avatar
wafwerar 已提交
932
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
933 934 935 936 937 938 939 940 941
  return _InterlockedXor8((int8_t volatile*)(ptr), (int8_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_xor((ptr), (val));
#else
  return __atomic_fetch_xor((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int16_t atomic_fetch_xor_16(int16_t volatile *ptr, int16_t val) {
wafwerar's avatar
wafwerar 已提交
942
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
943 944 945 946 947 948 949 950 951
  return _InterlockedXor16((int16_t volatile*)(ptr), (int16_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_xor((ptr), (val));
#else
  return __atomic_fetch_xor((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int32_t atomic_fetch_xor_32(int32_t volatile *ptr, int32_t val) {
wafwerar's avatar
wafwerar 已提交
952
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
953 954 955 956 957 958 959 960 961
  return _InterlockedXor((int32_t volatile*)(ptr), (int32_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_xor((ptr), (val));
#else
  return __atomic_fetch_xor((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int64_t atomic_fetch_xor_64(int64_t volatile *ptr, int64_t val) {
wafwerar's avatar
wafwerar 已提交
962
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
963 964 965 966 967 968 969 970 971
  return interlocked_fetch_xor_64((int64_t volatile*)(ptr), (int64_t)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_xor((ptr), (val));
#else
  return __atomic_fetch_xor((ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

void* atomic_fetch_xor_ptr(void *ptr, void *val) {
wafwerar's avatar
wafwerar 已提交
972
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
973 974 975 976 977 978 979 980
  return interlocked_fetch_xor_ptr((void* volatile*)(ptr), (void*)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_xor((ptr), (val));
#else
  return __atomic_fetch_xor((void **)(ptr), (val), __ATOMIC_SEQ_CST);
#endif
}