mirror of
https://github.com/boostorg/container.git
synced 2026-02-23 15:42:17 +00:00
Fixes #211 ("Use atomics for pmr get/set default resource")
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user