Skip to content

Commit

Permalink
Defuse throttled_func when it's accidentally engaged
Browse files Browse the repository at this point in the history
  • Loading branch information
lhecker committed Nov 22, 2024
1 parent 282670a commit caeca70
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
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
27 changes: 19 additions & 8 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,10 @@ namespace til
return _isPending.exchange(true, std::memory_order_relaxed);
}

std::tuple<> take()
void apply(const auto& func)
{
reset();
return {};
func();
}

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

Expand Down

0 comments on commit caeca70

Please sign in to comment.