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

This commit is contained in:
Max Kellermann 2018-01-20 19:45:18 +01:00
parent 1ea04cd8cc
commit b8e5932506
2 changed files with 25 additions and 22 deletions

View File

@ -36,10 +36,10 @@ TagBuilder::TagBuilder(const Tag &other) noexcept
{
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)
items.push_back(tag_pool_dup_item(other.items[i]));
tag_pool_lock.unlock();
}
TagBuilder::TagBuilder(Tag &&other) noexcept
@ -66,10 +66,9 @@ TagBuilder::operator=(const TagBuilder &other) noexcept
items = other.items;
/* increment the tag pool refcounters */
tag_pool_lock.lock();
const std::lock_guard<Mutex> protect(tag_pool_lock);
for (auto i : items)
tag_pool_dup_item(i);
tag_pool_lock.unlock();
return *this;
}
@ -179,13 +178,12 @@ TagBuilder::Complement(const Tag &other) noexcept
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) {
TagItem *item = other.items[i];
if (!present[item->type])
items.push_back(tag_pool_dup_item(item));
}
tag_pool_lock.unlock();
}
inline void
@ -197,9 +195,11 @@ TagBuilder::AddItemInternal(TagType type, StringView value) noexcept
if (!f.IsNull())
value = { f.data, f.size };
tag_pool_lock.lock();
auto i = tag_pool_get_item(type, value);
tag_pool_lock.unlock();
TagItem *i;
{
const std::lock_guard<Mutex> protect(tag_pool_lock);
i = tag_pool_get_item(type, value);
}
free(f.data);
@ -229,9 +229,11 @@ TagBuilder::AddItem(TagType type, const char *value) noexcept
void
TagBuilder::AddEmptyItem(TagType type) noexcept
{
tag_pool_lock.lock();
auto i = tag_pool_get_item(type, "");
tag_pool_lock.unlock();
TagItem *i;
{
const std::lock_guard<Mutex> protect(tag_pool_lock);
i = tag_pool_get_item(type, "");
}
items.push_back(i);
}
@ -239,10 +241,11 @@ TagBuilder::AddEmptyItem(TagType type) noexcept
void
TagBuilder::RemoveAll() noexcept
{
tag_pool_lock.lock();
for (auto i : items)
tag_pool_put_item(i);
tag_pool_lock.unlock();
{
const std::lock_guard<Mutex> protect(tag_pool_lock);
for (auto i : items)
tag_pool_put_item(i);
}
items.clear();
}

View File

@ -30,10 +30,11 @@ Tag::Clear() noexcept
duration = SignedSongTime::Negative();
has_playlist = false;
tag_pool_lock.lock();
for (unsigned i = 0; i < num_items; ++i)
tag_pool_put_item(items[i]);
tag_pool_lock.unlock();
{
const std::lock_guard<Mutex> protect(tag_pool_lock);
for (unsigned i = 0; i < num_items; ++i)
tag_pool_put_item(items[i]);
}
delete[] items;
items = nullptr;
@ -48,10 +49,9 @@ Tag::Tag(const Tag &other) noexcept
if (num_items > 0) {
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++)
items[i] = tag_pool_dup_item(other.items[i]);
tag_pool_lock.unlock();
}
}