Commit b8e59325 authored by Max Kellermann's avatar Max Kellermann

tag/{Tag,Builder}: use std::lock_guard

parent 1ea04cd8
...@@ -36,10 +36,10 @@ TagBuilder::TagBuilder(const Tag &other) noexcept ...@@ -36,10 +36,10 @@ TagBuilder::TagBuilder(const Tag &other) noexcept
{ {
items.reserve(other.num_items); items.reserve(other.num_items);
tag_pool_lock.lock(); const std::lock_guard<Mutex> protect(tag_pool_lock);
for (unsigned i = 0, n = other.num_items; i != n; ++i) for (unsigned i = 0, n = other.num_items; i != n; ++i)
items.push_back(tag_pool_dup_item(other.items[i])); items.push_back(tag_pool_dup_item(other.items[i]));
tag_pool_lock.unlock();
} }
TagBuilder::TagBuilder(Tag &&other) noexcept TagBuilder::TagBuilder(Tag &&other) noexcept
...@@ -66,10 +66,9 @@ TagBuilder::operator=(const TagBuilder &other) noexcept ...@@ -66,10 +66,9 @@ TagBuilder::operator=(const TagBuilder &other) noexcept
items = other.items; items = other.items;
/* increment the tag pool refcounters */ /* increment the tag pool refcounters */
tag_pool_lock.lock(); const std::lock_guard<Mutex> protect(tag_pool_lock);
for (auto i : items) for (auto i : items)
tag_pool_dup_item(i); tag_pool_dup_item(i);
tag_pool_lock.unlock();
return *this; return *this;
} }
...@@ -179,13 +178,12 @@ TagBuilder::Complement(const Tag &other) noexcept ...@@ -179,13 +178,12 @@ TagBuilder::Complement(const Tag &other) noexcept
items.reserve(items.size() + other.num_items); items.reserve(items.size() + other.num_items);
tag_pool_lock.lock(); const std::lock_guard<Mutex> protect(tag_pool_lock);
for (unsigned i = 0, n = other.num_items; i != n; ++i) { for (unsigned i = 0, n = other.num_items; i != n; ++i) {
TagItem *item = other.items[i]; TagItem *item = other.items[i];
if (!present[item->type]) if (!present[item->type])
items.push_back(tag_pool_dup_item(item)); items.push_back(tag_pool_dup_item(item));
} }
tag_pool_lock.unlock();
} }
inline void inline void
...@@ -197,9 +195,11 @@ TagBuilder::AddItemInternal(TagType type, StringView value) noexcept ...@@ -197,9 +195,11 @@ TagBuilder::AddItemInternal(TagType type, StringView value) noexcept
if (!f.IsNull()) if (!f.IsNull())
value = { f.data, f.size }; value = { f.data, f.size };
tag_pool_lock.lock(); TagItem *i;
auto i = tag_pool_get_item(type, value); {
tag_pool_lock.unlock(); const std::lock_guard<Mutex> protect(tag_pool_lock);
i = tag_pool_get_item(type, value);
}
free(f.data); free(f.data);
...@@ -229,9 +229,11 @@ TagBuilder::AddItem(TagType type, const char *value) noexcept ...@@ -229,9 +229,11 @@ TagBuilder::AddItem(TagType type, const char *value) noexcept
void void
TagBuilder::AddEmptyItem(TagType type) noexcept TagBuilder::AddEmptyItem(TagType type) noexcept
{ {
tag_pool_lock.lock(); TagItem *i;
auto i = tag_pool_get_item(type, ""); {
tag_pool_lock.unlock(); const std::lock_guard<Mutex> protect(tag_pool_lock);
i = tag_pool_get_item(type, "");
}
items.push_back(i); items.push_back(i);
} }
...@@ -239,10 +241,11 @@ TagBuilder::AddEmptyItem(TagType type) noexcept ...@@ -239,10 +241,11 @@ TagBuilder::AddEmptyItem(TagType type) noexcept
void void
TagBuilder::RemoveAll() noexcept TagBuilder::RemoveAll() noexcept
{ {
tag_pool_lock.lock(); {
for (auto i : items) const std::lock_guard<Mutex> protect(tag_pool_lock);
tag_pool_put_item(i); for (auto i : items)
tag_pool_lock.unlock(); tag_pool_put_item(i);
}
items.clear(); items.clear();
} }
......
...@@ -30,10 +30,11 @@ Tag::Clear() noexcept ...@@ -30,10 +30,11 @@ Tag::Clear() noexcept
duration = SignedSongTime::Negative(); duration = SignedSongTime::Negative();
has_playlist = false; has_playlist = false;
tag_pool_lock.lock(); {
for (unsigned i = 0; i < num_items; ++i) const std::lock_guard<Mutex> protect(tag_pool_lock);
tag_pool_put_item(items[i]); for (unsigned i = 0; i < num_items; ++i)
tag_pool_lock.unlock(); tag_pool_put_item(items[i]);
}
delete[] items; delete[] items;
items = nullptr; items = nullptr;
...@@ -48,10 +49,9 @@ Tag::Tag(const Tag &other) noexcept ...@@ -48,10 +49,9 @@ Tag::Tag(const Tag &other) noexcept
if (num_items > 0) { if (num_items > 0) {
items = new TagItem *[num_items]; items = new TagItem *[num_items];
tag_pool_lock.lock(); const std::lock_guard<Mutex> protect(tag_pool_lock);
for (unsigned i = 0; i < num_items; i++) for (unsigned i = 0; i < num_items; i++)
items[i] = tag_pool_dup_item(other.items[i]); items[i] = tag_pool_dup_item(other.items[i]);
tag_pool_lock.unlock();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment