Skip to content

Commit

Permalink
Fix possible spurious wake-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
knopp committed Oct 1, 2020
1 parent dad0cc3 commit d046e86
Showing 1 changed file with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
@interface FlutterResizeSynchronizer () {
uint32_t cookie; // counter to detect stale callbacks
std::mutex mutex;
std::condition_variable condRun;
std::condition_variable condBlock;
std::condition_variable condNoPendingCommit;
std::condition_variable condPendingCommit;
bool accepting;
bool waiting;
bool pendingCommit;
Expand Down Expand Up @@ -38,7 +38,8 @@ - (void)beginResize:(CGSize)size notify:(dispatch_block_t)notify {
accepting = false;

// let pending commits finish to unblock the raster thread
condRun.notify_all();
pendingCommit = false;
condNoPendingCommit.notify_all();

// let the engine send resize notification
notify();
Expand All @@ -47,13 +48,11 @@ - (void)beginResize:(CGSize)size notify:(dispatch_block_t)notify {

waiting = true;

condBlock.wait(lock);
condPendingCommit.wait(lock, [&] { return pendingCommit; });

if (pendingCommit) {
[delegate resizeSynchronizerCommit:self];
pendingCommit = false;
condRun.notify_all();
}
[delegate resizeSynchronizerCommit:self];
pendingCommit = false;
condNoPendingCommit.notify_all();

waiting = false;
}
Expand All @@ -76,21 +75,23 @@ - (void)requestCommit {

if (waiting) { // BeginResize is in progress, interrupt it and schedule commit call
pendingCommit = true;
condBlock.notify_all();
condRun.wait(lock);
condPendingCommit.notify_all();
condNoPendingCommit.wait(lock, [&]() { return !pendingCommit; });
} else {
// No resize, schedule commit on platform thread and wait until either done
// or interrupted by incoming BeginResize
pendingCommit = true;
dispatch_async(dispatch_get_main_queue(), [self, cookie_ = cookie] {
std::unique_lock<std::mutex> lock(mutex);
if (cookie_ == cookie) {
if (delegate) {
[delegate resizeSynchronizerCommit:self];
}
condRun.notify_all();
pendingCommit = false;
condNoPendingCommit.notify_all();
}
});
condRun.wait(lock);
condNoPendingCommit.wait(lock, [&]() { return !pendingCommit; });
}
}

Expand Down

0 comments on commit d046e86

Please sign in to comment.