21 |
uint32_t numStrands; // number of strands in the world |
uint32_t numStrands; // number of strands in the world |
22 |
void **inState; |
void **inState; |
23 |
void **outState; |
void **outState; |
24 |
bool *isActive; // array of isActive flags |
uint8_t *status; // array of strand status flags |
25 |
}; |
}; |
26 |
|
|
27 |
extern float getOutf (void *self); |
extern float getOutf (void *self); |
47 |
while (nActive > 0) { |
while (nActive > 0) { |
48 |
nSteps++; |
nSteps++; |
49 |
// update strands |
// update strands |
50 |
|
bool existsStabilizing = false; |
51 |
for (int i = 0; i < wrld->numStrands; i++) { |
for (int i = 0; i < wrld->numStrands; i++) { |
52 |
if (wrld->isActive[i]) { |
if (! wrld->status[i]) { |
53 |
nUpdates++; |
nUpdates++; |
54 |
StrandStatus_t sts = Diderot_Strands[0]->update(wrld->inState[i], wrld->outState[i]); |
StrandStatus_t sts = Diderot_Strands[0]->update(wrld->inState[i], wrld->outState[i]); |
55 |
switch (sts) { |
switch (sts) { |
56 |
case DIDEROT_STABILIZE: |
case DIDEROT_STABILIZE: |
57 |
// copy out to in so that both copies are the stable state |
existsStabilizing = true; |
58 |
// FIXME: there is a race condition here, since other strands might query this strand |
wrld->status[i] = DIDEROT_STABILIZE; |
|
memcpy (wrld->inState[i], wrld->outState[i], Diderot_Strands[0]->stateSzb); |
|
|
wrld->isActive[i] = false; |
|
|
nActive--; |
|
59 |
break; |
break; |
60 |
case DIDEROT_DIE: |
case DIDEROT_DIE: |
61 |
// FIXME: need more than booleans here, since we have to differentiate between dead and stable strands |
wrld->status[i] = DIDEROT_DIE; |
|
wrld->isActive[i] = false; |
|
62 |
nActive--; |
nActive--; |
63 |
break; |
break; |
64 |
} |
} |
65 |
} |
} |
66 |
} |
} |
67 |
|
if (existsStabilizing) { |
68 |
|
for (int i = 0; i < wrld->numStrands; i++) { |
69 |
|
// NOTE: we may want to compact the array of strands |
70 |
|
if (wrld->status[i] == DIDEROT_STABILIZE) { |
71 |
|
// copy out to in so that both copies are the stable state |
72 |
|
memcpy (wrld->inState[i], wrld->outState[i], Diderot_Strands[0]->stateSzb); |
73 |
|
wrld->status[i] = DIDEROT_STABLE; |
74 |
|
nActive--; |
75 |
|
} |
76 |
|
} |
77 |
|
} |
78 |
// swap in and out |
// swap in and out |
79 |
void **tmp = wrld->inState; |
void **tmp = wrld->inState; |
80 |
wrld->inState = wrld->outState; |
wrld->inState = wrld->outState; |
90 |
} |
} |
91 |
|
|
92 |
for (int i = 0; i < wrld->numStrands; i++) { |
for (int i = 0; i < wrld->numStrands; i++) { |
93 |
|
if (wrld->status[i] == DIDEROT_STABLE) |
94 |
Diderot_Strands[0]->print (outS, wrld->inState[i]); |
Diderot_Strands[0]->print (outS, wrld->inState[i]); |
95 |
} |
} |
96 |
fclose (outS); |
fclose (outS); |
139 |
wrld->numStrands = numStrands; |
wrld->numStrands = numStrands; |
140 |
wrld->inState = (void **) malloc (numStrands * sizeof(void *)); |
wrld->inState = (void **) malloc (numStrands * sizeof(void *)); |
141 |
wrld->outState = (void **) malloc (numStrands * sizeof(void *)); |
wrld->outState = (void **) malloc (numStrands * sizeof(void *)); |
142 |
wrld->isActive = (bool *) malloc (numStrands * sizeof(bool)); |
wrld->status = (uint8_t *) malloc (numStrands * sizeof(uint8_t)); |
143 |
if ((wrld->inState == 0) || (wrld->outState == 0) || (wrld->isActive == 0)) { |
if ((wrld->inState == 0) || (wrld->outState == 0) || (wrld->status == 0)) { |
144 |
fprintf (stderr, "unable to allocate strand states\n"); |
fprintf (stderr, "unable to allocate strand states\n"); |
145 |
exit (1); |
exit (1); |
146 |
} |
} |
149 |
for (size_t i = 0; i < numStrands; i++) { |
for (size_t i = 0; i < numStrands; i++) { |
150 |
wrld->inState[i] = Diderot_AllocStrand (strand); |
wrld->inState[i] = Diderot_AllocStrand (strand); |
151 |
wrld->outState[i] = Diderot_AllocStrand (strand); |
wrld->outState[i] = Diderot_AllocStrand (strand); |
152 |
wrld->isActive[i] = true; |
wrld->status[i] = DIDEROT_ACTIVE; |
153 |
} |
} |
154 |
|
|
155 |
return wrld; |
return wrld; |
172 |
bool Diderot_IsActive (Diderot_World_t *wrld, uint32_t i) |
bool Diderot_IsActive (Diderot_World_t *wrld, uint32_t i) |
173 |
{ |
{ |
174 |
assert (i < wrld->numStrands); |
assert (i < wrld->numStrands); |
175 |
return wrld->isActive[i]; |
return !wrld->status[i]; |
176 |
} |
} |