Fixes #211 ("Use atomics for pmr get/set default resource")

This commit is contained in:
Ion Gaztañaga
2022-07-25 01:28:58 +02:00
parent b8c59d595c
commit 231dd48e70
2 changed files with 29 additions and 2 deletions

View File

@ -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 (...)"]].

View File

@ -77,12 +77,13 @@ BOOST_CONTAINER_DECL memory_resource* null_memory_resource() BOOST_NOEXCEPT
return &boost::container::dtl::singleton_default<null_memory_resource_imp>::instance();
}
#if defined(BOOST_NO_CXX11_HDR_ATOMIC)
static memory_resource *default_memory_resource =
&boost::container::dtl::singleton_default<new_delete_resource_imp>::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 <atomic>
namespace boost {
namespace container {
namespace pmr {
static std::atomic<memory_resource*> default_memory_resource =
ATOMIC_VAR_INIT(&boost::container::dtl::singleton_default<new_delete_resource_imp>::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 {