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

Resolve circular reference in ThrottledFunc #9729

Merged
1 commit merged into from
Apr 6, 2021
Merged
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
1 change: 0 additions & 1 deletion src/cascadia/TerminalControl/TerminalControlLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
<ClCompile Include="TermControl.cpp">
<DependentUpon>TermControl.xaml</DependentUpon>
</ClCompile>
<ClCompile Include="ThrottledFunc.cpp" />
<ClCompile Include="TSFInputControl.cpp">
<DependentUpon>TSFInputControl.xaml</DependentUpon>
</ClCompile>
Expand Down
54 changes: 0 additions & 54 deletions src/cascadia/TerminalControl/ThrottledFunc.cpp

This file was deleted.

79 changes: 55 additions & 24 deletions src/cascadia/TerminalControl/ThrottledFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,7 @@ class ThrottledFunc : public std::enable_shared_from_this<ThrottledFunc<Args...>
}
}

_dispatcher.RunAsync(CoreDispatcherPriority::Low, [weakThis = this->weak_from_this()]() {
if (auto self{ weakThis.lock() })
{
DispatcherTimer timer;
timer.Interval(self->_delay);
timer.Tick([=](auto&&...) {
if (auto self{ weakThis.lock() })
{
timer.Stop();

std::optional<std::tuple<Args...>> args;
{
std::lock_guard guard{ self->_lock };
self->_pendingRunArgs.swap(args);
}
std::apply(self->_func, args.value());
}
});
timer.Start();
}
});
_Fire(_delay, _dispatcher, this->weak_from_this());
}

// Method Description:
Expand Down Expand Up @@ -110,12 +90,29 @@ class ThrottledFunc : public std::enable_shared_from_this<ThrottledFunc<Args...>
}

private:
static winrt::fire_and_forget _Fire(winrt::Windows::Foundation::TimeSpan delay, winrt::Windows::UI::Core::CoreDispatcher dispatcher, std::weak_ptr<ThrottledFunc> weakThis)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: I've tried using a lambda instead of a static function, but the code turned out to be about as ugly (due to the need to explicitly capture every member variable). I've chosen the static function as it at least ensures that the this pointer isn't accidentally used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds great to me. Good point on avoiding accidental usage.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So cool that we got to move it to the header!

{
co_await winrt::resume_after(delay);
co_await winrt::resume_foreground(dispatcher);

if (auto self{ weakThis.lock() })
{
std::optional<std::tuple<Args...>> args;
{
std::lock_guard guard{ self->_lock };
self->_pendingRunArgs.swap(args);
}

std::apply(self->_func, args.value());
}
}

Func _func;
winrt::Windows::Foundation::TimeSpan _delay;
winrt::Windows::UI::Core::CoreDispatcher _dispatcher;

std::optional<std::tuple<Args...>> _pendingRunArgs;
std::mutex _lock;
std::optional<std::tuple<Args...>> _pendingRunArgs;
};

// Class Description:
Expand All @@ -129,11 +126,45 @@ class ThrottledFunc<> : public std::enable_shared_from_this<ThrottledFunc<>>
public:
using Func = std::function<void()>;

ThrottledFunc(Func func, winrt::Windows::Foundation::TimeSpan delay, winrt::Windows::UI::Core::CoreDispatcher dispatcher);
ThrottledFunc(Func func, winrt::Windows::Foundation::TimeSpan delay, winrt::Windows::UI::Core::CoreDispatcher dispatcher) :
_func{ func },
_delay{ delay },
_dispatcher{ dispatcher }
{
}

void Run();
// Method Description:
// - Runs the function later, except if `Run` is called again before
// with a new argument, in which case the request will be ignored.
// - For more information, read the class' documentation.
// - This method is always thread-safe. It can be called multiple times on
// different threads.
// Arguments:
// - <none>
// Return Value:
// - <none>
template<typename... MakeArgs>
void Run(MakeArgs&&... args)
{
if (!_isRunPending.test_and_set(std::memory_order_relaxed))
{
_Fire(_delay, _dispatcher, this->weak_from_this());
}
}

private:
static winrt::fire_and_forget _Fire(winrt::Windows::Foundation::TimeSpan delay, winrt::Windows::UI::Core::CoreDispatcher dispatcher, std::weak_ptr<ThrottledFunc> weakThis)
{
co_await winrt::resume_after(delay);
co_await winrt::resume_foreground(dispatcher);

if (auto self{ weakThis.lock() })
{
self->_isRunPending.clear(std::memory_order_relaxed);
self->_func();
}
}

Func _func;
winrt::Windows::Foundation::TimeSpan _delay;
winrt::Windows::UI::Core::CoreDispatcher _dispatcher;
Expand Down