fspr_atomic.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr.h"
  17. #include "fspr_atomic.h"
  18. #include "fspr_thread_mutex.h"
  19. APR_DECLARE(fspr_status_t) fspr_atomic_init(fspr_pool_t *p)
  20. {
  21. return APR_SUCCESS;
  22. }
  23. /*
  24. * Remapping function pointer type to accept fspr_uint32_t's type-safely
  25. * as the arguments for as our fspr_atomic_foo32 Functions
  26. */
  27. #if (_MSC_VER < 1800)
  28. typedef WINBASEAPI fspr_uint32_t (WINAPI * fspr_atomic_win32_ptr_fn)
  29. (fspr_uint32_t volatile *);
  30. typedef WINBASEAPI fspr_uint32_t (WINAPI * fspr_atomic_win32_ptr_val_fn)
  31. (fspr_uint32_t volatile *,
  32. fspr_uint32_t);
  33. typedef WINBASEAPI fspr_uint32_t (WINAPI * fspr_atomic_win32_ptr_val_val_fn)
  34. (fspr_uint32_t volatile *,
  35. fspr_uint32_t, fspr_uint32_t);
  36. typedef WINBASEAPI void * (WINAPI * fspr_atomic_win32_ptr_ptr_ptr_fn)
  37. (volatile void **,
  38. void *, const void *);
  39. #endif
  40. APR_DECLARE(fspr_uint32_t) fspr_atomic_add32(volatile fspr_uint32_t *mem, fspr_uint32_t val)
  41. {
  42. #if (defined(_M_IA64) || defined(_M_AMD64))
  43. return InterlockedExchangeAdd(mem, val);
  44. #elif (_MSC_VER >= 1800)
  45. return InterlockedExchangeAdd(mem, val);
  46. #else
  47. return ((fspr_atomic_win32_ptr_val_fn)InterlockedExchangeAdd)(mem, val);
  48. #endif
  49. }
  50. /* Of course we want the 2's compliment of the unsigned value, val */
  51. #pragma warning(disable: 4146)
  52. APR_DECLARE(void) fspr_atomic_sub32(volatile fspr_uint32_t *mem, fspr_uint32_t val)
  53. {
  54. #if (defined(_M_IA64) || defined(_M_AMD64))
  55. InterlockedExchangeAdd(mem, -val);
  56. #elif (_MSC_VER >= 1800)
  57. InterlockedExchangeAdd(mem, -val);
  58. #else
  59. ((fspr_atomic_win32_ptr_val_fn)InterlockedExchangeAdd)(mem, -val);
  60. #endif
  61. }
  62. APR_DECLARE(fspr_uint32_t) fspr_atomic_inc32(volatile fspr_uint32_t *mem)
  63. {
  64. /* we return old value, win32 returns new value :( */
  65. #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
  66. return InterlockedIncrement(mem) - 1;
  67. #elif (_MSC_VER >= 1800)
  68. return InterlockedIncrement(mem) - 1;
  69. #else
  70. return ((fspr_atomic_win32_ptr_fn)InterlockedIncrement)(mem) - 1;
  71. #endif
  72. }
  73. APR_DECLARE(int) fspr_atomic_dec32(volatile fspr_uint32_t *mem)
  74. {
  75. #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
  76. return InterlockedDecrement(mem);
  77. #elif (_MSC_VER >= 1800)
  78. return InterlockedDecrement(mem);
  79. #else
  80. return ((fspr_atomic_win32_ptr_fn)InterlockedDecrement)(mem);
  81. #endif
  82. }
  83. APR_DECLARE(void) fspr_atomic_set32(volatile fspr_uint32_t *mem, fspr_uint32_t val)
  84. {
  85. #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
  86. InterlockedExchange(mem, val);
  87. #elif (_MSC_VER >= 1800)
  88. InterlockedExchange(mem, val);
  89. #else
  90. ((fspr_atomic_win32_ptr_val_fn)InterlockedExchange)(mem, val);
  91. #endif
  92. }
  93. APR_DECLARE(fspr_uint32_t) fspr_atomic_read32(volatile fspr_uint32_t *mem)
  94. {
  95. return *mem;
  96. }
  97. APR_DECLARE(fspr_uint32_t) fspr_atomic_cas32(volatile fspr_uint32_t *mem, fspr_uint32_t with,
  98. fspr_uint32_t cmp)
  99. {
  100. #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
  101. return InterlockedCompareExchange(mem, with, cmp);
  102. #elif (_MSC_VER >= 1800)
  103. return InterlockedCompareExchange(mem, with, cmp);
  104. #else
  105. return ((fspr_atomic_win32_ptr_val_val_fn)InterlockedCompareExchange)(mem, with, cmp);
  106. #endif
  107. }
  108. APR_DECLARE(void *) fspr_atomic_casptr(volatile void **mem, void *with, const void *cmp)
  109. {
  110. #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
  111. return InterlockedCompareExchangePointer(mem, with, cmp);
  112. #elif (_MSC_VER >= 1800)
  113. return InterlockedCompareExchangePointer(mem, with, cmp);
  114. #else
  115. /* Too many VC6 users have stale win32 API files, stub this */
  116. return ((fspr_atomic_win32_ptr_ptr_ptr_fn)InterlockedCompareExchange)(mem, with, cmp);
  117. #endif
  118. }
  119. APR_DECLARE(fspr_uint32_t) fspr_atomic_xchg32(volatile fspr_uint32_t *mem, fspr_uint32_t val)
  120. {
  121. #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
  122. return InterlockedExchange(mem, val);
  123. #elif (_MSC_VER >= 1800)
  124. return InterlockedExchange(mem, val);
  125. #else
  126. return ((fspr_atomic_win32_ptr_val_fn)InterlockedExchange)(mem, val);
  127. #endif
  128. }