58-memory.patch 3.1 KB

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