From 10cb2e8956b47343666550dda55996cd7cfa6fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Mon, 9 Jan 2023 22:10:08 +0100 Subject: Storage: call watcher callbacks without holding lock --- src/storage.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/storage.cpp b/src/storage.cpp index 9b49e96..2e948b0 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -367,15 +367,27 @@ void FilesystemStorage::inotifyWatch() event = (const struct inotify_event *) ptr; if (event->mask & IN_MOVED_TO) { - scoped_lock lock(watcherLock); - UUID type = watchMap[event->wd]; - if (auto mbid = UUID::fromString(event->name)) { - if (auto mbref = headRef(type, *mbid)) { - auto range = watchers.equal_range(type); - for (auto it = range.first; it != range.second; it++) - std::get<1>(it->second)(*mbid, *mbref); + vector> callbacks; + optional mbid; + optional mbref; + + { + // Copy relevant callbacks to temporary array, so they + // can be called without holding the watcherLock. + + scoped_lock lock(watcherLock); + UUID type = watchMap[event->wd]; + if ((mbid = UUID::fromString(event->name))) { + if ((mbref = headRef(type, *mbid))) { + auto range = watchers.equal_range(type); + for (auto it = range.first; it != range.second; it++) + callbacks.push_back(std::get<1>(it->second)); + } } } + + for (const auto & cb : callbacks) + cb(*mbid, *mbref); } } } -- cgit v1.2.3