From c76b1fc1f0c27c4b7d8dfe91650a435c32fbbded Mon Sep 17 00:00:00 2001 From: CristinaCristescu <bianca-cristina.cristescu@cern.ch> Date: Mon, 2 May 2016 16:53:14 +0200 Subject: [PATCH] Replace the llvm::thread_local with and atomic push. The atommic operation does the increment and the mod to the number of cache lines to ensure we are not trying to access memory out of the cache. --- interpreter/cling/lib/Utils/Validation.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/interpreter/cling/lib/Utils/Validation.cpp b/interpreter/cling/lib/Utils/Validation.cpp index 93ae3553d7e..89d09f6ae80 100644 --- a/interpreter/cling/lib/Utils/Validation.cpp +++ b/interpreter/cling/lib/Utils/Validation.cpp @@ -18,7 +18,8 @@ #include <array> #include <algorithm> #include <fcntl.h> -# include <unistd.h> +#include <unistd.h> +#include <atomic> #endif namespace cling { @@ -30,26 +31,25 @@ namespace cling { struct Cache { private: std::array<const void*, 8> lines; - unsigned mostRecent = 0; + std::atomic<unsigned> mostRecent = {0}; public: bool contains(const void* P) { return std::find(lines.begin(), lines.end(), P) != lines.end(); } void push(const void* P) { - mostRecent = (mostRecent + 1) % lines.size(); - lines[mostRecent] = P; + unsigned acquiredVal = mostRecent; + while(!mostRecent.compare_exchange_weak(acquiredVal, (acquiredVal+1)%lines.size())) { + acquiredVal = mostRecent; + } + lines[acquiredVal] = P; } }; // Trying to be thread-safe. // Each thread creates a new cache when needed. static Cache& getCache() { - static llvm::sys::ThreadLocal<Cache> threadCache; - if (!threadCache.get()) { - // Leak, 1 Cache/thread. - threadCache.set(new Cache()); - } - return *threadCache.get(); + static Cache threadCache; + return threadCache; } static int getNullDevFileDescriptor() { -- GitLab