SetPassword derives a full Argon2 hash, so the [password command, the admin gump, account creation and the XML import each cost ~8.9 ms of frozen world. Only the login verify had been moved. DRY-ing the two paths surfaced a correctness trap rather than just shared code. ApplyPasswordUpgrade guarded by comparing the stored hash, which is right for a login rehash -- do not clobber a newer password with a rehash of the one it superseded -- but wrong for an explicit change: two changes dispatched before either landed would drop the second and silently keep the older password. Ordering is now a per-account dispatch sequence claimed on the loop, which gives "newest wins" for both callers through one mechanism. SetPassword bumps it as well, so an inline write also supersedes an in-flight one. One job type serves both: PasswordJob carries an optional verify phrase and an optional hash phrase plus an OnComplete that runs on the loop, so a login verifies and may rehash while a password change only hashes. The class is PasswordWorker now, since verification no longer describes what it does. PasswordWorker.SetPassword is the single entry point and falls back to hashing inline when the gate is off or the queue is saturated -- unlike a login, a password change must never be silently dropped, and it is rare enough that the loop can absorb one. The [password confirmation moves into the callback, because off the loop it has not happened when the call returns. Account creation stays inline: it gates the login flow, so deferring it restructures the accept path.
9.5 KiB
| name | description |
|---|---|
| modernuo-threading | Trigger when discussing async patterns, world saves, game loop, or reviewing code for threading issues. When using await, Task, or any concurrency-related code in game logic. |
ModernUO Threading & Event Loop
When This Activates
- Reviewing code for threading issues
- Discussing async/await patterns
- Working with world saves
- Any mention of
Task.Run,Thread,lock,ConcurrentDictionary - Understanding the game loop
CRITICAL RULE: Single-Threaded Game Logic
ModernUO uses a single-threaded game loop. All game logic runs on one thread. There are NO exceptions for game code.
Forbidden in Game Code
// ALL of these are WRONG in Projects/UOContent/ code:
Task.Run(() => ProcessItems()); // Background thread
new Thread(BackgroundWork).Start(); // Manual thread
ThreadPool.QueueUserWorkItem(Work); // Thread pool
lock (_syncObj) { ... } // Locking
Monitor.Enter(obj); // Monitor
volatile int _counter; // Volatile
ConcurrentDictionary<int, Item> _items; // Concurrent collections
ConcurrentQueue<T> _queue; // Concurrent collections
Interlocked.Increment(ref _count); // Atomics
Mutex mutex; // OS mutex
Semaphore sem; // Semaphore
ReaderWriterLockSlim rwl; // RW lock
Why: The game loop is single-threaded. Concurrency primitives add overhead for no benefit, and background threads would cause data races with game state.
Why await Is Safe
EventLoopContext implements SynchronizationContext and routes all await continuations back to the main thread:
// This is SAFE in game code:
await Timer.Pause(TimeSpan.FromMilliseconds(100));
// Continuation runs on the game thread, not a thread pool thread
The flow:
awaitcapturesEventLoopContextas the synchronization context- When the awaited task completes, the continuation is posted to
EventLoopContext._queue LoopContext.ExecuteTasks()runs those continuations on the main thread during the next game loop tick
Game Loop Structure
// Simplified from Projects/Server/Main.cs
while (!Closing)
{
_tickCount = GetTimestamp();
_now = DateTime.UtcNow;
Mobile.ProcessDeltaQueue(); // Send mobile state changes to clients
Item.ProcessDeltaQueue(); // Send item state changes to clients
Timer.Slice(_tickCount); // Execute due timers
NetState.Slice(); // Process network I/O
LoopContext.ExecuteTasks(); // Run async continuations (Timer.Pause, etc.)
Timer.CheckTimerPool(); // Refill timer pool if needed
}
EventLoopContext Details
public sealed class EventLoopContext : SynchronizationContext
{
private readonly ConcurrentQueue<Action> _queue; // Normal tasks
private readonly ConcurrentQueue<Action> _priorityQueue; // High priority
private readonly int _maxPerFrame; // Default: 128
// Posts run on next ExecuteTasks() call
public void Post(Action d, Priority priority = Priority.Normal);
// Send blocks if called from another thread, immediate if on game thread
public override void Send(SendOrPostCallback d, object state);
// Called once per game loop tick
public void ExecuteTasks();
}
Memory: STArrayPool vs ArrayPool
In game code, use STArrayPool<T>.Shared (single-threaded, no locks):
// GOOD - no locking overhead
var buffer = STArrayPool<byte>.Shared.Rent(1024);
try { /* use buffer */ }
finally { STArrayPool<byte>.Shared.Return(buffer); }
ArrayPool<T>.Shared uses locks for thread safety -- unnecessary overhead in single-threaded context.
Memory: PooledRefList
// Stack-allocated list using pooled arrays
using var list = PooledRefList<Mobile>.Create();
list.Add(mobile);
// Automatically returns array to pool on Dispose
// For multi-threaded contexts (rare):
using var list = PooledRefList<Mobile>.CreateMT();
World Saves
World saves use parallel serialization threads. This is critical to understand:
- Preserialize: Allocates heaps and wakes serialization thread workers (background)
- Snapshot: Main thread calls
Persistence.SerializeAll()which pushes entities intoSerializationThreadWorkerqueues (round-robin). Workers callSerialize(writer)on their own background threads in parallel. - Write snapshot: Disk I/O on background threads after serialization completes
// From World.cs -- save flow:
World.Save();
→ Preserialize() on thread pool (allocate heaps, wake serialization workers)
→ Snapshot() on main thread (queues entities to workers, workers serialize in parallel)
→ SerializationThreadWorker.Execute() calls e.Serialize(writer) on background thread
→ PauseSerializationThreads() (wait for workers to finish)
→ WriteSnapshot() on thread pool (disk I/O only)
Serialize() runs on background threads
Because SerializationThreadWorker calls Serialize() on its own thread, Serialize() must be pure:
- NO creating/destroying Items or Mobiles
- NO starting/stopping timers (not thread-safe)
- NO sending packets or modifying NetState
- NO mutating shared game state
- ONLY read fields and write to
IGenericWriter
See modernuo-serialization.md for full purity rules.
Exceptions: Server Infrastructure
These files MAY use threading (they're server infrastructure, not game logic):
Projects/Server/Main.cs- Event loop, thread setupProjects/Server/World/World.cs- World save I/OProjects/Server/Network/- Network I/OProjects/Server/Timer/Timer.Pool.cs- Pool refill
Exceptions: Vetted Workers in UOContent
A background thread is a last resort. The forbidden list is about game logic, which is never
threaded. A dedicated worker touching no game state is the sanctioned way off the loop, and
necessarily uses new Thread, ConcurrentQueue<T>, Interlocked, AutoResetEvent and
volatile at the thread boundary only.
Prove the need first
- Measure on-loop time, not wall-clock. Frozen world is the cost; player latency is not.
- Off-loading creates no CPU. On 1-2 cores there is no spare core — gate on
ProcessorCount. - Count what stays: dispatch, continuation, and the loop slowing while the worker evicts shared L3.
- Record the measurement, or nobody can re-justify the worker later.
Game logic stays on the loop — chunk it
Work needing game state cannot be threaded at any core count. Too slow for one tick? Split across ticks, bounded by count or elapsed time — never "until done".
Timer.DelayCall(TimeSpan.Zero, TimeSpan.FromMilliseconds(50), () =>
{
var budget = 0;
while (_cursor < _items.Count && budget++ < 100) { Process(_items[_cursor++]); }
});
Vetted workers
| Worker | Justification |
|---|---|
Accounting/Security/PasswordWorker.cs |
8.9 ms/login on-loop; 3.5-8.9 ms measured saving |
Engines/Advanced Search/AdvancedSearchGump.cs |
Admin-triggered full-world scan, saves disabled |
The five rules
- No game state read or written off-thread; dispatch immutable values captured on the loop.
- Resolve policy (algorithm, salt, era branch) at dispatch — the worker holds none.
- Park on a kernel wait, never spin. Spinning burns a core on shared hosts.
- Run only while
WorldState is Running or WritingSave. NotWorld.Saving— that missesPendingSave, where serialization threads are already spinning. - Bounded queue, or a bound upstream named in a comment.
Crossing the boundary
Dispatch captures what the continuation will need to re-validate:
var job = new Job { Target = state, Expected = account.Password, Input = DerivePhrase(...) };
if (!Worker.TryEnqueue(job)) { /* reject — never fall back to running it inline */ }
Hand back one of two ways, and no other:
Core.LoopContext.Post(() => Apply(job, result)); // a result for a specific caller
Volatile.Write(ref _snapshot, newTable); // a shared table rebuilt periodically
The continuation re-validates, because time passed:
if (job.Target?.Running != true) { return; } // gone
if (account.Password != job.Expected) { return; } // changed underneath
Always post a result, including on failure — a worker that throws silently leaves its caller
waiting forever. Use ConfigureAwait(false) on every await inside off-loop work.
Anti-Patterns
| Pattern | Problem | Solution |
|---|---|---|
Task.Run(...) |
Runs on thread pool, races with game state | Use Timer.StartTimer() |
new Thread(...) |
Same as above | Use Timer.StartTimer() |
lock(obj) |
Unnecessary overhead, no contention exists | Remove lock, use plain code |
ConcurrentDictionary |
Lock-free but still overhead | Use Dictionary<K,V> |
volatile |
Memory barriers not needed on single thread | Use plain field |
Thread.Sleep() |
Blocks entire game loop | Use await Timer.Pause() |
ArrayPool<T>.Shared |
Uses locks | Use STArrayPool<T>.Shared |
Real Examples
- Game loop:
Projects/Server/Main.cs(RunEventLoop) - EventLoopContext:
Projects/Server/EventLoopTasks.cs - STArrayPool:
Projects/Server/Buffers/STArrayPool.cs - PooledRefList:
Projects/Server/Collections/PooledRefList.cs - World save:
Projects/Server/World/World.cs
See Also
dev-docs/threading-model.md- Complete threading documentationdev-docs/claude-skills/modernuo-code-audit.md- Threading audit rulesdev-docs/claude-skills/modernuo-timers.md- Timer-based scheduling