Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix a mutex use in ptr_cache #3555

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 29 additions & 28 deletions include/vapor/ptr_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,21 @@ namespace VAPoR {
// Note : 1) Key must support == operator and = operator.
// 2) BigObj must be able to be destructed by delete.
//
template<typename Key, typename BigObj, unsigned int Size, bool Query>
class ptr_cache {
template<typename Key, typename BigObj, unsigned int Size, bool Query> class ptr_cache {
public:

//
// Constructors and Destructor
//
ptr_cache() = default;
ptr_cache(const ptr_cache&) = delete;
ptr_cache(const ptr_cache&&) = delete;
ptr_cache& operator=(const ptr_cache&) = delete;
ptr_cache& operator=(const ptr_cache&&) = delete;
ptr_cache(const ptr_cache &) = delete;
ptr_cache(const ptr_cache &&) = delete;
ptr_cache &operator=(const ptr_cache &) = delete;
ptr_cache &operator=(const ptr_cache &&) = delete;
~ptr_cache()
{
for (auto& p : _element_vector) {
if (p.second)
delete p.second;
}
for (auto &p : _element_vector) {
if (p.second) delete p.second;
}
}

auto size() const -> size_t { return _element_vector.size(); }
Expand All @@ -64,19 +61,23 @@ class ptr_cache {
//
auto query(const Key &key) -> const BigObj *
{
// Only need to apply the mutex if `Query` is true.
if (Query) const std::lock_guard<std::mutex> lock_gd(_element_vector_mutex);

auto it = std::find_if(_element_vector.begin(), _element_vector.end(), [&key](element_type &e) { return e.first == key; });
if (it == _element_vector.end()) { // This key does not exist
return nullptr;
} else { // This key does exist
if (!Query) {
return it->second;
} else {
if (Query) {
// Use a guard lock if queries can change ordering.
const std::lock_guard<std::mutex> lock_gd(_element_vector_mutex);
auto it = std::find_if(_element_vector.begin(), _element_vector.end(), [&key](element_type &e) { return e.first == key; });
if (it == _element_vector.end()) // This key does not exist
return nullptr;
else { // This key does exist
std::rotate(_element_vector.begin(), it, it + 1);
return _element_vector.front().second;
}
} else {
// Just query, no change whatsoever.
auto it = std::find_if(_element_vector.begin(), _element_vector.end(), [&key](element_type &e) { return e.first == key; });
if (it == _element_vector.end()) // This key does not exist
return nullptr;
else // This key does exist
return it->second;
}
}

Expand All @@ -86,21 +87,21 @@ class ptr_cache {

auto it = std::find_if(_element_vector.begin(), _element_vector.end(), [&key](element_type &e) { return e.first == key; });

if (it == _element_vector.end()) { // This key does not exist
--it; // `it` points to the last element now.
if (it == _element_vector.end()) { // This key does not exist
--it; // `it` points to the last element now.
it->first = key;
}
if (it->second) // Destroy the old object held here.
delete it->second;
if (it->second) // Destroy the old object held here.
delete it->second;
it->second = ptr;
std::rotate(_element_vector.begin(), it, it + 1);
}

private:
using element_type = std::pair<Key, const BigObj*>;
using element_type = std::pair<Key, const BigObj *>;

std::mutex _element_vector_mutex;
std::array<element_type, Size> _element_vector;
std::mutex _element_vector_mutex;
std::array<element_type, Size> _element_vector;
};
} // namespace VAPoR

Expand Down