@@ -187,7 +187,14 @@ class Object {
187187 * \return The usage count of the cell.
188188 * \note We use stl style naming to be consistent with known API in shared_ptr.
189189 */
190- int32_t use_count () const { return details::AtomicLoadRelaxed (&(header_.ref_counter )); }
190+ int32_t use_count () const {
191+ // only need relaxed load of counters
192+ #ifdef _MSC_VER
193+ return (reinterpret_cast <const volatile long *>(&header_.ref_counter ))[0 ]; // NOLINT(*)
194+ #else
195+ return __atomic_load_n (&(header_.ref_counter ), __ATOMIC_RELAXED);
196+ #endif
197+ }
191198
192199 // Information about the object
193200 static constexpr const char * _type_key = " object.Object" ;
@@ -220,15 +227,35 @@ class Object {
220227
221228 private:
222229 /* ! \brief increase reference count */
223- void IncRef () { details::AtomicIncrementRelaxed (&(header_.ref_counter )); }
230+ void IncRef () {
231+ #ifdef _MSC_VER
232+ _InterlockedIncrement (reinterpret_cast <volatile long *>(&header_.ref_counter )); // NOLINT(*)
233+ #else
234+ __atomic_fetch_add (&(header_.ref_counter ), 1 , __ATOMIC_RELAXED);
235+ #endif
236+ }
224237
225238 /* ! \brief decrease reference count and delete the object */
226239 void DecRef () {
227- if (details::AtomicDecrementRelAcq (&(header_.ref_counter )) == 1 ) {
240+ #ifdef _MSC_VER
241+ if (_InterlockedDecrement ( //
242+ reinterpret_cast <volatile long *>(&header_.ref_counter )) == 0 ) { // NOLINT(*)
243+ // full barrrier is implicit in InterlockedDecrement
244+ if (header_.deleter != nullptr ) {
245+ header_.deleter (&(this ->header_ ));
246+ }
247+ }
248+ #else
249+ // first do a release, note we only need to acquire for deleter
250+ if (__atomic_fetch_sub (&(header_.ref_counter ), 1 , __ATOMIC_RELEASE) == 1 ) {
251+ // only acquire when we need to call deleter
252+ // in this case we need to ensure all previous writes are visible
253+ __atomic_thread_fence (__ATOMIC_ACQUIRE);
228254 if (header_.deleter != nullptr ) {
229255 header_.deleter (&(this ->header_ ));
230256 }
231257 }
258+ #endif
232259 }
233260
234261 // friend classes
0 commit comments