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

feat: add task helpers #198

Merged
merged 2 commits into from
Oct 25, 2023
Merged
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
38 changes: 34 additions & 4 deletions CommonLibSF/include/SFSE/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace SFSE::stl
template <class T, std::size_t Size = 5>
constexpr void write_thunk_call(const std::uintptr_t a_address) noexcept
{
SFSE::AllocTrampoline(14);
auto& trampoline = SFSE::GetTrampoline();
AllocTrampoline(14);
auto& trampoline = GetTrampoline();
T::func = trampoline.write_call<Size>(a_address, T::thunk);
}

Expand All @@ -35,8 +35,38 @@ namespace SFSE::stl
template <class T, std::size_t Size = 5>
constexpr void write_thunk_jump(const std::uintptr_t a_src) noexcept
{
SFSE::AllocTrampoline(14);
auto& trampoline = SFSE::GetTrampoline();
AllocTrampoline(14);
auto& trampoline = GetTrampoline();
T::func = trampoline.write_branch<Size>(a_src, T::thunk);
}

namespace detail
{
template <class T>
struct is_chrono_duration : std::false_type
{};

template <class Rep, class Duration>
struct is_chrono_duration<std::chrono::duration<Rep, Duration>> : std::true_type
{};

template <typename T>
concept is_duration = is_chrono_duration<T>::value;
}

void add_thread_task(const std::function<void()> a_fn, detail::is_duration auto a_wait_for = 0ms) noexcept
{
std::jthread([=] {
std::this_thread::sleep_for(a_wait_for);
GetTaskInterface()->AddTask(a_fn);
}).detach();
}

void add_thread_task_permanent(const std::function<void()> a_fn, detail::is_duration auto a_wait_for = 0ms) noexcept
{
std::jthread([=] {
std::this_thread::sleep_for(a_wait_for);
GetTaskInterface()->AddPermanentTask(a_fn);
}).detach();
}
}
Loading