-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynchlist.cc
98 lines (86 loc) · 2.84 KB
/
synchlist.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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.
//----------------------------------------------------------------------
void
SynchList::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.
//----------------------------------------------------------------------
void
SynchList::Mapcar(VoidFunctionPtr func)
{
lock->Acquire();
list->Mapcar(func);
lock->Release();
}