Struct UnitOfWorkContext
A 24-byte execution context that flows through all operations inside a Unit of Work (or standalone transaction). Embeds a WaitContext
(deadline + cancellation) and adds UoW identity and holdoff state. Lock sites can use ref ctx.WaitContext directly — zero construction cost.
public struct UnitOfWorkContext
- Inherited Members
Remarks
default(UnitOfWorkContext) is already expired (fail-safe) — the embedded deadline is expired and ThrowIfCancelled()
will throw immediately. Use None for unbounded operations (infinite deadline, no cancellation).
This is a value type passed by ref through synchronous call chains. The CancellationTokenSource that generates
the token is owned externally — by the future UnitOfWork class (Tier 3) or by Typhon.Engine.Internals.DeadlineWatchdog.
Constructors
UnitOfWorkContext(Deadline, CancellationToken, ushort)
Primary constructor — deadline + cancellation token.
public UnitOfWorkContext(Deadline deadline, CancellationToken token, ushort uowId = 0)
Parameters
deadlineDeadlinetokenCancellationTokenuowIdushort
UnitOfWorkContext(WaitContext, ushort)
Primary constructor — WaitContext + UoW ID.
public UnitOfWorkContext(WaitContext waitContext, ushort uowId = 0)
Parameters
waitContextWaitContextuowIdushort
Fields
UowId
UoW identifier for revision stamping and crash recovery. Distinct from EpochManager.GlobalEpoch (EBRM resource lifecycle).
public readonly ushort UowId
Field Value
WaitContext
Embedded wait context carrying deadline + cancellation token. Lock call sites can use ref ctx.WaitContext directly.
Non-readonly to allow ref passing to lock primitives.
public WaitContext WaitContext
Field Value
Properties
Deadline
The embedded deadline.
public Deadline Deadline { get; }
Property Value
IsCancellationRequested
True if the cancellation token has been triggered.
public bool IsCancellationRequested { get; }
Property Value
IsExpired
True if the deadline has expired.
public bool IsExpired { get; }
Property Value
IsInHoldoff
True if currently inside a holdoff region.
public bool IsInHoldoff { get; }
Property Value
None
Unbounded context: infinite deadline, no cancellation. For internal operations that should not be subject to timeout (e.g., cleanup, rollback).
public static UnitOfWorkContext None { get; }
Property Value
Remaining
Remaining time until deadline expires.
public TimeSpan Remaining { get; }
Property Value
Token
The embedded cancellation token.
public CancellationToken Token { get; }
Property Value
Methods
BeginHoldoff()
Increment holdoff counter (prefer Typhon.Engine.UnitOfWorkContext.EnterHoldoff for RAII safety).
public void BeginHoldoff()
EndHoldoff()
Decrement holdoff counter.
public void EndHoldoff()
FromTimeout(TimeSpan)
Create from a relative timeout (no cancellation, no UoW ID).
public static UnitOfWorkContext FromTimeout(TimeSpan timeout)
Parameters
timeoutTimeSpan
Returns
FromTimeout(TimeSpan, CancellationToken)
Create from a relative timeout + cancellation token.
public static UnitOfWorkContext FromTimeout(TimeSpan timeout, CancellationToken token)
Parameters
timeoutTimeSpantokenCancellationToken
Returns
ThrowIfCancelled()
Cooperative cancellation check. Call at yield points (safe locations where aborting won't leave data structures in an inconsistent state).
public void ThrowIfCancelled()
Remarks
If Typhon.Engine.UnitOfWorkContext._holdoffCount > 0 (inside a holdoff region), this is a no-op. Cancellation is deferred until the holdoff exits.
Checks deadline first (cheaper than token check on most paths), then cancellation token. Accesses fields through embedded WaitContext — the JIT resolves struct field offsets at compile time, producing identical code to direct field access.
Exceptions
- TyphonTimeoutException
Deadline has expired (outside holdoff).
- OperationCanceledException
Token was canceled (outside holdoff).