osAtomic.c 28.1 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 19

#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
S
slguan 已提交
20 21 22

// add
char interlocked_add_fetch_8(char volatile* ptr, char val) {
23
  return _InterlockedExchangeAdd8(ptr, val) + val;
S
slguan 已提交
24 25 26
}

short interlocked_add_fetch_16(short volatile* ptr, short val) {
27
  return _InterlockedExchangeAdd16(ptr, val) + val;
S
slguan 已提交
28 29 30 31 32 33 34
}

long interlocked_add_fetch_32(long volatile* ptr, long val) {
  return _InterlockedExchangeAdd(ptr, val) + val;
}

__int64 interlocked_add_fetch_64(__int64 volatile* ptr, __int64 val) {
35
  return InterlockedExchangeAdd64(ptr, val) + val;
S
slguan 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
}

char interlocked_and_fetch_8(char volatile* ptr, char val) {
  return _InterlockedAnd8(ptr, val) & val;
}

short interlocked_and_fetch_16(short volatile* ptr, short val) {
  return _InterlockedAnd16(ptr, val) & val;
}

long interlocked_and_fetch_32(long volatile* ptr, long val) {
  return _InterlockedAnd(ptr, val) & val;
}


__int64 interlocked_and_fetch_64(__int64 volatile* ptr, __int64 val) {
52
#ifndef _M_IX86
S
slguan 已提交
53 54 55 56 57 58
  return _InterlockedAnd64(ptr, val) & val;
#else
  __int64 old, res;
  do {
    old = *ptr;
    res = old & val;
59
  } while (_InterlockedCompareExchange64(ptr, res, old) != old);
S
slguan 已提交
60
  return res;
61
#endif
S
slguan 已提交
62 63 64
}

__int64 interlocked_fetch_and_64(__int64 volatile* ptr, __int64 val) {
65
#ifdef _M_IX86
S
slguan 已提交
66 67 68
  __int64 old;
  do {
    old = *ptr;
69
  } while (_InterlockedCompareExchange64(ptr, old & val, old) != old);
S
slguan 已提交
70
  return old;
71 72
#else
  return _InterlockedAnd64((__int64 volatile*)(ptr), (__int64)(val));
S
slguan 已提交
73
#endif
74
}
S
slguan 已提交
75 76 77 78 79 80 81 82

char interlocked_or_fetch_8(char volatile* ptr, char val) {
  return _InterlockedOr8(ptr, val) | val;
}

short interlocked_or_fetch_16(short volatile* ptr, short val) {
  return _InterlockedOr16(ptr, val) | val;
}
83

S
slguan 已提交
84 85 86 87 88
long interlocked_or_fetch_32(long volatile* ptr, long val) {
  return _InterlockedOr(ptr, val) | val;
}

__int64 interlocked_or_fetch_64(__int64 volatile* ptr, __int64 val) {
89
#ifdef _M_IX86
S
slguan 已提交
90 91 92 93 94 95
  __int64 old, res;
  do {
    old = *ptr;
    res = old | val;
  } while(_InterlockedCompareExchange64(ptr, res, old) != old);
  return res;
96 97 98
#else
  return _InterlockedOr64(ptr, val) & val;
#endif
S
slguan 已提交
99 100 101
}

__int64 interlocked_fetch_or_64(__int64 volatile* ptr, __int64 val) {
102
#ifdef _M_IX86
S
slguan 已提交
103 104 105 106 107
  __int64 old;
  do {
    old = *ptr;
  } while(_InterlockedCompareExchange64(ptr, old | val, old) != old);
  return old;
108 109
#else
  return _InterlockedOr64((__int64 volatile*)(ptr), (__int64)(val));
S
slguan 已提交
110
#endif
111
}
S
slguan 已提交
112 113 114 115 116 117 118 119

char interlocked_xor_fetch_8(char volatile* ptr, char val) {
  return _InterlockedXor8(ptr, val) ^ val;
}

short interlocked_xor_fetch_16(short volatile* ptr, short val) {
  return _InterlockedXor16(ptr, val) ^ val;
}
120

S
slguan 已提交
121 122 123 124 125
long interlocked_xor_fetch_32(long volatile* ptr, long val) {
  return _InterlockedXor(ptr, val) ^ val;
}

__int64 interlocked_xor_fetch_64(__int64 volatile* ptr, __int64 val) {
126
#ifdef _M_IX86
S
slguan 已提交
127 128 129 130 131 132
  __int64 old, res;
  do {
    old = *ptr;
    res = old ^ val;
  } while(_InterlockedCompareExchange64(ptr, res, old) != old);
  return res;
133 134 135
#else
  return _InterlockedXor64(ptr, val) ^ val;
#endif
S
slguan 已提交
136 137 138
}

__int64 interlocked_fetch_xor_64(__int64 volatile* ptr, __int64 val) {
139
#ifdef _M_IX86
S
slguan 已提交
140 141 142
  __int64 old;
  do {
    old = *ptr;
143
  } while (_InterlockedCompareExchange64(ptr, old ^ val, old) != old);
S
slguan 已提交
144
  return old;
145 146
#else
  return _InterlockedXor64((__int64 volatile*)(ptr), (__int64)(val));
S
slguan 已提交
147
#endif
148
}
S
slguan 已提交
149

S
Shengliang Guan 已提交
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
#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 已提交
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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894


int8_t atomic_load_8(int8_t volatile *ptr) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  ((*(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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  ((*(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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  ((*(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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  ((*(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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  ((*(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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  #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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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
}

void* atomic_add_fetch_ptr(void *ptr, int32_t val) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return interlocked_add_fetch_ptr((void* volatile*)(ptr), (void*)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_add_and_fetch((ptr), (val));
#else
  return __atomic_add_fetch((void **)(ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int8_t atomic_fetch_add_8(int8_t volatile *ptr, int8_t val) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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
}

void* atomic_fetch_add_ptr(void *ptr, int32_t val) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return _InterlockedExchangeAddptr((void* volatile*)(ptr), (void*)(val));
#elif defined(_TD_NINGSI_60)
  return __sync_fetch_and_add((ptr), (val));
#else
  return __atomic_fetch_add((void **)(ptr), (val), __ATOMIC_SEQ_CST);
#endif
}

int8_t atomic_sub_fetch_8(int8_t volatile *ptr, int8_t val) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return interlocked_add_fetch_32((int32_t volatile*)(ptr), -(int32_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
}

int64_t atomic_sub_fetch_64(int64_t volatile *ptr, int64_t val) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return interlocked_add_fetch_64((int64_t volatile*)(ptr), -(int64_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
}

void* atomic_sub_fetch_ptr(void *ptr, int32_t val) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return interlocked_add_fetch_ptr((void* volatile*)(ptr), -(void*)(val));
#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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return _InterlockedExchangeAdd((int32_t volatile*)(ptr), -(int32_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
}

int64_t atomic_fetch_sub_64(int64_t volatile *ptr, int64_t val) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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
}

void* atomic_fetch_sub_ptr(void *ptr, int32_t val) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return _InterlockedExchangeAddptr((void* volatile*)(ptr), -(void*)(val));
#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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  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
}