Skip to content

Commit

Permalink
fix race condition where iterator can be invalidated before dereferen…
Browse files Browse the repository at this point in the history
…cing
  • Loading branch information
cwharris committed Apr 2, 2021
1 parent e71dbc1 commit b8580e2
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions cpp/src/jit/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,18 @@ jitify2::ProgramCache<>& get_program_cache(jitify2::PreprocessedProgramData prep
static std::mutex caches_mutex{};
static std::unordered_map<std::string, std::unique_ptr<jitify2::ProgramCache<>>> caches{};

std::lock_guard<std::mutex> caches_lock(caches_mutex);

auto existing_cache = caches.find(preprog.name());

if (existing_cache == caches.end()) {
// if the program cache was not found, lock and search for it again
std::lock_guard<std::mutex> caches_lock(caches_mutex);

existing_cache = caches.find(preprog.name());
auto res = caches.insert(
{preprog.name(),
std::make_unique<jitify2::ProgramCache<>>(100, preprog, nullptr, get_program_cache_dir())});

if (existing_cache == caches.end()) {
// if the program cache still wasn't found, we're first in line to create it.
auto res = caches.insert({preprog.name(),
std::make_unique<jitify2::ProgramCache<>>(
100, preprog, nullptr, get_program_cache_dir())});

existing_cache = res.first;
}
existing_cache = res.first;
}

// return the found / created cache.
return *(existing_cache->second);
}

Expand Down

0 comments on commit b8580e2

Please sign in to comment.