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

📄 table.cc

📁 nachos系统作业 实现线程系统 实现一个电梯模拟 附实验报告
💻 CC
字号:
#include "Table.h"
//#include <malloc.h>
//----------------------------------------------------------------------------------
     // create a table to hold at most 'size' entries.
Table::Table(int fixsize)
{      size=fixsize;        now=0;          p=new TElement[size];      lock = new LockSem("Table lock");       tableFull = new ConditionSem("table full cond");}//-------------------------------------------------------------------------------------Table::~Table(){      delete p;      delete lock;      delete tableFull;}//--------------------------------------------------------------------------------------
     // allocate a table slot for 'object'.
     // return the table index for the slot or -1 on error.
intTable::Alloc(void *object){      int j;       lock->Acquire();          while (IsFull())	           tableFull->Wait(lock);          for(int i=0;i<size;i++){                 if((p+i)->flag==0){                    (p+i)->object=object;                    (p+i)->flag=1;now++;j=i;break;}                  }       lock->Release();       return j;}
//----------------------------------------------------------------------------------------   
     // return the object from table index 'index' or NULL on error.
     // (assert index is in range).  Leave the table entry allocated
     // and the pointer in place.void *
Table::Get(int index)
{                     void *item=NULL;if(index>=0&&index<size){             lock->Acquire();             item=(p+index)->object;             lock->Release(); }      return item;}//-------------------------------------------------------------------------------------------     // free a table slot
voidTable::Release(int index)
{  if(index>=0&&index<size){    lock->Acquire();       if((p+index)->flag==1){//delete ((int *)(p+index)->object);                         (p+index)->object=NULL;                         (p+index)->flag=0;now--;                         tableFull->Signal(lock);}    lock->Release();                                }   else return;}//-----------------------------------------------------------------------------------------------  boolTable::IsFull(){ if(now==size)return true; else return false;}

⌨️ 快捷键说明

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