diff --git a/doc/container.qbk b/doc/container.qbk index 1dd3139..cc2a1e5 100644 --- a/doc/container.qbk +++ b/doc/container.qbk @@ -1342,6 +1342,7 @@ use [*Boost.Container]? There are several reasons for that: * Fixed bugs/issues: * [@https://github.com/boostorg/container/issues/209 GitHub #209: ['"Some boost warnings with my R package (Wclass-memaccess warnings with std::pair)"]]. + * [@https://github.com/boostorg/container/issues/211 GitHub #211: ['"Use atomics for pmr get/set default resource"]]. * [@https://github.com/boostorg/container/issues/210 GitHub #210: ['"Use sized delete in boost::container::new_allocator if __cpp_sized_deallocation is defined"]]. * [@https://github.com/boostorg/container/issues/221 GitHub #218: ['"small_vector static capacity is too small when not a multiple of 8 bytes"]]. * [@https://github.com/boostorg/container/issues/221 GitHub #221: ['"flat_set and friends should offer a const sequence_type& sequence() const method (...)"]]. diff --git a/src/global_resource.cpp b/src/global_resource.cpp index ca2dc68..bd8cc0d 100644 --- a/src/global_resource.cpp +++ b/src/global_resource.cpp @@ -77,12 +77,13 @@ BOOST_CONTAINER_DECL memory_resource* null_memory_resource() BOOST_NOEXCEPT return &boost::container::dtl::singleton_default::instance(); } +#if defined(BOOST_NO_CXX11_HDR_ATOMIC) + static memory_resource *default_memory_resource = &boost::container::dtl::singleton_default::instance(); BOOST_CONTAINER_DECL memory_resource* set_default_resource(memory_resource* r) BOOST_NOEXCEPT { - //TO-DO: synchronizes-with part using atomics if(dlmalloc_global_sync_lock()){ memory_resource *previous = default_memory_resource; if(!previous){ @@ -100,7 +101,6 @@ BOOST_CONTAINER_DECL memory_resource* set_default_resource(memory_resource* r) B BOOST_CONTAINER_DECL memory_resource* get_default_resource() BOOST_NOEXCEPT { - //TO-DO: synchronizes-with part using atomics if(dlmalloc_global_sync_lock()){ memory_resource *current = default_memory_resource; if(!current){ @@ -115,6 +115,32 @@ BOOST_CONTAINER_DECL memory_resource* get_default_resource() BOOST_NOEXCEPT } } +#else // #if defined(BOOST_NO_CXX11_HDR_ATOMIC) + +} //namespace pmr { +} //namespace container { +} //namespace boost { + +#include + +namespace boost { +namespace container { +namespace pmr { + +static std::atomic default_memory_resource = + ATOMIC_VAR_INIT(&boost::container::dtl::singleton_default::instance()); + +BOOST_CONTAINER_DECL memory_resource* set_default_resource(memory_resource* r) BOOST_NOEXCEPT +{ + memory_resource *const res = r ? r : new_delete_resource(); + return default_memory_resource.exchange(res, std::memory_order_acq_rel); +} + +BOOST_CONTAINER_DECL memory_resource* get_default_resource() BOOST_NOEXCEPT +{ return default_memory_resource.load(std::memory_order_acquire); } + +#endif + } //namespace pmr { } //namespace container { } //namespace boost {