ipa: raspberrypi: Switch to std::scoped_lock in the Metadata class

Replace std::lock_guard with std::scoped_lock. When locking a single
mutex, both are functionally the same. When locking two mutexes in the
operator= overload, the scoped_lock uses a deadlock avoidance algorithm
to avoid deadlock.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: David Plowman <david.plowman@raspberrypi.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
Naushir Patuck 2021-05-10 10:58:12 +01:00 committed by Laurent Pinchart
parent 340477f987
commit 7de2bbed9b

View file

@ -22,14 +22,14 @@ public:
template<typename T> template<typename T>
void Set(std::string const &tag, T const &value) void Set(std::string const &tag, T const &value)
{ {
std::lock_guard<std::mutex> lock(mutex_); std::scoped_lock lock(mutex_);
data_[tag] = value; data_[tag] = value;
} }
template<typename T> template<typename T>
int Get(std::string const &tag, T &value) const int Get(std::string const &tag, T &value) const
{ {
std::lock_guard<std::mutex> lock(mutex_); std::scoped_lock lock(mutex_);
auto it = data_.find(tag); auto it = data_.find(tag);
if (it == data_.end()) if (it == data_.end())
return -1; return -1;
@ -39,14 +39,13 @@ public:
void Clear() void Clear()
{ {
std::lock_guard<std::mutex> lock(mutex_); std::scoped_lock lock(mutex_);
data_.clear(); data_.clear();
} }
Metadata &operator=(Metadata const &other) Metadata &operator=(Metadata const &other)
{ {
std::lock_guard<std::mutex> lock(mutex_); std::scoped_lock lock(mutex_, other.mutex_);
std::lock_guard<std::mutex> other_lock(other.mutex_);
data_ = other.data_; data_ = other.data_;
return *this; return *this;
} }