Skip to content

Commit

Permalink
fix spinwait
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Jan 2, 2024
1 parent fb0491d commit 79fc42a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/Snap.Hutao/Snap.Hutao/Core/Threading/SpinWaitPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
11 changes: 8 additions & 3 deletions src/Snap.Hutao/Snap.Hutao/Service/Discord/DiscordController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ private struct DiscordAsyncAction
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)
{
Expand All @@ -255,7 +255,7 @@ public DiscordResult WaitUpdateActivity(DiscordActivity activity)
activityManagerPtr->update_activity(activityManagerPtr, &activity, actionPtr, &HandleResult);
}

SpinWaitPolyfill.SpinUntil(discordAsyncAction, (state) => state.IsCompleted);
SpinWaitPolyfill.SpinUntil(ref discordAsyncAction, &CheckActionCompleted);
return discordAsyncAction.Result;
}

Expand All @@ -266,5 +266,10 @@ private static void HandleResult(void* state, DiscordResult result)
action->Result = result;
action->IsCompleted = true;
}

private static bool CheckActionCompleted(ref readonly DiscordAsyncAction state)
{
return state.IsCompleted;
}
}
}

0 comments on commit 79fc42a

Please sign in to comment.