ia32.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_arch_atomic.h"
  17. #ifdef USE_ATOMICS_IA32
  18. APR_DECLARE(apr_status_t) apr_atomic_init(apr_pool_t *p)
  19. {
  20. return APR_SUCCESS;
  21. }
  22. APR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem)
  23. {
  24. return *mem;
  25. }
  26. APR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val)
  27. {
  28. *mem = val;
  29. }
  30. APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val)
  31. {
  32. asm volatile ("lock; xaddl %0,%1"
  33. : "=r" (val), "=m" (*mem)
  34. : "0" (val), "m" (*mem)
  35. : "memory", "cc");
  36. return val;
  37. }
  38. APR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val)
  39. {
  40. asm volatile ("lock; subl %1, %0"
  41. : /* no output */
  42. : "m" (*(mem)), "r" (val)
  43. : "memory", "cc");
  44. }
  45. APR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem)
  46. {
  47. return apr_atomic_add32(mem, 1);
  48. }
  49. APR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem)
  50. {
  51. unsigned char prev;
  52. asm volatile ("lock; decl %0; setnz %1"
  53. : "=m" (*mem), "=qm" (prev)
  54. : "m" (*mem)
  55. : "memory");
  56. return prev;
  57. }
  58. APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with,
  59. apr_uint32_t cmp)
  60. {
  61. apr_uint32_t prev;
  62. asm volatile ("lock; cmpxchgl %1, %2"
  63. : "=a" (prev)
  64. : "r" (with), "m" (*(mem)), "0"(cmp)
  65. : "memory", "cc");
  66. return prev;
  67. }
  68. APR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val)
  69. {
  70. apr_uint32_t prev = val;
  71. asm volatile ("xchgl %0, %1"
  72. : "=r" (prev), "+m" (*mem)
  73. : "0" (prev));
  74. return prev;
  75. }
  76. APR_DECLARE(void*) apr_atomic_casptr(volatile void **mem, void *with, const void *cmp)
  77. {
  78. void *prev;
  79. #if APR_SIZEOF_VOIDP == 4
  80. asm volatile ("lock; cmpxchgl %2, %1"
  81. : "=a" (prev), "=m" (*mem)
  82. : "r" (with), "m" (*mem), "0" (cmp));
  83. #elif APR_SIZEOF_VOIDP == 8
  84. asm volatile ("lock; cmpxchgq %q2, %1"
  85. : "=a" (prev), "=m" (*mem)
  86. : "r" ((unsigned long)with), "m" (*mem),
  87. "0" ((unsigned long)cmp));
  88. #else
  89. #error APR_SIZEOF_VOIDP value not supported
  90. #endif
  91. return prev;
  92. }
  93. APR_DECLARE(void*) apr_atomic_xchgptr(volatile void **mem, void *with)
  94. {
  95. void *prev;
  96. #if APR_SIZEOF_VOIDP == 4
  97. asm volatile ("xchgl %2, %1"
  98. : "=a" (prev), "+m" (*mem)
  99. : "0" (with));
  100. #elif APR_SIZEOF_VOIDP == 8
  101. asm volatile ("xchgq %q2, %1"
  102. : "=a" (prev), "+m" (*mem)
  103. : "r" ((unsigned long)with));
  104. #else
  105. #error APR_SIZEOF_VOIDP value not supported
  106. #endif
  107. return prev;
  108. }
  109. #endif /* USE_ATOMICS_IA32 */