nachos下线程与同步的实验

源代码在线查看: table.cc

软件大小: 14 K
上传用户: wuweixiong123
关键词: nachos 线程 同步的 实验
下载地址: 免注册下载 普通下载 VIP

相关代码

				#include "Table.h"
				
				Table::Table(int size)
				// create a table to hold at most 'size' entries.
				{
				     S = size;
				     lock = new Lock("Tablelock");
				     L = new int[size];
				     value = new int[size];
				     for ( int i=0; i				         value[i] = 0;           
				}
				
				Table::~Table() //析构函数 
				{
				     for ( int i=0; i				         Release(i);
				     delete lock;
				     delete value;
				}
				
				int 
				Table::Alloc(void *object)
				// allocate a table slot for 'object'.
				// return the table index for the slot or -1 on error.
				{
				      int i;
				      lock->Acquire();
				      for ( i=0; i				          if ( value[i] == 0 )
				          {
				               value[i] = 1;
				               object = (void *)(L+i);
				               break;
				          }
				      lock->Release();
				      if ( i				           return i+1;
				      else 
				           return -1;
				}
				
				void *Table::Get(int *index)
				// 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.
				{
				     if ( index>0 && index				          return (void *)(L+index-1);
				     else
				          return NULL;
				}
				
				void Table::Release(int index)  // free a table slot
				{
				     lokc->Acquire();
				     if ( index>0 && index				          delete (L+index-1);
				     lock->Release();
				}
							

相关资源