atomic.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* atomic.c -- Support for atomic functions if not present.
  2. Copyright (C) 2013-2021 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Google.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. (1) Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. (2) Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. (3) The name of the author may not be used to
  14. endorse or promote products derived from this software without
  15. specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE. */
  27. #include "config.h"
  28. #include <sys/types.h>
  29. #include "backtrace.h"
  30. #include "backtrace-supported.h"
  31. #include "internal.h"
  32. /* This file holds implementations of the atomic functions that are
  33. used if the host compiler has the sync functions but not the atomic
  34. functions, as is true of versions of GCC before 4.7. */
  35. #if !defined (HAVE_ATOMIC_FUNCTIONS) && defined (HAVE_SYNC_FUNCTIONS)
  36. /* Do an atomic load of a pointer. */
  37. void *
  38. backtrace_atomic_load_pointer (void *arg)
  39. {
  40. void **pp;
  41. void *p;
  42. pp = (void **) arg;
  43. p = *pp;
  44. while (!__sync_bool_compare_and_swap (pp, p, p))
  45. p = *pp;
  46. return p;
  47. }
  48. /* Do an atomic load of an int. */
  49. int
  50. backtrace_atomic_load_int (int *p)
  51. {
  52. int i;
  53. i = *p;
  54. while (!__sync_bool_compare_and_swap (p, i, i))
  55. i = *p;
  56. return i;
  57. }
  58. /* Do an atomic store of a pointer. */
  59. void
  60. backtrace_atomic_store_pointer (void *arg, void *p)
  61. {
  62. void **pp;
  63. void *old;
  64. pp = (void **) arg;
  65. old = *pp;
  66. while (!__sync_bool_compare_and_swap (pp, old, p))
  67. old = *pp;
  68. }
  69. /* Do an atomic store of a size_t value. */
  70. void
  71. backtrace_atomic_store_size_t (size_t *p, size_t v)
  72. {
  73. size_t old;
  74. old = *p;
  75. while (!__sync_bool_compare_and_swap (p, old, v))
  76. old = *p;
  77. }
  78. /* Do an atomic store of a int value. */
  79. void
  80. backtrace_atomic_store_int (int *p, int v)
  81. {
  82. size_t old;
  83. old = *p;
  84. while (!__sync_bool_compare_and_swap (p, old, v))
  85. old = *p;
  86. }
  87. #endif