apr_atomic.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "apr.h"
  17. #include "apr_atomic.h"
  18. #include "apr_thread_mutex.h"
  19. APR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p)
  20. {
  21. return APR_SUCCESS;
  22. }
  23. /*
  24. * Remapping function pointer type to accept apr_uint32_t's type-safely
  25. * as the arguments for as our apr_atomic_foo32 Functions
  26. */
  27. typedef WINBASEAPI apr_uint32_t (WINAPI * apr_atomic_win32_ptr_fn)
  28. (apr_uint32_t volatile *);
  29. typedef WINBASEAPI apr_uint32_t (WINAPI * apr_atomic_win32_ptr_val_fn)
  30. (apr_uint32_t volatile *,
  31. apr_uint32_t);
  32. typedef WINBASEAPI apr_uint32_t (WINAPI * apr_atomic_win32_ptr_val_val_fn)
  33. (apr_uint32_t volatile *,
  34. apr_uint32_t, apr_uint32_t);
  35. typedef WINBASEAPI void * (WINAPI * apr_atomic_win32_ptr_ptr_ptr_fn)
  36. (volatile void **,
  37. void *, const void *);
  38. typedef WINBASEAPI void * (WINAPI * apr_atomic_win32_ptr_ptr_fn)
  39. (volatile void **,
  40. void *);
  41. APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val)
  42. {
  43. #if (defined(_M_IA64) || defined(_M_AMD64))
  44. return InterlockedExchangeAdd(mem, val);
  45. #elif defined(__MINGW32__)
  46. return InterlockedExchangeAdd((long *)mem, val);
  47. #else
  48. return ((apr_atomic_win32_ptr_val_fn)InterlockedExchangeAdd)(mem, val);
  49. #endif
  50. }
  51. /* Of course we want the 2's compliment of the unsigned value, val */
  52. #ifdef _MSC_VER
  53. #pragma warning(disable: 4146)
  54. #endif
  55. APR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val)
  56. {
  57. #if (defined(_M_IA64) || defined(_M_AMD64))
  58. InterlockedExchangeAdd(mem, -val);
  59. #elif defined(__MINGW32__)
  60. InterlockedExchangeAdd((long *)mem, -val);
  61. #else
  62. ((apr_atomic_win32_ptr_val_fn)InterlockedExchangeAdd)(mem, -val);
  63. #endif
  64. }
  65. APR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem)
  66. {
  67. /* we return old value, win32 returns new value :( */
  68. #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
  69. return InterlockedIncrement(mem) - 1;
  70. #elif defined(__MINGW32__)
  71. return InterlockedIncrement((long *)mem) - 1;
  72. #else
  73. return ((apr_atomic_win32_ptr_fn)InterlockedIncrement)(mem) - 1;
  74. #endif
  75. }
  76. APR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem)
  77. {
  78. #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
  79. return InterlockedDecrement(mem);
  80. #elif defined(__MINGW32__)
  81. return InterlockedDecrement((long *)mem);
  82. #else
  83. return ((apr_atomic_win32_ptr_fn)InterlockedDecrement)(mem);
  84. #endif
  85. }
  86. APR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val)
  87. {
  88. #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
  89. InterlockedExchange(mem, val);
  90. #elif defined(__MINGW32__)
  91. InterlockedExchange((long*)mem, val);
  92. #else
  93. ((apr_atomic_win32_ptr_val_fn)InterlockedExchange)(mem, val);
  94. #endif
  95. }
  96. APR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem)
  97. {
  98. return *mem;
  99. }
  100. APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with,
  101. apr_uint32_t cmp)
  102. {
  103. #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
  104. return InterlockedCompareExchange(mem, with, cmp);
  105. #elif defined(__MINGW32__)
  106. return InterlockedCompareExchange((long*)mem, with, cmp);
  107. #else
  108. return ((apr_atomic_win32_ptr_val_val_fn)InterlockedCompareExchange)(mem, with, cmp);
  109. #endif
  110. }
  111. APR_DECLARE(void *) apr_atomic_casptr(volatile void **mem, void *with, const void *cmp)
  112. {
  113. #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
  114. return InterlockedCompareExchangePointer((void* volatile*)mem, with, (void*)cmp);
  115. #elif defined(__MINGW32__)
  116. return InterlockedCompareExchangePointer((void**)mem, with, (void*)cmp);
  117. #else
  118. /* Too many VC6 users have stale win32 API files, stub this */
  119. return ((apr_atomic_win32_ptr_ptr_ptr_fn)InterlockedCompareExchange)(mem, with, cmp);
  120. #endif
  121. }
  122. APR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val)
  123. {
  124. #if (defined(_M_IA64) || defined(_M_AMD64)) && !defined(RC_INVOKED)
  125. return InterlockedExchange(mem, val);
  126. #elif defined(__MINGW32__)
  127. return InterlockedExchange((long *)mem, val);
  128. #else
  129. return ((apr_atomic_win32_ptr_val_fn)InterlockedExchange)(mem, val);
  130. #endif
  131. }
  132. APR_DECLARE(void*) apr_atomic_xchgptr(volatile void **mem, void *with)
  133. {
  134. #if (defined(_M_IA64) || defined(_M_AMD64) || defined(__MINGW32__)) && !defined(RC_INVOKED)
  135. return InterlockedExchangePointer((void**)mem, with);
  136. #else
  137. /* Too many VC6 users have stale win32 API files, stub this */
  138. return ((apr_atomic_win32_ptr_ptr_fn)InterlockedExchange)(mem, with);
  139. #endif
  140. }