Add dispatcher object routing for wait satisfaction and interval calculation
This commit is contained in:
@@ -10,6 +10,45 @@
|
||||
#include <xtos.hh>
|
||||
|
||||
|
||||
/**
|
||||
* Calculates the remaining wait interval for a thread after a wait operation has been interrupted.
|
||||
*
|
||||
* @param OriginalDueTime
|
||||
* Supplies the original timeout value requested by the caller.
|
||||
*
|
||||
* @param PreviousDueTime
|
||||
* Supplies the base timestamp, usually the time when the wait was first initiated.
|
||||
*
|
||||
* @param NewDueTime
|
||||
* Supplies a pointer to a LARGE_INTEGER buffer where the recalculated time will be stored.
|
||||
*
|
||||
* @return This routine returns a pointer to the resulting due time.
|
||||
*
|
||||
* @since XT 1.0
|
||||
*/
|
||||
XTFASTCALL
|
||||
PLARGE_INTEGER
|
||||
KE::Dispatcher::ComputeWaitInterval(IN PLARGE_INTEGER OriginalDueTime,
|
||||
IN PLARGE_INTEGER PreviousDueTime,
|
||||
IN OUT PLARGE_INTEGER NewDueTime)
|
||||
{
|
||||
/* Check if the timeout is absolute */
|
||||
if(OriginalDueTime->QuadPart >= 0)
|
||||
{
|
||||
/* No recalculation is needed, return the original value */
|
||||
return OriginalDueTime;
|
||||
}
|
||||
|
||||
/* Fetch the current system interrupt time for the recalculation base */
|
||||
KE::SystemTime::GetInterruptTime(NewDueTime);
|
||||
|
||||
/* Calculate the delta */
|
||||
NewDueTime->QuadPart -= PreviousDueTime->QuadPart;
|
||||
|
||||
/* Return the new due time */
|
||||
return NewDueTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enters the system idle thread loop for the current processor, running continuously when no other
|
||||
* threads are scheduled for execution.
|
||||
@@ -149,7 +188,7 @@ KE::Dispatcher::HandleDispatchInterrupt(IN PKTRAP_FRAME TrapFrame)
|
||||
/* Start a guarded code block */
|
||||
{
|
||||
/* Lock the processor control block */
|
||||
KE::SpinLockGuard ControlBlockGuard(&Prcb->PrcbLock);
|
||||
KE::SpinLockGuard PrcbGuard(&Prcb->PrcbLock);
|
||||
|
||||
/* Capture the outgoing (preempted) and incoming threads */
|
||||
CurrentThread = Prcb->CurrentThread;
|
||||
@@ -175,6 +214,60 @@ KE::Dispatcher::HandleDispatchInterrupt(IN PKTRAP_FRAME TrapFrame)
|
||||
AR::CpuFunctions::ClearInterruptFlag();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Evaluates the state of a dispatcher object to determine if a wait can be satisfied immediately without
|
||||
* suspending the current thread.
|
||||
*
|
||||
* @param Object
|
||||
* Supplies a pointer to the dispatcher object header.
|
||||
*
|
||||
* @param Thread
|
||||
* Supplies a pointer to the current thread attempting to acquire the object.
|
||||
*
|
||||
* @return This routine returns a status code indicating the success or failure of the operation.
|
||||
*
|
||||
* @since XT 1.0
|
||||
*/
|
||||
XTFASTCALL
|
||||
XTSTATUS
|
||||
KE::Dispatcher::SatisfyWaitingObject(IN PDISPATCHER_HEADER Object,
|
||||
IN PKTHREAD Thread)
|
||||
{
|
||||
PKMUTEX Mutex;
|
||||
|
||||
/* Check if the object is a Mutex */
|
||||
if(Object->Type == MutexObject)
|
||||
{
|
||||
/* Get a pointer to the Mutex object */
|
||||
Mutex = (PKMUTEX)Object;
|
||||
|
||||
/* Check if the mutex is free or if the current thread already owns it */
|
||||
if((Mutex->Header.SignalState > 0) || (Thread == Mutex->OwnerThread))
|
||||
{
|
||||
/* Verify that recursive acquisition has not hit the mathematical lower bound */
|
||||
if(Mutex->Header.SignalState != MINLONG)
|
||||
{
|
||||
/* Satisfy the wait and inherit priority if applicable */
|
||||
KE::DispatcherObject::SatisfyWaitingMutexObject(Mutex, Thread);
|
||||
return (XTSTATUS)Thread->WaitStatus;
|
||||
}
|
||||
|
||||
/* The mutex counter has overflowed */
|
||||
return STATUS_MUTEX_LIMIT_EXCEEDED;
|
||||
}
|
||||
}
|
||||
else if(Object->SignalState > 0)
|
||||
{
|
||||
/* Apply generic satisfaction rules */
|
||||
KE::DispatcherObject::SatisfyWaitingNonMutexObject(Object);
|
||||
return STATUS_WAIT_0;
|
||||
}
|
||||
|
||||
/* Object is not signaled, the thread must be queued */
|
||||
return STATUS_PENDING;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the runtime quantum of the currently executing thread and handles preemption.
|
||||
*
|
||||
@@ -291,3 +384,83 @@ KE::Dispatcher::UpdateRunTime(IN PKTRAP_FRAME TrapFrame,
|
||||
HL::Irq::SendSoftwareInterrupt(DISPATCH_LEVEL);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Places the current thread into a wait state until the specified dispatcher object is set to a signaled state,
|
||||
* or until the optional timeout expires.
|
||||
*
|
||||
* @param Object
|
||||
* Supplies a pointer to the dispatcher object to wait on.
|
||||
*
|
||||
* @param WaitReason
|
||||
* Supplies the reason for the wait, utilized for diagnostic and profiling purposes.
|
||||
*
|
||||
* @param WaitMode
|
||||
* Supplies the processor mode in which the wait is occurring (KernelMode or UserMode).
|
||||
*
|
||||
* @param Alertable
|
||||
* Specifies whether the wait is alertable by asynchronous procedure calls (APCs).
|
||||
*
|
||||
* @param Timeout
|
||||
* Supplies an optional pointer to an absolute or relative timeout value.
|
||||
*
|
||||
* @return This routine returns the completion status of the wait operation.
|
||||
*
|
||||
* @since XT 1.0
|
||||
*/
|
||||
XTAPI
|
||||
XTSTATUS
|
||||
KE::Dispatcher::WaitForSingleObject(IN PVOID Object,
|
||||
IN KWAIT_REASON WaitReason,
|
||||
IN KPROCESSOR_MODE WaitMode,
|
||||
IN BOOLEAN Alertable,
|
||||
IN PLARGE_INTEGER Timeout)
|
||||
{
|
||||
PDISPATCHER_HEADER Header;
|
||||
LARGE_INTEGER CurrentTime;
|
||||
PKTHREAD CurrentThread;
|
||||
|
||||
/* Not implemented, active polling only */
|
||||
UNIMPLEMENTED;
|
||||
|
||||
/* Get the dispatcher header */
|
||||
Header = (PDISPATCHER_HEADER)Object;
|
||||
|
||||
/* Get the current thread */
|
||||
CurrentThread = KE::Processor::GetCurrentThread();
|
||||
|
||||
/* Check if the object is already signaled or if it is an already owned Mutex */
|
||||
if((Header->SignalState > 0) || ((Header->Type == MutexObject) && (CurrentThread == ((PKMUTEX)Object)->OwnerThread)))
|
||||
{
|
||||
/* Satisfy the object and return status code */
|
||||
SatisfyWaitingObject(Header, CurrentThread);
|
||||
return STATUS_WAIT_0;
|
||||
}
|
||||
|
||||
/* Enter cctive polling loop */
|
||||
while(Header->SignalState <= 0)
|
||||
{
|
||||
/* Check if the timeout has expired */
|
||||
if(Timeout != NULLPTR)
|
||||
{
|
||||
/* Get the current interrupt time */
|
||||
KE::SystemTime::GetInterruptTime(&CurrentTime);
|
||||
|
||||
/* Check if current time exceeds the timeout value */
|
||||
if(CurrentTime.QuadPart >= Timeout->QuadPart)
|
||||
{
|
||||
/* Wait expired, return status code */
|
||||
return STATUS_TIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
/* Yield the processor */
|
||||
AR::CpuFunctions::YieldProcessor();
|
||||
}
|
||||
|
||||
/* Apply acquisition rules */
|
||||
SatisfyWaitingObject(Header, CurrentThread);
|
||||
|
||||
/* Return status code */
|
||||
return STATUS_WAIT_0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user