Main Page | Alphabetical List | Class List | File List | Class Members | File Members

synchlist.cc

Go to the documentation of this file.
00001 // synchlist.cc 00002 // Routines for synchronized access to a list. 00003 // 00004 // Implemented by surrounding the List abstraction 00005 // with synchronization routines. 00006 // 00007 // Implemented in "monitor"-style -- surround each procedure with a 00008 // lock acquire and release pair, using condition signal and wait for 00009 // synchronization. 00010 // 00011 // Copyright (c) 1992-1993 The Regents of the University of California. 00012 // All rights reserved. See copyright.h for copyright notice and limitation 00013 // of liability and disclaimer of warranty provisions. 00014 00015 #include "copyright.h" 00016 #include "synchlist.h" 00017 00018 //---------------------------------------------------------------------- 00019 // SynchList::SynchList 00020 // Allocate and initialize the data structures needed for a 00021 // synchronized list, empty to start with. 00022 // Elements can now be added to the list. 00023 //---------------------------------------------------------------------- 00024 00025 SynchList::SynchList() 00026 { 00027 list = new List(); 00028 lock = new Lock("list lock"); 00029 listEmpty = new Condition("list empty cond"); 00030 } 00031 00032 //---------------------------------------------------------------------- 00033 // SynchList::~SynchList 00034 // De-allocate the data structures created for synchronizing a list. 00035 //---------------------------------------------------------------------- 00036 00037 SynchList::~SynchList() 00038 { 00039 delete list; 00040 delete lock; 00041 delete listEmpty; 00042 } 00043 00044 //---------------------------------------------------------------------- 00045 // SynchList::Append 00046 // Append an "item" to the end of the list. Wake up anyone 00047 // waiting for an element to be appended. 00048 // 00049 // "item" is the thing to put on the list, it can be a pointer to 00050 // anything. 00051 //---------------------------------------------------------------------- 00052 00053 void 00054 SynchList::Append(void *item) 00055 { 00056 lock->Acquire(); // enforce mutual exclusive access to the list 00057 list->Append(item); 00058 listEmpty->Signal(lock); // wake up a waiter, if any 00059 lock->Release(); 00060 } 00061 00062 //---------------------------------------------------------------------- 00063 // SynchList::Remove 00064 // Remove an "item" from the beginning of the list. Wait if 00065 // the list is empty. 00066 // Returns: 00067 // The removed item. 00068 //---------------------------------------------------------------------- 00069 00070 void * 00071 SynchList::Remove() 00072 { 00073 void *item; 00074 00075 lock->Acquire(); // enforce mutual exclusion 00076 while (list->IsEmpty()) 00077 listEmpty->Wait(lock); // wait until list isn't empty 00078 item = list->Remove(); 00079 ASSERT(item != NULL); 00080 lock->Release(); 00081 return item; 00082 } 00083 00084 //---------------------------------------------------------------------- 00085 // SynchList::Mapcar 00086 // Apply function to every item on the list. Obey mutual exclusion 00087 // constraints. 00088 // 00089 // "func" is the procedure to be applied. 00090 //---------------------------------------------------------------------- 00091 00092 void 00093 SynchList::Mapcar(VoidFunctionPtr func) 00094 { 00095 lock->Acquire(); 00096 list->Mapcar(func); 00097 lock->Release(); 00098 }

Generated on Thu Sep 16 12:33:45 2004 for NachOS by doxygen 1.3.8