From c11345c4d92c5128c70830d480ed6e95c7393d58 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 15 Dec 2015 23:24:34 +0100 Subject: [PATCH] db/DatabaseLock: add class ScopeDatabaseUnlock --- src/db/DatabaseLock.hxx | 14 ++++++++++++++ src/db/plugins/simple/Directory.cxx | 14 ++++++-------- src/db/update/Editor.cxx | 5 ++--- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/db/DatabaseLock.hxx b/src/db/DatabaseLock.hxx index 253eb73fb..d6e8ae5ca 100644 --- a/src/db/DatabaseLock.hxx +++ b/src/db/DatabaseLock.hxx @@ -107,4 +107,18 @@ public: } }; +/** + * Unlock the database while in the current scope. + */ +class ScopeDatabaseUnlock { +public: + ScopeDatabaseUnlock() { + db_unlock(); + } + + ~ScopeDatabaseUnlock() { + db_lock(); + } +}; + #endif diff --git a/src/db/plugins/simple/Directory.cxx b/src/db/plugins/simple/Directory.cxx index 0c2b19efb..dbf7c54a9 100644 --- a/src/db/plugins/simple/Directory.cxx +++ b/src/db/plugins/simple/Directory.cxx @@ -232,14 +232,12 @@ Directory::Walk(bool recursive, const SongFilter *filter, /* TODO: eliminate this unlock/lock; it is necessary because the child's SimpleDatabasePlugin::Visit() call will lock it again */ - db_unlock(); - bool result = WalkMount(GetPath(), *mounted_database, - recursive, filter, - visit_directory, visit_song, - visit_playlist, - error); - db_lock(); - return result; + const ScopeDatabaseUnlock unlock; + return WalkMount(GetPath(), *mounted_database, + recursive, filter, + visit_directory, visit_song, + visit_playlist, + error); } if (visit_song) { diff --git a/src/db/update/Editor.cxx b/src/db/update/Editor.cxx index 90c3b3b45..c389e0603 100644 --- a/src/db/update/Editor.cxx +++ b/src/db/update/Editor.cxx @@ -36,15 +36,14 @@ DatabaseEditor::DeleteSong(Directory &dir, Song *del) /* first, prevent traversers in main task from getting this */ dir.RemoveSong(del); - db_unlock(); /* temporary unlock, because update_remove_song() blocks */ + /* temporary unlock, because update_remove_song() blocks */ + const ScopeDatabaseUnlock unlock; /* now take it out of the playlist (in the main_task) */ remove.Remove(del); /* finally, all possible references gone, free it */ del->Free(); - - db_lock(); } void