[llvm] [OFFLOAD] Add support for indexed per-thread containers (PR #164263)
Kevin Sala Penades via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 21 10:49:11 PST 2025
================
@@ -14,101 +14,256 @@
#define OFFLOAD_PERTHREADTABLE_H
#include <list>
+#include <llvm/ADT/SmallVector.h>
+#include <llvm/Support/Error.h>
#include <memory>
#include <mutex>
+#include <type_traits>
+
+template <typename ObjectType> class PerThread {
+ std::mutex Mutex;
+ llvm::SmallVector<std::shared_ptr<ObjectType>> ThreadDataList;
+
+ ObjectType &getThreadData() {
+ static thread_local std::shared_ptr<ObjectType> ThreadData = nullptr;
+ if (!ThreadData) {
+ ThreadData = std::make_shared<ObjectType>();
+ std::lock_guard<std::mutex> Lock(Mutex);
+ ThreadDataList.push_back(ThreadData);
----------------
kevinsala wrote:
Are you referring to creating the object (and the smart pointer) in the emplace back statement? If so, that would move the allocation of the object within the mutex-protected region.
https://github.com/llvm/llvm-project/pull/164263
More information about the llvm-commits
mailing list