]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Remove some more unconditional uses of atomics
authorJonathan Wakely <jwakely@redhat.com>
Tue, 12 Sep 2023 11:04:37 +0000 (12:04 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 14 Sep 2023 12:58:34 +0000 (13:58 +0100)
These atomics cause linker errors on arm4t where __sync_synchronize is
not defined. For single-threaded targets we don't need the atomics.

libstdc++-v3/ChangeLog:

* include/experimental/io_context (io_context) [!_GLIBCXX_HAS_GTHREADS]:
Use a plain integer for _M_work_count for single-threaded
targets.
* include/experimental/memory_resource (__get_default_resource)
[!_GLIBCXX_HAS_GTHREADS]: Use unsynchronized type for
single-threaded targets.
* src/c++17/default_resource.h: Adjust preprocessor conditions
to match memory_resource.cc.
* src/c++17/memory_resource.cc [!_GLIBCXX_HAS_GTHREADS]
(atomic_mem_res): Use unsynchronized type for single-threaded
targets.

libstdc++-v3/include/experimental/io_context
libstdc++-v3/include/experimental/memory_resource
libstdc++-v3/src/c++17/default_resource.h
libstdc++-v3/src/c++17/memory_resource.cc

index c59f8c8e73b3ecee690d3110fe64f66c422b6db1..c878d5a7025b925f0f17c22bc7a47f6b4c77d576 100644 (file)
@@ -562,7 +562,11 @@ inline namespace v1
        }
       };
 
+#ifdef _GLIBCXX_HAS_GTHREADS
     atomic<count_type>         _M_work_count;
+#else
+    count_type                 _M_work_count;
+#endif
     mutable execution_context::mutex_type              _M_mtx;
     queue<function<void()>>    _M_op;
     bool                       _M_stopped = false;
index 9f1cb42373eda34a1b1dd12c1828425315a4123d..6f419a0a929f982631bac6f2bfb32c9c4a1d8a43 100644 (file)
@@ -549,10 +549,20 @@ namespace pmr {
   // 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;
index 522cee13b90eb92d68ab345d556d111ccc6926a9..f8d03d7d3bcafb6662d25f70d760688aa0269d02 100644 (file)
@@ -2,7 +2,11 @@
 // to suppress the warning caused by using a reserved init_priority.
 #pragma GCC system_header
 
-#if ATOMIC_POINTER_LOCK_FREE == 2 || defined(__GTHREAD_MUTEX_INIT)
+#ifndef _GLIBCXX_HAS_GTHREADS
+# error "This file should not be included for this build"
+#elif ATOMIC_POINTER_LOCK_FREE == 2
+# error "This file should not be included for this build"
+#elif defined __GTHREAD_MUTEX_INIT
 # error "This file should not be included for this build"
 #endif
 
index c0c7cf0cf8325cbe0edd530c6d6ecd67e7228710..63856eadaf500b94febbbf55ede642cd440ac7b7 100644 (file)
@@ -27,9 +27,9 @@
 #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)
@@ -94,56 +94,57 @@ namespace pmr
 
     __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};