mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
fix spinwait
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user