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

📄 synchlist.cc

📁 nachos系统作业 实现线程系统 实现一个电梯模拟 附实验报告
💻 CC
字号:
// synchlist.cc//	Routines for synchronized access to a list.////	Implemented by surrounding the List abstraction//	with synchronization routines.//// 	Implemented in "monitor"-style -- surround each procedure with a// 	lock acquire and release pair, using condition signal and wait for// 	synchronization.//// Copyright (c) 1992-1993 The Regents of the University of California.// All rights reserved.  See copyright.h for copyright notice and limitation // of liability and disclaimer of warranty provisions.#include "copyright.h"#include "synchlist.h"//----------------------------------------------------------------------// SynchList::SynchList//	Allocate and initialize the data structures needed for a //	synchronized list, empty to start with.//	Elements can now be added to the list.//----------------------------------------------------------------------SynchList::SynchList(){    list = new List();    lock = new Lock("list lock");     listEmpty = new Condition("list empty cond");}//----------------------------------------------------------------------// SynchList::~SynchList//	De-allocate the data structures created for synchronizing a list. //----------------------------------------------------------------------SynchList::~SynchList(){     delete list;     delete lock;    delete listEmpty;}//----------------------------------------------------------------------// SynchList::Append//      Append an "item" to the end of the list.  Wake up anyone//	waiting for an element to be appended.////	"item" is the thing to put on the list, it can be a pointer to //		anything.//----------------------------------------------------------------------voidSynchList::Append(void *item){    lock->Acquire();		// enforce mutual exclusive access to the list     list->Append(item);    listEmpty->Signal(lock);	// wake up a waiter, if any    lock->Release();}//----------------------------------------------------------------------// SynchList::Remove//      Remove an "item" from the beginning of the list.  Wait if//	the list is empty.// Returns://	The removed item. //----------------------------------------------------------------------void *SynchList::Remove(){    void *item;    lock->Acquire();			// enforce mutual exclusion    while (list->IsEmpty())	listEmpty->Wait(lock);		// wait until list isn't empty    item = list->Remove();    ASSERT(item != NULL);    lock->Release();    return item;}//----------------------------------------------------------------------// SynchList::Mapcar//      Apply function to every item on the list.  Obey mutual exclusion//	constraints.////	"func" is the procedure to be applied.//----------------------------------------------------------------------voidSynchList::Mapcar(VoidFunctionPtr func){     lock->Acquire();     list->Mapcar(func);    lock->Release(); }

⌨️ 快捷键说明

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