summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2023-01-08 18:39:31 +0100
committerRoman Smrž <roman.smrz@seznam.cz>2023-01-31 21:27:00 +0100
commited40a224e4256867d5f8c9f7c4c3ea998d4b88ce (patch)
tree096723be59422b514a5a5bbf2ff2ee8eb1cdf704
parent10cb2e8956b47343666550dda55996cd7cfa6fbd (diff)
Test: fix shared state watcher reset
-rw-r--r--src/main.cpp19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/main.cpp b/src/main.cpp
index de4ffbc..12f46ac 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -323,8 +323,15 @@ void sharedStateGet(const vector<string> &)
void sharedStateWait(const vector<string> & args)
{
- static optional<Watched<vector<Ref>>> watched;
- watched = h->behavior().lens<vector<Ref>>().watch([args, watched = watched] (const vector<Ref> & refs) mutable {
+ struct SharedStateWait
+ {
+ mutex lock;
+ bool done { false };
+ optional<Watched<vector<Ref>>> watched;
+ };
+ auto watchedPtr = make_shared<SharedStateWait>();
+
+ auto watched = h->behavior().lens<vector<Ref>>().watch([args, watchedPtr] (const vector<Ref> & refs) {
vector<Stored<Object>> objs;
objs.reserve(refs.size());
for (const auto & r : refs)
@@ -345,9 +352,15 @@ void sharedStateWait(const vector<string> & args)
ss << " " << a;
printLine(ss.str());
- watched = std::nullopt;
+ scoped_lock lock(watchedPtr->lock);
+ watchedPtr->done = true;
+ watchedPtr->watched = std::nullopt;
}
});
+
+ scoped_lock lock(watchedPtr->lock);
+ if (!watchedPtr->done)
+ watchedPtr->watched = move(watched);
}
void watchLocalIdentity(const vector<string> &)