-
Notifications
You must be signed in to change notification settings - Fork 1
/
block.c
295 lines (255 loc) · 6.35 KB
/
block.c
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <stdlib.h>
#include "myassert.h"
#include "block.h"
#include "vm.h"
#include "sim.h"
static unsigned int nextBlockId = 1;
static BlockList blockList; // = Q_STATIC_INIT;
static Block** allBlocks = NULL;
static int allBlockSize = 0;
// initializes the access mutex
void initBlockList()
{
Q_INIT_HEAD(&blockList);
allBlocks = (Block**)calloc(2, sizeof(Block*));
allBlockSize = 2;
}
void registerBlock(Block* block)
{
Q_INSERT_TAIL(&blockList, block, blockLink);
if (block->id >= allBlockSize) {
//fprintf(stderr, "Reallocing to %d\n", allBlockSize*2);
allBlocks = (Block**)realloc(allBlocks, allBlockSize*2*sizeof(Block*));
allBlockSize = allBlockSize*2;
}
allBlocks[block->id] = block;
}
void
startBlock(Block *block)
{
// start block off with no neighbors and all vacant
msg2vm(block, CMD_ADD_NCOUNT, block->localTime, 0);
int i;
for (i=0; i<NUM_PORTS; i++)
msg2vm(block, CMD_ADD_VACANT, block->localTime, i);
checkForNeighbors(block, NULL);
}
Block *
createBlock(int x, int y, int z)
{
Block* newBlock;
newBlock = calloc(1, sizeof(Block));
if (newBlock == NULL)
return newBlock;
newBlock->id = nextBlockId++;
newBlock->x = x;
newBlock->y = y;
newBlock->z = z;
newBlock->simLEDr = 120;
newBlock->simLEDg = 120;
newBlock->simLEDb = 120;
newBlock->simLEDi = 200;
newBlock->msgTargetsDelta = 0;
newBlock->msgTargets = 0;
newBlock->destroyed = 0;
newBlock->localTime = globalTimeStamp;
newBlock->lastSimTime = globalTimeStamp-1;
int i;
for (i=0; i<NUM_PORTS; i++) {
newBlock->neighborhood[i] = NULL;
}
//fprintf(stderr, "made block, inserting into Q\n");
registerBlock(newBlock);
msg2vm(newBlock, CMD_CREATE, newBlock->localTime, 1, newBlock->id);
return newBlock;
}
typedef struct {
Block* block;
Face face;
} FaceBlock;
void tellNeighborsDestroyed(Block *b, FaceBlock* list);
void destroyBlock(Block *block)
{
FaceBlock neighbors[NUM_PORTS+1];
tellNeighborsDestroyed(block, neighbors);
Q_REMOVE(&blockList, block, blockLink);
block->destroyed = 1;
allBlocks[block->id] = NULL;
free(block);
// now set neighbor count
int i;
for (i=0; neighbors[i].block != 0; i++) {
msg2vm(neighbors[i].block, CMD_ADD_NCOUNT, countNeighbors(neighbors[i].block));
msg2vm(neighbors[i].block, CMD_ADD_VACANT, neighbors[i].face);
}
}
BlockList *getBlockList(void)
{
return &blockList;
}
Block*
getBlock(NodeID id)
{
if (id > nextBlockId) err("Asked for %d higher than num allocated blocks %d", id, nextBlockId);
Block* block = allBlocks[id];
if (block == NULL) err("Aksed for %d which has NULL entry in allBlocks", id);
if (block->destroyed) err("Asked for %d which was destroyed", id);
return block;
}
// format NodeID
char*
nodeIDasString(NodeID x, char* sid)
{
static char ssid[10];
if (sid == 0) sid = ssid;
sprintf(sid, "%02u", x);
return sid;
}
// b: block to check
// other: if set, then we only tell b about other's face
void
checkForNeighbors(Block* b, Block* other)
{
int i;
for(i = 0; i < NUM_PORTS; ++i) {
// see if we have a neighbor at port i
Block* d = seeIfNeighborAt(b, i);
if ((d != 0)&&((other==NULL)||(other==d))) {
if (d->neighborhood[i] == NULL) {
// we have a neighbor. tell them
msg2vm(d, CMD_DEL_VACANT, b->localTime, i);
msg2vm(d, CMD_ADD_NBR, b->localTime, b->id, i);
if (other == NULL) checkForNeighbors(d, b);
msg2vm(d, CMD_ADD_NCOUNT, b->localTime, countNeighbors(d));
d->neighborhood[i] = b;
}
assert(d->neighborhood[i] == b);
}
}
}
int
countNeighbors(Block* b)
{
int cnt = 0;
int i;
for(i = 0; i < NUM_PORTS; ++i) {
// see if we have a neighbor at port i
Block* d = seeIfNeighborAt(b, i);
if (d != NULL) cnt++;
}
return cnt;
}
void
tellNeighborsDestroyed(Block *b, FaceBlock* list)
{
int i;
int j = 0;
for(i = 0; i < NUM_PORTS; ++i) {
// see if we have a neighbor at port i
Block* d = seeIfNeighborAt(b, i);
if (d != 0) {
assert(d->neighborhood[i] == b);
d->neighborhood[i] = NULL;
// if so, then eliminate it from that block
msg2vm(d, CMD_DEL_NBR, b->localTime, i);
if (list != NULL) {
list[j].block = d;
list[j].face = i;
j++;
}
}
}
if (list != NULL) list[j].block = NULL;
}
#include "heap.h"
int
bLocalTimeCompare(Block* a, Block* b)
{
return a->localTime - b->localTime;
}
int
bLastSimTimeCompare(Block* a, Block* b)
{
return a->lastSimTime - b->lastSimTime;
}
Heap* gheap; /* blocktime vrs. globaltime */
Heap* dheap; /* blocktime vrs. last sim time */
void
initTimeKeeping(void)
{
gheap = newHeap(10, (ComparisonFunction)bLocalTimeCompare);
dheap = newHeap(10, (ComparisonFunction)bLastSimTimeCompare);
globalTimeStamp = 0;
}
void
updateTime(Block* block, Time ts, int delta)
{
fprintf(stderr, "Updatetime: %d <- %d was:%d, %d\n", block->id, ts, block->localTime, block->lastSimTime);
if (block->localTime <= ts) {
block->localTime = ts;
if (heapUpdate(gheap, block) == NULL) {
heapAdd(gheap, block);
}
}
Block* minBlock = (Block*)heapPeek(gheap);
if (minBlock->localTime > globalTimeStamp)
globalTimeStamp = minBlock->localTime;
}
static int
checkGlobalTime(Block* b)
{
return b->localTime < globalTimeStamp;
}
static int
checkSimTime(Block* b)
{
return b->lastSimTime < b->localTime;
}
static int
checkASched(Heap* heap, int (*ok)(Block*))
{
int cnt = 0;
Block* b = heapPeek(heap);
if (b == NULL) return 0;
while ((b != NULL) && ok(b)) {
heapRemove(heap);
b->localTime = globalTimeStamp;
cnt++;
msg2vm(b, CMD_RUN, b->localTime);
b->lastSimTime = b->localTime;
b = heapPeek(heap);
}
return cnt;
}
int
checkSchedule(void)
{
int cnt;
cnt = checkASched(gheap, checkGlobalTime);
if (cnt == 0) cnt = checkASched(dheap, checkSimTime);
return cnt;
}
void
needsSchedule(Block* block, Time ts)
{
}
void
showBlock(FILE* f, Block* block)
{
fprintf(f, "%s:%d @%d,%d (%2d,%2d,%2d) [%3d,%3d,%3d,%3d]\n",
nodeIDasString(block->id, 0),
block->msgTargets,
block->localTime,
block->lastSimTime,
block->x,
block->y,
block->z,
block->simLEDr,
block->simLEDg,
block->simLEDb,
block->simLEDi);
}
// Local Variables:
// tab-width: 4
// indent-tabs-mode: nil
// End: