// The default memory resource
/// @cond undocumented
- inline std::atomic<memory_resource*>&
+ inline auto&
__get_default_resource()
{
+#ifndef _GLIBCXX_HAS_GTHREADS
+ struct type {
+ using value_type = memory_resource*;
+ explicit type(value_type __r) : _M_r(__r) { }
+ value_type _M_r;
+ value_type load() const { return _M_r; }
+ value_type exchange(value_type __r) { return std::__exchange(_M_r, __r); }
+ };
+#else
using type = atomic<memory_resource*>;
+#endif
alignas(type) static unsigned char __buf[sizeof(type)];
static type* __r = new(__buf) type(new_delete_resource());
return *__r;
#include <atomic>
#include <bit> // has_single_bit, bit_ceil, bit_width
#include <new>
+#include <bits/move.h> // std::__exchange
#if ATOMIC_POINTER_LOCK_FREE != 2
# include <bits/std_mutex.h> // std::mutex, std::lock_guard
-# include <bits/move.h> // std::__exchange
#endif
#if __has_cpp_attribute(clang::require_constant_initialization)
__constinit constant_init<newdel_res_t> newdel_res{};
__constinit constant_init<null_res_t> null_res{};
-#if ATOMIC_POINTER_LOCK_FREE == 2
- using atomic_mem_res = atomic<memory_resource*>;
+
+#ifndef _GLIBCXX_HAS_GTHREADS
# define _GLIBCXX_ATOMIC_MEM_RES_CAN_BE_CONSTANT_INITIALIZED
-#elif defined(_GLIBCXX_HAS_GTHREADS)
- // Can't use pointer-width atomics, define a type using a mutex instead:
+ // Single-threaded, no need for synchronization
struct atomic_mem_res
{
-# ifdef __GTHREAD_MUTEX_INIT
-# define _GLIBCXX_ATOMIC_MEM_RES_CAN_BE_CONSTANT_INITIALIZED
- // std::mutex has constexpr constructor
constexpr
-# endif
atomic_mem_res(memory_resource* r) : val(r) { }
- mutex mx;
memory_resource* val;
- memory_resource* load(std::memory_order)
+ memory_resource* load(std::memory_order) const
{
- lock_guard<mutex> lock(mx);
return val;
}
memory_resource* exchange(memory_resource* r, std::memory_order)
{
- lock_guard<mutex> lock(mx);
return std::__exchange(val, r);
}
};
-#else
+#elif ATOMIC_POINTER_LOCK_FREE == 2
+ using atomic_mem_res = atomic<memory_resource*>;
# define _GLIBCXX_ATOMIC_MEM_RES_CAN_BE_CONSTANT_INITIALIZED
- // Single-threaded, no need for synchronization
+#else
+ // Can't use pointer-width atomics, define a type using a mutex instead:
struct atomic_mem_res
{
+# ifdef __GTHREAD_MUTEX_INIT
+# define _GLIBCXX_ATOMIC_MEM_RES_CAN_BE_CONSTANT_INITIALIZED
+ // std::mutex has constexpr constructor
constexpr
+# endif
atomic_mem_res(memory_resource* r) : val(r) { }
+ mutex mx;
memory_resource* val;
- memory_resource* load(std::memory_order) const
+ memory_resource* load(std::memory_order)
{
+ lock_guard<mutex> lock(mx);
return val;
}
memory_resource* exchange(memory_resource* r, std::memory_order)
{
+ lock_guard<mutex> lock(mx);
return std::__exchange(val, r);
}
};
-#endif // ATOMIC_POINTER_LOCK_FREE == 2
+#endif
#ifdef _GLIBCXX_ATOMIC_MEM_RES_CAN_BE_CONSTANT_INITIALIZED
__constinit constant_init<atomic_mem_res> default_res{&newdel_res.obj};