Skip to content

Commit

Permalink
Use shared_mutex and shared_lock
Browse files Browse the repository at this point in the history
Signed-off-by: Nghia Truong <[email protected]>
  • Loading branch information
ttnghia committed Jul 29, 2024
1 parent 79372f3 commit 8ba0763
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
6 changes: 3 additions & 3 deletions cpp/include/cudf/utilities/prefetch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <rmm/device_uvector.hpp>

#include <map>
#include <mutex>
#include <shared_mutex>
#include <string>
#include <string_view>

Expand Down Expand Up @@ -53,7 +53,7 @@ class prefetch_config {
* @param key The configuration key.
* @return The value of the configuration key.
*/
bool get(std::string_view key) const;
bool get(std::string_view key);
/**
* @brief Set the value of a configuration key.
*
Expand All @@ -73,7 +73,7 @@ class prefetch_config {
private:
prefetch_config() = default; //< Private constructor to enforce singleton pattern
std::map<std::string, bool> config_values; //< Map of configuration keys to values
std::mutex config_mtx; //< Mutex for thread-safe config access
std::shared_mutex config_mtx; //< Mutex for thread-safe config access
};

/**
Expand Down
11 changes: 6 additions & 5 deletions cpp/src/utilities/prefetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ prefetch_config& prefetch_config::instance()
return instance;
}

bool prefetch_config::get(std::string_view key) const
bool prefetch_config::get(std::string_view key)
{
std::scoped_lock lock(config_mtx);
// Default to not prefetching
if (config_values.find(key.data()) == config_values.end()) { return false; }
std::shared_lock<std::shared_mutex> lock(config_mtx);
if (config_values.find(key.data()) == config_values.end()) {
return false; // default to not prefetching
}
return config_values.at(key.data());
}

void prefetch_config::set(std::string_view key, bool value)
{
std::scoped_lock lock(config_mtx);
std::lock_guard<std::shared_mutex> lock(config_mtx);
config_values[key.data()] = value;
}

Expand Down

0 comments on commit 8ba0763

Please sign in to comment.