fix spinwait

This commit is contained in:
DismissedLight
2024-01-02 18:45:35 +08:00
parent fb0491dc57
commit 79fc42aa3b
2 changed files with 12 additions and 5 deletions

View File

@@ -3,12 +3,14 @@
namespace Snap.Hutao.Core.Threading;
internal delegate bool SpinWaitPredicate<T>(ref readonly T state);
internal static class SpinWaitPolyfill
{
public static void SpinUntil<T>(T state, Func<T, bool> condition)
public static unsafe void SpinUntil<T>(ref T state, delegate*<ref readonly T, bool> condition)
{
SpinWait spinner = default;
while (!condition(state))
while (!condition(ref state))
{
spinner.SpinOnce();
}

View File

@@ -238,10 +238,10 @@ internal static class DiscordController
public bool IsCompleted;
}
private unsafe readonly struct DiscordUpdateActivityAsyncAction
private unsafe struct DiscordUpdateActivityAsyncAction
{
private readonly DiscordAsyncAction discordAsyncAction;
private readonly IDiscordActivityManager* activityManagerPtr;
private DiscordAsyncAction discordAsyncAction;
public DiscordUpdateActivityAsyncAction(IDiscordActivityManager* activityManagerPtr)
{
@@ -255,7 +255,7 @@ internal static class DiscordController
activityManagerPtr->update_activity(activityManagerPtr, &activity, actionPtr, &HandleResult);
}
SpinWaitPolyfill.SpinUntil(discordAsyncAction, (state) => state.IsCompleted);
SpinWaitPolyfill.SpinUntil(ref discordAsyncAction, &CheckActionCompleted);
return discordAsyncAction.Result;
}
@@ -266,5 +266,10 @@ internal static class DiscordController
action->Result = result;
action->IsCompleted = true;
}
private static bool CheckActionCompleted(ref readonly DiscordAsyncAction state)
{
return state.IsCompleted;
}
}
}