@@ -7,17 +7,18 @@ namespace OpenDreamRuntime.Procs;
77
88public sealed partial class ProcScheduler {
99 private sealed class ThreadYieldTracker {
10- public uint threadYieldCounter = 1 ;
11- public Dictionary < int , uint > procYieldCounter = new ( ) ;
10+ public uint ThreadYieldCounter = 1 ;
11+ public readonly Dictionary < int , uint > ProcYieldCounter = new ( ) ;
1212 }
13+
1314 [ Dependency ] private readonly IOpenDreamGameTiming _gameTiming = default ! ;
1415
1516 private PriorityQueue < DelayTicker , uint > _tickers = new ( ) ;
1617
17- private Dictionary < int , ThreadYieldTracker > YieldTrackersByThread = new ( ) ;
18+ private readonly Dictionary < int , ThreadYieldTracker > _yieldTrackersByThread = new ( ) ;
1819
19- private readonly ushort YieldProcThreshold = 10 ;
20- private readonly ushort YieldThreadThreshold = 20 ;
20+ private readonly ushort _yieldProcThreshold = 10 ;
21+ private readonly ushort _yieldThreadThreshold = 20 ;
2122
2223 // This is for deferred tasks that need to fire in the current tick.
2324 private readonly Queue < TaskCompletionSource > _deferredTasks = new ( ) ;
@@ -29,6 +30,12 @@ private sealed class ThreadYieldTracker {
2930 /// <param name="deciseconds">
3031 /// The amount of time, in deciseconds, to sleep. Gets rounded down to a number of ticks.
3132 /// </param>
33+ /// <param name="procId">
34+ /// ID of active process to track sharing
35+ /// </param>
36+ /// <param name="threadId">
37+ /// ID of active thread to track sharing
38+ /// </param>
3239 public Task CreateDelay ( float deciseconds , int procId , int threadId ) {
3340 // BYOND stores sleep/spawn delays with an exact amount of ticks.
3441 // Yes, this means that if you change world.fps/tick_lag while sleeping,
@@ -48,6 +55,12 @@ public Task CreateDelay(float deciseconds, int procId, int threadId) {
4855 /// <param name="ticks">
4956 /// The amount of ticks to sleep.
5057 /// </param>
58+ /// <param name="procId">
59+ /// ID of active process to track sharing
60+ /// </param>
61+ /// <param name="threadId">
62+ /// ID of active thread to track sharing
63+ /// </param>
5164 public Task CreateDelayTicks ( int ticks , int procId , int threadId ) {
5265 // When the delay is <= zero, we should run again in the current tick.
5366 // Now, BYOND apparently does have a difference between 0 and -1. See https://github.com/OpenDreamProject/OpenDream/issues/1262#issuecomment-1563663041
@@ -57,27 +70,27 @@ public Task CreateDelayTicks(int ticks, int procId, int threadId) {
5770 // In simple testing, this tends to amount to the same as one thread sleeping with -1 twenty times in one tick or a single proc ten times.
5871
5972 if ( ticks < 0 ) {
60- var yieldTracker = YieldTrackersByThread . GetValueOrDefault ( threadId , new ( ) ) ;
61- uint sleepCountByThread = yieldTracker . threadYieldCounter ;
62- uint sleepCountByProc = yieldTracker . procYieldCounter . GetValueOrDefault ( procId , 1u ) ;
73+ var yieldTracker = _yieldTrackersByThread . GetValueOrDefault ( threadId , new ( ) ) ;
74+ uint sleepCountByThread = yieldTracker . ThreadYieldCounter ;
75+ uint sleepCountByProc = yieldTracker . ProcYieldCounter . GetValueOrDefault ( procId , 1u ) ;
6376
6477 bool exceeded = false ;
65- if ( sleepCountByThread < YieldThreadThreshold ) {
66- yieldTracker . threadYieldCounter = sleepCountByThread + 1u ;
78+ if ( sleepCountByThread < _yieldThreadThreshold ) {
79+ yieldTracker . ThreadYieldCounter = sleepCountByThread + 1u ;
6780 } else {
6881 exceeded = true ;
6982 }
7083
71- if ( sleepCountByProc < YieldProcThreshold ) {
72- yieldTracker . procYieldCounter [ procId ] = sleepCountByProc + 1u ;
84+ if ( sleepCountByProc < _yieldProcThreshold ) {
85+ yieldTracker . ProcYieldCounter [ procId ] = sleepCountByProc + 1u ;
7386 } else {
7487 exceeded = true ;
7588 }
7689
7790 if ( exceeded ) {
78- YieldTrackersByThread . Remove ( threadId ) ;
91+ _yieldTrackersByThread . Remove ( threadId ) ;
7992 } else {
80- YieldTrackersByThread [ threadId ] = yieldTracker ;
93+ _yieldTrackersByThread [ threadId ] = yieldTracker ;
8194 return Task . CompletedTask ;
8295 }
8396 }
@@ -104,7 +117,7 @@ private void InsertTask(DelayTicker ticker) {
104117 }
105118
106119 private void UpdateDelays ( ) {
107- YieldTrackersByThread . Clear ( ) ;
120+ _yieldTrackersByThread . Clear ( ) ;
108121
109122 while ( _tickers . Count > 0 ) {
110123 var ticker = _tickers . Peek ( ) ;
0 commit comments