14-has-no-threads.patch 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. diff --git a/include/__memory/shared_ptr.h b/include/__memory/shared_ptr.h
  2. index d9ddb8a..4b9da00 100644
  3. --- a/include/__memory/shared_ptr.h
  4. +++ b/include/__memory/shared_ptr.h
  5. @@ -54,7 +54,7 @@
  6. #include <stdexcept>
  7. #include <typeinfo>
  8. #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
  9. -# include <__atomic/memory_order.h>
  10. +# include <atomic>
  11. #endif
  12. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  13. @@ -149,7 +149,12 @@ class _LIBCPP_EXPORTED_FROM_ABI __shared_count
  14. __shared_count& operator=(const __shared_count&);
  15. protected:
  16. - long __shared_owners_;
  17. +#ifdef _LIBCPP_HAS_NO_THREADS
  18. + typedef long __atomic_count;
  19. +#else
  20. + typedef atomic<long> __atomic_count;
  21. +#endif
  22. + __atomic_count __shared_owners_;
  23. virtual ~__shared_count();
  24. private:
  25. virtual void __on_zero_shared() _NOEXCEPT = 0;
  26. @@ -165,11 +170,19 @@ public:
  27. #else
  28. _LIBCPP_INLINE_VISIBILITY
  29. void __add_shared() _NOEXCEPT {
  30. +#ifdef _LIBCPP_HAS_NO_THREADS
  31. __libcpp_atomic_refcount_increment(__shared_owners_);
  32. +#else
  33. + __shared_owners_++;
  34. +#endif
  35. }
  36. _LIBCPP_INLINE_VISIBILITY
  37. bool __release_shared() _NOEXCEPT {
  38. +#ifdef _LIBCPP_HAS_NO_THREADS
  39. if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
  40. +#else
  41. + if (--__shared_owners_ == -1) {
  42. +#endif
  43. __on_zero_shared();
  44. return true;
  45. }
  46. @@ -178,14 +191,23 @@ public:
  47. #endif
  48. _LIBCPP_INLINE_VISIBILITY
  49. long use_count() const _NOEXCEPT {
  50. +#ifdef _LIBCPP_HAS_NO_THREADS
  51. return __libcpp_relaxed_load(&__shared_owners_) + 1;
  52. +#else
  53. + return __shared_owners_.load(memory_order_relaxed) + 1;
  54. +#endif
  55. }
  56. };
  57. class _LIBCPP_EXPORTED_FROM_ABI __shared_weak_count
  58. : private __shared_count
  59. {
  60. - long __shared_weak_owners_;
  61. +#ifdef _LIBCPP_HAS_NO_THREADS
  62. + typedef long __atomic_count;
  63. +#else
  64. + typedef atomic<long> __atomic_count;
  65. +#endif
  66. + __atomic_count __shared_weak_owners_;
  67. public:
  68. _LIBCPP_INLINE_VISIBILITY
  69. @@ -207,7 +229,11 @@ public:
  70. }
  71. _LIBCPP_INLINE_VISIBILITY
  72. void __add_weak() _NOEXCEPT {
  73. +#ifdef _LIBCPP_HAS_NO_THREADS
  74. __libcpp_atomic_refcount_increment(__shared_weak_owners_);
  75. +#else
  76. + __shared_weak_owners_++;
  77. +#endif
  78. }
  79. _LIBCPP_INLINE_VISIBILITY
  80. void __release_shared() _NOEXCEPT {