Skip to content
Snippets Groups Projects
Commit c76b1fc1 authored by Bianca Cristina Cristescu's avatar Bianca Cristina Cristescu Committed by Axel Naumann
Browse files

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.
parent d02cec94
No related branches found
No related tags found
No related merge requests found
...@@ -18,7 +18,8 @@ ...@@ -18,7 +18,8 @@
#include <array> #include <array>
#include <algorithm> #include <algorithm>
#include <fcntl.h> #include <fcntl.h>
# include <unistd.h> #include <unistd.h>
#include <atomic>
#endif #endif
namespace cling { namespace cling {
...@@ -30,26 +31,25 @@ namespace cling { ...@@ -30,26 +31,25 @@ namespace cling {
struct Cache { struct Cache {
private: private:
std::array<const void*, 8> lines; std::array<const void*, 8> lines;
unsigned mostRecent = 0; std::atomic<unsigned> mostRecent = {0};
public: public:
bool contains(const void* P) { bool contains(const void* P) {
return std::find(lines.begin(), lines.end(), P) != lines.end(); return std::find(lines.begin(), lines.end(), P) != lines.end();
} }
void push(const void* P) { void push(const void* P) {
mostRecent = (mostRecent + 1) % lines.size(); unsigned acquiredVal = mostRecent;
lines[mostRecent] = P; while(!mostRecent.compare_exchange_weak(acquiredVal, (acquiredVal+1)%lines.size())) {
acquiredVal = mostRecent;
}
lines[acquiredVal] = P;
} }
}; };
// Trying to be thread-safe. // Trying to be thread-safe.
// Each thread creates a new cache when needed. // Each thread creates a new cache when needed.
static Cache& getCache() { static Cache& getCache() {
static llvm::sys::ThreadLocal<Cache> threadCache; static Cache threadCache;
if (!threadCache.get()) { return threadCache;
// Leak, 1 Cache/thread.
threadCache.set(new Cache());
}
return *threadCache.get();
} }
static int getNullDevFileDescriptor() { static int getNullDevFileDescriptor() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment