public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Remove some more unconditional uses of atomics
Date: Thu, 14 Sep 2023 14:27:09 +0100	[thread overview]
Message-ID: <20230914132728.2165656-1-jwakely@redhat.com> (raw)

Tested aarch64-linux. Pushed to trunk.

-- >8 --

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  |  4 ++
 .../include/experimental/memory_resource      | 12 ++++-
 libstdc++-v3/src/c++17/default_resource.h     |  6 ++-
 libstdc++-v3/src/c++17/memory_resource.cc     | 49 ++++++++++---------
 4 files changed, 45 insertions(+), 26 deletions(-)

diff --git a/libstdc++-v3/include/experimental/io_context b/libstdc++-v3/include/experimental/io_context
index c59f8c8e73b..c878d5a7025 100644
--- a/libstdc++-v3/include/experimental/io_context
+++ b/libstdc++-v3/include/experimental/io_context
@@ -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;
diff --git a/libstdc++-v3/include/experimental/memory_resource b/libstdc++-v3/include/experimental/memory_resource
index 9f1cb42373e..6f419a0a929 100644
--- a/libstdc++-v3/include/experimental/memory_resource
+++ b/libstdc++-v3/include/experimental/memory_resource
@@ -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;
diff --git a/libstdc++-v3/src/c++17/default_resource.h b/libstdc++-v3/src/c++17/default_resource.h
index 522cee13b90..f8d03d7d3bc 100644
--- a/libstdc++-v3/src/c++17/default_resource.h
+++ b/libstdc++-v3/src/c++17/default_resource.h
@@ -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
 
diff --git a/libstdc++-v3/src/c++17/memory_resource.cc b/libstdc++-v3/src/c++17/memory_resource.cc
index c0c7cf0cf83..63856eadaf5 100644
--- a/libstdc++-v3/src/c++17/memory_resource.cc
+++ b/libstdc++-v3/src/c++17/memory_resource.cc
@@ -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,10 +94,31 @@ namespace pmr
 
     __constinit constant_init<newdel_res_t> newdel_res{};
     __constinit constant_init<null_res_t> null_res{};
-#if ATOMIC_POINTER_LOCK_FREE == 2
+
+#ifndef _GLIBCXX_HAS_GTHREADS
+# define _GLIBCXX_ATOMIC_MEM_RES_CAN_BE_CONSTANT_INITIALIZED
+    // Single-threaded, no need for synchronization
+    struct atomic_mem_res
+    {
+      constexpr
+      atomic_mem_res(memory_resource* r) : val(r) { }
+
+      memory_resource* val;
+
+      memory_resource* load(std::memory_order) const
+      {
+	return val;
+      }
+
+      memory_resource* exchange(memory_resource* r, std::memory_order)
+      {
+	return std::__exchange(val, r);
+      }
+    };
+#elif ATOMIC_POINTER_LOCK_FREE == 2
     using atomic_mem_res = atomic<memory_resource*>;
 # define _GLIBCXX_ATOMIC_MEM_RES_CAN_BE_CONSTANT_INITIALIZED
-#elif defined(_GLIBCXX_HAS_GTHREADS)
+#else
     // Can't use pointer-width atomics, define a type using a mutex instead:
     struct atomic_mem_res
     {
@@ -123,27 +144,7 @@ namespace pmr
 	return std::__exchange(val, r);
       }
     };
-#else
-# define _GLIBCXX_ATOMIC_MEM_RES_CAN_BE_CONSTANT_INITIALIZED
-    // Single-threaded, no need for synchronization
-    struct atomic_mem_res
-    {
-      constexpr
-      atomic_mem_res(memory_resource* r) : val(r) { }
-
-      memory_resource* val;
-
-      memory_resource* load(std::memory_order) const
-      {
-	return val;
-      }
-
-      memory_resource* exchange(memory_resource* r, std::memory_order)
-      {
-	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};
-- 
2.41.0


                 reply	other threads:[~2023-09-14 13:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230914132728.2165656-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).