Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defuse throttled_func when it's accidentally engaged #18235

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cascadia/WinRTUtils/inc/ThrottledFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class ThrottledFunc : public std::enable_shared_from_this<ThrottledFunc<leading,
{
try
{
std::apply(self->_func, self->_storage.take());
self->_storage.apply(self->_func);
}
CATCH_LOG();
}
Expand Down
31 changes: 22 additions & 9 deletions src/inc/til/throttled_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@ namespace til
}
}

std::tuple<Args...> take()
void apply(const auto& func)
{
std::unique_lock guard{ _lock };
auto pendingRunArgs = std::move(*_pendingRunArgs);
_pendingRunArgs.reset();
return pendingRunArgs;
decltype(_pendingRunArgs) args;
{
std::unique_lock guard{ _lock };
args = std::move(_pendingRunArgs);
_pendingRunArgs.reset();
}
// Theoretically it should always have a value, because the throttled_func
// should not call the callback without there being a reason.
// But in practice a failure here was observed at least once.
// It's unknown to me what caused it, so the best we can do is avoid a crash.
assert(args.has_value());
if (args)
{
std::apply(func, *args);
}
}

explicit operator bool() const
Expand All @@ -60,10 +71,12 @@ namespace til
return _isPending.exchange(true, std::memory_order_relaxed);
}

std::tuple<> take()
void apply(const auto& func)
{
reset();
return {};
if (_isPending.exchange(false, std::memory_order_relaxed))
{
func();
}
}

void reset()
Expand Down Expand Up @@ -193,7 +206,7 @@ namespace til
}
else
{
std::apply(_func, _storage.take());
_storage.apply(_func);
}
}

Expand Down
Loading