58-memory.patch 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. diff --git a/src/memory.cpp b/src/memory.cpp
  2. index 4b2a369..e318eee 100644
  3. --- a/src/memory.cpp
  4. +++ b/src/memory.cpp
  5. @@ -21,7 +21,11 @@
  6. # endif
  7. #endif
  8. +#if !defined(_LIBCPP_HAS_NO_THREADS)
  9. +#include <atomic>
  10. +#else
  11. #include "include/atomic_support.h"
  12. +#endif
  13. _LIBCPP_BEGIN_NAMESPACE_STD
  14. @@ -45,13 +49,21 @@ __shared_weak_count::~__shared_weak_count()
  15. void
  16. __shared_count::__add_shared() noexcept
  17. {
  18. +#ifdef _LIBCPP_HAS_NO_THREADS
  19. __libcpp_atomic_refcount_increment(__shared_owners_);
  20. +#else
  21. + ++__shared_owners_;
  22. +#endif
  23. }
  24. bool
  25. __shared_count::__release_shared() noexcept
  26. {
  27. +#ifdef _LIBCPP_HAS_NO_THREADS
  28. if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1)
  29. +#else
  30. + if (--__shared_owners_ == -1)
  31. +#endif
  32. {
  33. __on_zero_shared();
  34. return true;
  35. @@ -68,7 +80,11 @@ __shared_weak_count::__add_shared() noexcept
  36. void
  37. __shared_weak_count::__add_weak() noexcept
  38. {
  39. +#ifdef _LIBCPP_HAS_NO_THREADS
  40. __libcpp_atomic_refcount_increment(__shared_weak_owners_);
  41. +#else
  42. + ++__shared_weak_owners_;
  43. +#endif
  44. }
  45. void
  46. @@ -103,26 +119,42 @@ __shared_weak_count::__release_weak() noexcept
  47. // threads, and have them all get copied at once. The argument
  48. // also doesn't apply for __release_shared, because an outstanding
  49. // weak_ptr::lock() could read / modify the shared count.
  50. +#ifdef _LIBCPP_HAS_NO_THREADS
  51. if (__libcpp_atomic_load(&__shared_weak_owners_, _AO_Acquire) == 0)
  52. +#else
  53. + if (__shared_weak_owners_.load(memory_order_acquire) == 0)
  54. +#endif
  55. {
  56. // no need to do this store, because we are about
  57. // to destroy everything.
  58. //__libcpp_atomic_store(&__shared_weak_owners_, -1, _AO_Release);
  59. __on_zero_shared_weak();
  60. }
  61. +#ifdef _LIBCPP_HAS_NO_THREADS
  62. else if (__libcpp_atomic_refcount_decrement(__shared_weak_owners_) == -1)
  63. +#else
  64. + else if (--__shared_weak_owners_ == -1)
  65. +#endif
  66. __on_zero_shared_weak();
  67. }
  68. __shared_weak_count*
  69. __shared_weak_count::lock() noexcept
  70. {
  71. +#ifdef _LIBCPP_HAS_NO_THREADS
  72. long object_owners = __libcpp_atomic_load(&__shared_owners_);
  73. +#else
  74. + long object_owners = __shared_owners_.load();
  75. +#endif
  76. while (object_owners != -1)
  77. {
  78. +#ifdef _LIBCPP_HAS_NO_THREADS
  79. if (__libcpp_atomic_compare_exchange(&__shared_owners_,
  80. &object_owners,
  81. object_owners+1))
  82. +#else
  83. + if (__shared_owners_.compare_exchange_weak(object_owners, object_owners+1))
  84. +#endif
  85. return this;
  86. }
  87. return nullptr;