Skip to content

Commit

Permalink
feat: add task helpers (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThirdEyeSqueegee authored Oct 25, 2023
1 parent 13420b2 commit 71a91f6
Showing 1 changed file with 34 additions and 4 deletions.
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();
}
}

0 comments on commit 71a91f6

Please sign in to comment.