[libcxx-commits] [libcxx] [libc++] Implement P2545R4 Read-Copy Update (RCU) (PR #175451)

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Feb 28 10:59:02 PST 2026


================
@@ -0,0 +1,284 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___RCU_RCU_DOMAIN_H
+#define _LIBCPP___RCU_RCU_DOMAIN_H
+
+#include <__config>
+
+#include <__atomic/atomic.h>
+#include <__rcu/rcu_list.h>
+
+// todo replace with internal headers
+#include <atomic>
+#include <cstdint>
+#include <map>
+#include <mutex>
+#include <vector>
+
+// todo debug
+#include <cstdio>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_THREADS && _LIBCPP_HAS_EXPERIMENTAL_RCU
+
+template <class _ThreadLocal>
+class __thread_local_owner {
+  // todo put globals in experimental dylib
+  inline static thread_local map<const __thread_local_owner*, _ThreadLocal> __thread_local_instances;
+
+  // Keep track of all thread-local instances owned by this owner.
+  // Only emplaced the first time a thread is trying to access its thread-local instance.
+  vector<atomic_ref<_ThreadLocal>> __owned_instances_;
+  mutex __mtx_;
+
+  void __register(_ThreadLocal& __obj) {
+    lock_guard<std::mutex> __lg(__mtx_);
+    __owned_instances_.emplace_back(__obj);
+  }
+
+  // todo: deregister on thread exit?
+
+public:
+  __thread_local_owner()                       = default;
+  __thread_local_owner(__thread_local_owner&&) = delete;
+
+  atomic_ref<_ThreadLocal> __get_current_thread_instance() {
+    auto __it = __thread_local_instances.find(this);
+    if (__it == __thread_local_instances.end()) {
+      auto [new_it, _] = __thread_local_instances.try_emplace(this, _ThreadLocal());
+      auto& __obj      = new_it->second;
+      __register(__obj);
+      return atomic_ref(__obj);
+    }
+    return atomic_ref(__it->second);
+  }
+
+  template <class _Func>
+  void __for_each_owned_instances(_Func&& __f) {
+    unique_lock<std::mutex> __lock(__mtx_);
+    for (auto __instance : __owned_instances_) {
+      __f(__instance);
+    }
+  }
+};
+
+// Adopted the 2-phase implementation in the section
+// "3) General-Purpose RCU" of the paper
----------------
huixie90 wrote:

added design doc

https://github.com/llvm/llvm-project/pull/175451


More information about the libcxx-commits mailing list