[llvm] [OFFLOAD] Add support for indexed per-thread containers (PR #164263)

Alex Duran via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 23 01:13:01 PDT 2025


================
@@ -16,17 +16,93 @@
 #include <list>
 #include <memory>
 #include <mutex>
+#include <type_traits>
+
+template <typename ObjectType> struct PerThread {
+  struct PerThreadData {
+    std::unique_ptr<ObjectType> ThreadEntry;
+  };
+
+  std::mutex Mutex;
+  std::list<std::shared_ptr<PerThreadData>> ThreadDataList;
+
+  // define default constructors, disable copy and move constructors
+  PerThread() = default;
+  PerThread(const PerThread &) = delete;
+  PerThread(PerThread &&) = delete;
+  PerThread &operator=(const PerThread &) = delete;
+  PerThread &operator=(PerThread &&) = delete;
+  ~PerThread() {
+    std::lock_guard<std::mutex> Lock(Mutex);
+    ThreadDataList.clear();
----------------
adurang wrote:

You mean removing the lock here? I think we can do it here given that at this point there should only be one thread doing things... I was being a bit conservative. 
If you meant to remove the lock completely and move it up, I don't think we can do that because get requires fine-grain locking in general. But we could make locking optional so in case you already had a lock outside another is not used inside. 

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


More information about the llvm-commits mailing list