⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 synch-sleep.cc

📁 nachos下线程与同步的实验
💻 CC
字号:
#include "copyright.h"
#include "system.h"
#include "synch-sleep.h"

Lock::Lock(char* debugname) //initialize lock be FREE 
{
     name = debugname;
     queue = new List;
     HoldThread = new Thread("HoldThread");
     Hold = 0;
}

Lock::~Lock()  //析构函数
{
     delete queue;
     delete HolkThread;
}

void 
Lock::Acquire() //wait until the lock is FREE, then set it to BUSY
{
     ASSERT( !isHeldByCurrentThread() );
     IntStatus oldLevel = interrupt->SetLeve(IntOff);
     while ( Hold )
     {
           queue->Append((void *)currentThread);
           //put currentThread at the end of the list
           currenThread->Sleep();
     }
     HoldThread = currentThread;
     Hold = 1;
     (void) interrupt->SetLevel(oldLevel);
}

void 
Lock::Release() 
//set lock to be FREE, waking up a thread waiting in Acquire if necessary
{
     Thread *thread;
     ASSERT( isHeldByCurrent() );
     IntStatus oldLevel = interrupt->SetLevel(IntOff);
     HoldThread = NULL;
     thread = (Thread *)queue->Romove();
     if ( thread != NULL )
          scheduler->ReadyToRun(thread);
     (void) interrupt->SetLevel(oldLevel);
}    

bool 
Lock::isHeldByCurrentThread() 
// true if the current thread holds this lock.  Useful for checking in Release, 
//and in Condition variable ops below
{
     if ( HoldThread == currentThread && Hold )
          return true;
     else
          return false;
}

Condition::Condition(char* debugName) //initialize condition to "no one waiting"
{
     debuname = debugName;
     queue = new List;
}

Condition::~Condition() //析构函数 
{
     delete queue;
}

void 
Condition::Wait(Lock *conditionLock)
//release the lock, relinquish the CPU until signaled,then re-acquire the lock
{
     ASSERT(conditionLokc->isHeldByCurrentThread));
     IntStatus oldLevel = interrupt->SetLevel(IntOff);
     queue->Append((void *)currentThread);
     conditionLock->Release();
     conditionLock->Sleep();
     conditionLock->Acquire();
     (void) interrupt->SetLevel(oldLevel);
}

void 
Condition::Signal(Lock *conditionLock)
//wake up a thread, if there are any waiting on the condition
{
       Thread *thread;
       ASSERT( conditionLock->isHeldByCurrentThread) );
       IntStatus oldLevel = interrupt->SetLevel(intOff);
       thread = (Thread *)queue->Remove();
       if ( thread != NULL )
            scheduler->ReadyToRun(thread);
       (void) interruptLock->SetLevel(oldLevel);
}

void 
Condition::Broadcast(Lock* conditionLock)
//wake up all threads waiting on the condition
{
       Thread *thread;
       ASSERT( conditionLock->isHeldByCurrentThread());
       IntStatus oldLevel = interrupt->SetLevel(IntOff);
       thread = (Thread *)queue->Remove();
       while ( thread != NULL )
       {
             scheduler->ReadyToRun();
             thread = (Thread *)queue->Remove();
       }
       (void) interrupt->SetLevel(oldLevel);
}    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -