atomic.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <stdlib.h>
  19. fspr_status_t fspr_atomic_init(fspr_pool_t *p)
  20. {
  21. return APR_SUCCESS;
  22. }
  23. fspr_uint32_t fspr_atomic_add32(volatile fspr_uint32_t *mem, fspr_uint32_t val)
  24. {
  25. fspr_uint32_t old, new_val;
  26. old = *mem; /* old is automatically updated on cs failure */
  27. do {
  28. new_val = old + val;
  29. } while (__cs(&old, (cs_t *)mem, new_val));
  30. return old;
  31. }
  32. void fspr_atomic_sub32(volatile fspr_uint32_t *mem, fspr_uint32_t val)
  33. {
  34. fspr_uint32_t old, new_val;
  35. old = *mem; /* old is automatically updated on cs failure */
  36. do {
  37. new_val = old - val;
  38. } while (__cs(&old, (cs_t *)mem, new_val));
  39. }
  40. fspr_uint32_t fspr_atomic_inc32(volatile fspr_uint32_t *mem)
  41. {
  42. return fspr_atomic_add32(mem, 1);
  43. }
  44. int fspr_atomic_dec32(volatile fspr_uint32_t *mem)
  45. {
  46. fspr_uint32_t old, new_val;
  47. old = *mem; /* old is automatically updated on cs failure */
  48. do {
  49. new_val = old - 1;
  50. } while (__cs(&old, (cs_t *)mem, new_val));
  51. return new_val != 0;
  52. }
  53. fspr_uint32_t fspr_atomic_read32(volatile fspr_uint32_t *mem)
  54. {
  55. return *mem;
  56. }
  57. void fspr_atomic_set32(volatile fspr_uint32_t *mem, fspr_uint32_t val)
  58. {
  59. *mem = val;
  60. }
  61. fspr_uint32_t fspr_atomic_cas32(volatile fspr_uint32_t *mem, fspr_uint32_t swap,
  62. fspr_uint32_t cmp)
  63. {
  64. fspr_uint32_t old = cmp;
  65. __cs(&old, (cs_t *)mem, swap);
  66. return old; /* old is automatically updated from mem on cs failure */
  67. }
  68. fspr_uint32_t fspr_atomic_xchg32(volatile fspr_uint32_t *mem, fspr_uint32_t val)
  69. {
  70. fspr_uint32_t old, new_val;
  71. old = *mem; /* old is automatically updated on cs failure */
  72. do {
  73. new_val = val;
  74. } while (__cs(&old, (cs_t *)mem, new_val));
  75. return old;
  76. }