Skip to content

Commit

Permalink
ObserverManager::tryWaitForAllUpdates and variants
Browse files Browse the repository at this point in the history
Summary: These may be used in the future to test for updates encountering deadlock in the `ObserverManager` threads.

Reviewed By: Gownta

Differential Revision: D46418861

fbshipit-source-id: 4d81934af2d1783ba7ddd0e1348575c56fa7cd65
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Jun 6, 2023
1 parent 954e688 commit ff7a464
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
10 changes: 6 additions & 4 deletions folly/experimental/observer/detail/ObserverManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,19 @@ void ObserverManager::scheduleNext(Function<Core::Ptr()> coreFunc) {
getUpdatesManager();
}

void ObserverManager::waitForAllUpdates() {
bool ObserverManager::tryWaitForAllUpdatesImpl(TryWaitForAllUpdatesImplOp op) {
if (auto updatesManager = getUpdatesManager()) {
return updatesManager->waitForAllUpdates();
return updatesManager->tryWaitForAllUpdatesImpl(op);
}
return true;
}

void ObserverManager::UpdatesManager::waitForAllUpdates() {
bool ObserverManager::UpdatesManager::tryWaitForAllUpdatesImpl(
TryWaitForAllUpdatesImplOp op) {
auto& instance = ObserverManager::getInstance();
nextQueueProcessor_->waitForEmpty();
// Wait for all readers to release the lock.
SharedMutexReadPriority::WriteHolder wh(instance.versionMutex_);
return op(instance.versionMutex_).owns_lock();
}

struct ObserverManager::Singleton {
Expand Down
27 changes: 25 additions & 2 deletions folly/experimental/observer/detail/ObserverManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,25 @@ class ObserverManager {
});
}

static void waitForAllUpdates();
static void waitForAllUpdates() {
tryWaitForAllUpdatesImpl([=](auto& m) { return make_unique_lock(m); });
}
static bool tryWaitForAllUpdates() {
return tryWaitForAllUpdatesImpl(
[=](auto& m) { return make_unique_lock(m, std::try_to_lock); });
}
template <typename Rep, typename Period>
static bool tryWaitForAllUpdatesFor(
std::chrono::duration<Rep, Period> timeout) {
return tryWaitForAllUpdatesImpl(
[=](auto& m) { return make_unique_lock(m, timeout); });
}
template <typename Clock, typename Duration>
static bool tryWaitForAllUpdatesUntil(
std::chrono::time_point<Clock, Duration> deadline) {
return tryWaitForAllUpdatesImpl(
[=](auto& m) { return make_unique_lock(m, deadline); });
}

class DependencyRecorder {
public:
Expand Down Expand Up @@ -186,15 +204,20 @@ class ObserverManager {
};

private:
using TryWaitForAllUpdatesImplOp =
FunctionRef<std::unique_lock<SharedMutexReadPriority>(
SharedMutexReadPriority&)>;
ObserverManager() {}

static bool tryWaitForAllUpdatesImpl(TryWaitForAllUpdatesImplOp op);

void scheduleCurrent(Function<void()>);
void scheduleNext(Function<Core::Ptr()>);

class UpdatesManager {
public:
UpdatesManager();
void waitForAllUpdates();
bool tryWaitForAllUpdatesImpl(TryWaitForAllUpdatesImplOp op);

private:
class CurrentQueueProcessor;
Expand Down

0 comments on commit ff7a464

Please sign in to comment.