db_lock: add C++ helper class ScopeDatabaseLock

This commit is contained in:
Max Kellermann 2012-09-05 20:50:15 +02:00
parent 7102ed8026
commit 0240e75426
2 changed files with 21 additions and 11 deletions

View File

@ -250,11 +250,8 @@ SimpleDatabase::LookupDirectory(const char *uri) const
assert(root != NULL);
assert(uri != NULL);
db_lock();
struct directory *directory =
directory_lookup_directory(root, uri);
db_unlock();
return directory;
ScopeDatabaseLock protect;
return directory_lookup_directory(root, uri);
}
bool
@ -281,12 +278,10 @@ SimpleDatabase::Visit(const DatabaseSelection &selection,
!visit_directory(*directory, error_r))
return false;
db_lock();
bool ret = directory->Walk(selection.recursive, selection.filter,
visit_directory, visit_song, visit_playlist,
error_r);
db_unlock();
return ret;
ScopeDatabaseLock protect;
return directory->Walk(selection.recursive, selection.filter,
visit_directory, visit_song, visit_playlist,
error_r);
}
bool

View File

@ -81,4 +81,19 @@ db_unlock(void)
g_static_mutex_unlock(&db_mutex);
}
#ifdef __cplusplus
class ScopeDatabaseLock {
public:
ScopeDatabaseLock() {
db_lock();
}
~ScopeDatabaseLock() {
db_unlock();
}
};
#endif
#endif