This commit is contained in:
2024-12-30 05:59:42 +08:00
parent bf78b95c9d
commit 72320ede22
42 changed files with 1758 additions and 1556 deletions

View File

@@ -48,7 +48,7 @@ Semaphore::Semaphore(char* debugName, int initialValue)
{
name = debugName;
value = initialValue;
queue = new List<Thread *>;
queue = new List<Thread*>;
}
//----------------------------------------------------------------------
@@ -75,20 +75,20 @@ Semaphore::~Semaphore()
void
Semaphore::P()
{
Interrupt *interrupt = kernel->interrupt;
Thread *currentThread = kernel->currentThread;
Interrupt* interrupt = kernel->interrupt;
Thread* currentThread = kernel->currentThread;
// disable interrupts
IntStatus oldLevel = interrupt->SetLevel(IntOff);
IntStatus oldLevel = interrupt->SetLevel(IntOff);
while (value == 0) { // semaphore not available
queue->Append(currentThread); // so go to sleep
currentThread->Sleep(FALSE);
}
queue->Append(currentThread); // so go to sleep
currentThread->Sleep(FALSE);
}
value--; // semaphore available, consume its value
// re-enable interrupts
(void) interrupt->SetLevel(oldLevel);
(void)interrupt->SetLevel(oldLevel);
}
//----------------------------------------------------------------------
@@ -102,18 +102,18 @@ Semaphore::P()
void
Semaphore::V()
{
Interrupt *interrupt = kernel->interrupt;
Interrupt* interrupt = kernel->interrupt;
// disable interrupts
IntStatus oldLevel = interrupt->SetLevel(IntOff);
IntStatus oldLevel = interrupt->SetLevel(IntOff);
if (!queue->IsEmpty()) { // make thread ready.
kernel->scheduler->ReadyToRun(queue->RemoveFront());
kernel->scheduler->ReadyToRun(queue->RemoveFront());
}
value++;
// re-enable interrupts
(void) interrupt->SetLevel(oldLevel);
(void)interrupt->SetLevel(oldLevel);
}
//----------------------------------------------------------------------
@@ -122,27 +122,27 @@ Semaphore::V()
// to control two threads ping-ponging back and forth.
//----------------------------------------------------------------------
static Semaphore *ping;
static Semaphore* ping;
static void
SelfTestHelper (Semaphore *pong)
SelfTestHelper(Semaphore* pong)
{
for (int i = 0; i < 10; i++) {
ping->P();
pong->V();
pong->V();
}
}
void
Semaphore::SelfTest()
{
Thread *helper = new Thread("ping", 1);
Thread* helper = new Thread("ping", 1);
ASSERT(value == 0); // otherwise test won't work!
ping = new Semaphore("ping", 0);
helper->Fork((VoidFunctionPtr) SelfTestHelper, this);
helper->Fork((VoidFunctionPtr)SelfTestHelper, this);
for (int i = 0; i < 10; i++) {
ping->V();
this->P();
this->P();
}
delete ping;
}
@@ -213,7 +213,7 @@ void Lock::Release()
Condition::Condition(char* debugName)
{
name = debugName;
waitQueue = new List<Semaphore *>;
waitQueue = new List<Semaphore*>;
}
//----------------------------------------------------------------------
@@ -241,18 +241,18 @@ Condition::~Condition()
// "conditionLock" -- lock protecting the use of this condition
//----------------------------------------------------------------------
void Condition::Wait(Lock* conditionLock)
void Condition::Wait(Lock* conditionLock)
{
Semaphore *waiter;
ASSERT(conditionLock->IsHeldByCurrentThread());
Semaphore* waiter;
waiter = new Semaphore("condition", 0);
waitQueue->Append(waiter);
conditionLock->Release();
waiter->P();
conditionLock->Acquire();
delete waiter;
ASSERT(conditionLock->IsHeldByCurrentThread());
waiter = new Semaphore("condition", 0);
waitQueue->Append(waiter);
conditionLock->Release();
waiter->P();
conditionLock->Acquire();
delete waiter;
}
//----------------------------------------------------------------------
@@ -272,13 +272,13 @@ void Condition::Wait(Lock* conditionLock)
void Condition::Signal(Lock* conditionLock)
{
Semaphore *waiter;
Semaphore* waiter;
ASSERT(conditionLock->IsHeldByCurrentThread());
if (!waitQueue->IsEmpty()) {
waiter = waitQueue->RemoveFront();
waiter->V();
waiter->V();
}
}
@@ -289,7 +289,7 @@ void Condition::Signal(Lock* conditionLock)
// "conditionLock" -- lock protecting the use of this condition
//----------------------------------------------------------------------
void Condition::Broadcast(Lock* conditionLock)
void Condition::Broadcast(Lock* conditionLock)
{
while (!waitQueue->IsEmpty()) {
Signal(conditionLock);