From 5dc467310ddebeae8dcb6262f5499f37382711ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Wed, 17 Feb 2021 22:15:27 +0100 Subject: WatchedHead object allowing to stop watching Head --- src/storage.h | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'src/storage.h') diff --git a/src/storage.h b/src/storage.h index b8d769c..7ed8cf6 100644 --- a/src/storage.h +++ b/src/storage.h @@ -39,7 +39,8 @@ public: virtual vector> headRefs(UUID type) const = 0; virtual UUID storeHead(UUID type, const Digest & dgst) = 0; virtual bool replaceHead(UUID type, UUID id, const Digest & old, const Digest & dgst) = 0; - virtual void watchHead(UUID type, const function &) = 0; + virtual int watchHead(UUID type, const function &) = 0; + virtual void unwatchHead(UUID type, int watchId) = 0; virtual optional> loadKey(const Digest &) const = 0; virtual void storeKey(const Digest &, const vector &) = 0; @@ -60,7 +61,8 @@ public: virtual vector> headRefs(UUID type) const override; virtual UUID storeHead(UUID type, const Digest & dgst) override; virtual bool replaceHead(UUID type, UUID id, const Digest & old, const Digest & dgst) override; - virtual void watchHead(UUID type, const function &) override; + virtual int watchHead(UUID type, const function &) override; + virtual void unwatchHead(UUID type, int watchId) override; virtual optional> loadKey(const Digest &) const override; virtual void storeKey(const Digest &, const vector &) override; @@ -83,7 +85,8 @@ private: std::thread watcherThread; int inotify = -1; int inotifyWakeup = -1; - unordered_multimap> watchers; + int nextWatcherId = 1; + unordered_multimap>> watchers; unordered_map watchMap; }; @@ -102,7 +105,8 @@ public: virtual vector> headRefs(UUID type) const override; virtual UUID storeHead(UUID type, const Digest & dgst) override; virtual bool replaceHead(UUID type, UUID id, const Digest & old, const Digest & dgst) override; - virtual void watchHead(UUID type, const function &) override; + virtual int watchHead(UUID type, const function &) override; + virtual void unwatchHead(UUID type, int watchId) override; virtual optional> loadKey(const Digest &) const override; virtual void storeKey(const Digest &, const vector &) override; @@ -113,7 +117,8 @@ private: unordered_map> keys; mutex watcherLock; - unordered_multimap> watchers; + int nextWatcherId = 1; + unordered_multimap>> watchers; }; class ChainStorage : public StorageBackend @@ -134,7 +139,8 @@ public: virtual vector> headRefs(UUID type) const override; virtual UUID storeHead(UUID type, const Digest & dgst) override; virtual bool replaceHead(UUID type, UUID id, const Digest & old, const Digest & dgst) override; - virtual void watchHead(UUID type, const function &) override; + virtual int watchHead(UUID type, const function &) override; + virtual void unwatchHead(UUID type, int watchId) override; virtual optional> loadKey(const Digest &) const override; virtual void storeKey(const Digest &, const vector &) override; @@ -142,6 +148,10 @@ public: private: shared_ptr storage; unique_ptr parent; + + mutex watcherLock; + int nextWatcherId = 1; + unordered_map> watchers; }; struct PartialStorage::Priv -- cgit v1.2.3