diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2017-11-06 13:44:46 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2017-11-06 13:44:46 +0200 |
commit | 702c5756d5a2050327603cb9e507c839f928a7a3 (patch) | |
tree | 2886b831eba8de926d42882fad8d043a2400ad4a /odb/details/shared-ptr/base.ixx | |
parent | e3fe7cd2d1bc82a60445412986ea31d30e494a3d (diff) |
Make shared_base counter atomic in C++11 and up
Diffstat (limited to 'odb/details/shared-ptr/base.ixx')
-rw-r--r-- | odb/details/shared-ptr/base.ixx | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/odb/details/shared-ptr/base.ixx b/odb/details/shared-ptr/base.ixx index 64bc88c..16207e1 100644 --- a/odb/details/shared-ptr/base.ixx +++ b/odb/details/shared-ptr/base.ixx @@ -45,22 +45,38 @@ namespace odb inline void shared_base:: _inc_ref () { - counter_++; +#ifdef ODB_CXX11 + counter_.fetch_add (1, std::memory_order_relaxed); +#else + ++counter_; +#endif } inline bool shared_base:: _dec_ref () { - if (callback_ == 0) - return --counter_ == 0; - else - return _dec_ref_callback (); + // While there are ways to avoid acquire (which is unnecessary except + // when the counter drops to zero), for our use-cases we'd rather keep + // it simple. + // + return +#ifdef ODB_CXX11 + counter_.fetch_sub (1, std::memory_order_acq_rel) == 1 +#else + --counter_ == 0 +#endif + ? callback_ == 0 || callback_->zero_counter (callback_->arg) + : false; } inline std::size_t shared_base:: _ref_count () const { +#ifdef ODB_CXX11 + return counter_.load (std::memory_order_relaxed); +#else return counter_; +#endif } #ifdef ODB_CXX11 |