Changeset 8add15e0 in mainline
- Timestamp:
- 2019-06-27T11:15:35Z (6 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 04c0fc5
- Parents:
- bc73be3
- Location:
- uspace/lib/cpp
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/cpp/Makefile
rbc73be3 r8add15e0 47 47 src/mutex.cpp \ 48 48 src/new.cpp \ 49 src/refcount_obj.cpp \ 49 50 src/shared_mutex.cpp \ 50 51 src/stdexcept.cpp \ -
uspace/lib/cpp/include/__bits/memory/shared_payload.hpp
rbc73be3 r8add15e0 30 30 #define LIBCPP_BITS_MEMORY_SHARED_PAYLOAD 31 31 32 #include <__bits/refcount_obj.hpp> 32 33 #include <cinttypes> 33 34 #include <utility> … … 43 44 namespace std::aux 44 45 { 45 /**46 * At the moment we do not have atomics, change this47 * to std::atomic<long> once we do.48 */49 using refcount_t = long;50 51 46 /** 52 47 * This allows us to construct shared_ptr from … … 66 61 67 62 template<class T> 68 class shared_payload_base 63 class shared_payload_base: public aux::refcount_obj 69 64 { 70 65 public: 71 virtual void destroy() = 0;72 66 virtual T* get() const noexcept = 0; 73 67 74 68 virtual uint8_t* deleter() const noexcept = 0; 75 69 76 virtual void increment() noexcept = 0;77 virtual void increment_weak() noexcept = 0;78 virtual bool decrement() noexcept = 0;79 virtual bool decrement_weak() noexcept = 0;80 virtual refcount_t refs() const noexcept = 0;81 virtual refcount_t weak_refs() const noexcept = 0;82 virtual bool expired() const noexcept = 0;83 70 virtual shared_payload_base* lock() noexcept = 0; 84 71 … … 91 78 public: 92 79 shared_payload(T* ptr, D deleter = D{}) 93 : data_{ptr}, deleter_{deleter}, 94 refcount_{1}, weak_refcount_{1} 80 : data_{ptr}, deleter_{deleter} 95 81 { /* DUMMY BODY */ } 96 82 … … 98 84 shared_payload(Args&&... args) 99 85 : data_{new T{forward<Args>(args)...}}, 100 deleter_{} , refcount_{1}, weak_refcount_{1}86 deleter_{} 101 87 { /* DUMMY BODY */ } 102 88 … … 104 90 shared_payload(allocator_arg_t, Alloc alloc, Args&&... args) 105 91 : data_{alloc.allocate(1)}, 106 deleter_{} , refcount_{1}, weak_refcount_{1}92 deleter_{} 107 93 { 108 94 alloc.construct(data_, forward<Args>(args)...); … … 112 98 shared_payload(D deleter, Alloc alloc, Args&&... args) 113 99 : data_{alloc.allocate(1)}, 114 deleter_{deleter} , refcount_{1}, weak_refcount_{1}100 deleter_{deleter} 115 101 { 116 102 alloc.construct(data_, forward<Args>(args)...); … … 119 105 void destroy() override 120 106 { 121 if ( refs() == 0)107 if (this->refs() == 0) 122 108 { 123 109 if (data_) … … 127 113 } 128 114 129 if ( weak_refs() == 0)115 if (this->weak_refs() == 0) 130 116 delete this; 131 117 } … … 142 128 } 143 129 144 void increment() noexcept override145 {146 __atomic_add_fetch(&refcount_, 1, __ATOMIC_ACQ_REL);147 }148 149 void increment_weak() noexcept override150 {151 __atomic_add_fetch(&weak_refcount_, 1, __ATOMIC_ACQ_REL);152 }153 154 bool decrement() noexcept override155 {156 if (__atomic_sub_fetch(&refcount_, 1, __ATOMIC_ACQ_REL) == 0)157 {158 /**159 * First call to destroy() will delete the held object,160 * so it doesn't matter what the weak_refcount_ is,161 * but we added one and we need to remove it now.162 */163 decrement_weak();164 165 return true;166 }167 else168 return false;169 }170 171 bool decrement_weak() noexcept override172 {173 return __atomic_sub_fetch(&weak_refcount_, 1, __ATOMIC_ACQ_REL) == 0 && refs() == 0;174 }175 176 refcount_t refs() const noexcept override177 {178 return __atomic_load_n(&refcount_, __ATOMIC_RELAXED);179 }180 181 refcount_t weak_refs() const noexcept override182 {183 return __atomic_load_n(&weak_refcount_, __ATOMIC_RELAXED);184 }185 186 bool expired() const noexcept override187 {188 return refs() == 0;189 }190 191 130 shared_payload_base<T>* lock() noexcept override 192 131 { 193 refcount_t rfs = refs();132 refcount_t rfs = this->refs(); 194 133 while (rfs != 0L) 195 134 { 196 if (__atomic_compare_exchange_n(& refcount_, &rfs, rfs + 1,135 if (__atomic_compare_exchange_n(&this->refcount_, &rfs, rfs + 1, 197 136 true, __ATOMIC_RELAXED, 198 137 __ATOMIC_RELAXED)) … … 208 147 T* data_; 209 148 D deleter_; 210 211 /**212 * We're using a trick where refcount_ > 0213 * means weak_refcount_ has 1 added to it,214 * this makes it easier for weak_ptrs that215 * can't decrement the weak_refcount_ to216 * zero with shared_ptrs using this object.217 */218 refcount_t refcount_;219 refcount_t weak_refcount_;220 149 }; 221 150 }
Note:
See TracChangeset
for help on using the changeset viewer.