Fix single-threaded build for targets without atomics
authorJonathan Wakely <jwakely@redhat.com>
Wed, 15 Aug 2018 09:56:33 +0000 (10:56 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 15 Aug 2018 09:56:33 +0000 (10:56 +0100)
* src/c++17/memory_resource.cc [!_GLIBCXX_HAS_GTHREADS]
(atomic_mem_res): Add unsynchronized definition for single-threaded.

From-SVN: r263554

libstdc++-v3/ChangeLog
libstdc++-v3/src/c++17/memory_resource.cc

index 182c0ed2ff39ec5857b42acb00e23a9d6cd2a74c..15d2eb5eee961c92f05ba0867819e6b8872fa968 100644 (file)
@@ -1,3 +1,8 @@
+2018-08-15  Jonathan Wakely  <jwakely@redhat.com>
+
+       * src/c++17/memory_resource.cc [!_GLIBCXX_HAS_GTHREADS]
+       (atomic_mem_res): Add unsynchronized definition for single-threaded.
+
 2018-08-14  Jonathan Wakely  <jwakely@redhat.com>
 
        PR libstdc++/86954
index bd8f32d931ec2b0dfa57bea3a6c89f4a4d2401a1..aa82813e645247f5000d67990023925db5e5b18e 100644 (file)
@@ -88,7 +88,7 @@ namespace pmr
 #if ATOMIC_POINTER_LOCK_FREE == 2
     using atomic_mem_res = atomic<memory_resource*>;
 # define _GLIBCXX_ATOMIC_MEM_RES_CAN_BE_CONSTANT_INITIALIZED
-#else
+#elif defined(_GLIBCXX_HAS_GTHREADS)
     // Can't use pointer-width atomics, define a type using a mutex instead:
     struct atomic_mem_res
     {
@@ -114,6 +114,26 @@ 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() const
+      {
+       return val;
+      }
+
+      memory_resource* exchange(memory_resource* r)
+      {
+       return std::exchange(val, r);
+      }
+    };
 #endif // ATOMIC_POINTER_LOCK_FREE == 2
 
 #ifdef _GLIBCXX_ATOMIC_MEM_RES_CAN_BE_CONSTANT_INITIALIZED