-
Notifications
You must be signed in to change notification settings - Fork 3
/
table.cpp
169 lines (147 loc) · 3.34 KB
/
table.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <string.h>
#include <stdlib.h>
extern "C" {
#include "llist.h"
}
#include "table.h"
#include "PadThreads.h"
/* Should really turn this into a hash,
both for speed and standard interface */
TableEntry::TableEntry()
{
name = NULL;
}
TableEntry::TableEntry(const TableEntry & rhs)
{
if (rhs.name)
name = strdup(rhs.name);
}
TableEntry::~TableEntry()
{
if (name)
free(name);
}
int TableEntry::compare(const void *entry1, const void *entry2) //Used to sort table
{
return (strcmp(((TableEntry *) entry1)->name, ((TableEntry *) entry2)->name));
}
int TableEntry::findName(const void *entry, const void *name)
{
return (strcmp(((TableEntry *) entry)->name, (const char *) name));
}
void TableEntry::acquire(void)
{
lock.enter();
}
void TableEntry::release(void)
{
lock.leave();
}
/*
OK the locking here uses a fixed 3 level hierarchy.
This means that if there is any contention on a TableEntry
then the whole table is NOT locked. See the locking
diagram for more info.
*/
Table::Table()
{
table=NULL;
}
Table::~Table()
{
tableLock.enter();
TableEntry* Entry;
while ((Entry = (TableEntry *) llist_pop(&table, NULL, NULL))) {
Entry->acquire();
delete Entry;
}
tableLock.leave();
}
/*
NB make sure you don't access an object after
you add it to a table. If this is required??
then ->acquire() first and then ->release()
*/
bool Table::add(TableEntry * Entry)
{
// Nameless objects CANNOT be part of a table
if (!Entry->name)
return false;
tableLock.enter();
int result = llist_add(&table, Entry);
tableLock.leave();
if (result)
return true;
else
return false;
}
TableEntry *Table::get(const char *Name)
{
if (!Name)
return NULL;
TableEntry *Entry;
tableLock.enter();
Entry = (TableEntry *) llist_find(table, Name, TableEntry::findName);
if (Entry)
Entry->acquire();
tableLock.leave();
return Entry;
}
bool Table::del(const char *Name)
{
if (!Name)
return false;
TableEntry *Entry;
table_rwlock.writelock();
tableLock.enter();
Entry = (TableEntry *) llist_pop(&table, Name, TableEntry::findName);
if (Entry) {
Entry->acquire();
delete Entry;
}
tableLock.leave();
table_rwlock.unlock();
if (Entry)
return true;
else
return false;
}
TableEntry *Table::getFirst(void **cursor)
{
TableEntry *Entry;
table_rwlock.readlock();
tableLock.enter();
*cursor = table;
if (table) {
Entry = (TableEntry *) table->val;
} else {
Entry = NULL;
}
if (Entry) {
Entry->acquire();
}
tableLock.leave();
if (!Entry)
table_rwlock.unlock(); //nothing in table => allow writers to table to modify
return Entry;
}
TableEntry *Table::getNext(void **cursor)
{
//Note could do (datanode*)(*cursor)->data.lock.leave();
//here but no as need explicit release in certain cases anyway.
TableEntry *Entry;
tableLock.enter();
*cursor = ((llist_entry *) *cursor)->next;
if (*cursor) {
Entry = (TableEntry *) ((llist_entry *) *cursor)->val;
} else {
Entry = NULL;
}
if (Entry) {
Entry->acquire();
}
tableLock.leave();
if (!Entry)
table_rwlock.unlock(); //@ end of table => allow writers to table to modify
return Entry;
}