Skip to content

Commit

Permalink
Add isMainThread check for rest of code
Browse files Browse the repository at this point in the history
  • Loading branch information
Atlinx committed Dec 30, 2023
1 parent 75ccc87 commit 54a5820
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
21 changes: 15 additions & 6 deletions addons/GDTask/GDTask.Delay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ static NextFramePromise()
TaskPool.RegisterSizeGetter(typeof(NextFramePromise), () => pool.Size);
}

bool isMainThread;
ulong frameCount;
CancellationToken cancellationToken;
GDTaskCompletionSourceCore<AsyncUnit> core;
Expand All @@ -273,7 +274,9 @@ public static IGDTaskSource Create(PlayerLoopTiming timing, CancellationToken ca
result = new NextFramePromise();
}

result.frameCount = GDTaskPlayerLoopAutoload.IsMainThread ? Engine.GetProcessFrames() : 0ul;
result.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread;
if (result.isMainThread)
result.frameCount = Engine.GetProcessFrames();
result.cancellationToken = cancellationToken;

TaskTracker.TrackActiveTask(result, 3);
Expand Down Expand Up @@ -319,7 +322,7 @@ public bool MoveNext()
return false;
}

if (frameCount == Engine.GetProcessFrames())
if (isMainThread && frameCount == Engine.GetProcessFrames())
{
return true;
}
Expand Down Expand Up @@ -348,6 +351,7 @@ static DelayFramePromise()
TaskPool.RegisterSizeGetter(typeof(DelayFramePromise), () => pool.Size);
}

bool isMainThread;
ulong initialFrame;
int delayFrameCount;
CancellationToken cancellationToken;
Expand All @@ -373,7 +377,9 @@ public static IGDTaskSource Create(int delayFrameCount, PlayerLoopTiming timing,

result.delayFrameCount = delayFrameCount;
result.cancellationToken = cancellationToken;
result.initialFrame = GDTaskPlayerLoopAutoload.IsMainThread ? Engine.GetProcessFrames() : 0ul;
result.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread;
if (result.isMainThread)
result.initialFrame = Engine.GetProcessFrames();

TaskTracker.TrackActiveTask(result, 3);

Expand Down Expand Up @@ -427,7 +433,7 @@ public bool MoveNext()
}

// skip in initial frame.
if (initialFrame == Engine.GetProcessFrames())
if (isMainThread && initialFrame == Engine.GetProcessFrames())
{
#if DEBUG
// force use Realtime.
Expand Down Expand Up @@ -476,6 +482,7 @@ static DelayPromise()
TaskPool.RegisterSizeGetter(typeof(DelayPromise), () => pool.Size);
}

bool isMainThread;
ulong initialFrame;
double delayTimeSpan;
double elapsed;
Expand All @@ -502,7 +509,9 @@ public static IGDTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming timi
result.elapsed = 0.0f;
result.delayTimeSpan = (float)delayTimeSpan.TotalSeconds;
result.cancellationToken = cancellationToken;
result.initialFrame = GDTaskPlayerLoopAutoload.IsMainThread ? Engine.GetProcessFrames() : 0ul;
result.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread;
if (result.isMainThread)
result.initialFrame = Engine.GetProcessFrames();

TaskTracker.TrackActiveTask(result, 3);

Expand Down Expand Up @@ -549,7 +558,7 @@ public bool MoveNext()

if (elapsed == 0.0f)
{
if (initialFrame == Engine.GetProcessFrames())
if (isMainThread && initialFrame == Engine.GetProcessFrames())
{
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion addons/GDTask/PlayerLoopTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ protected override void ResetCore(TimeSpan? interval)
{
this.elapsed = 0.0;
this.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread;
this.initialFrame = Engine.GetProcessFrames();
if (this.isMainThread)
this.initialFrame = Engine.GetProcessFrames();
if (interval != null)
{
this.interval = (float)interval.Value.TotalSeconds;
Expand Down

0 comments on commit 54a5820

Please sign in to comment.