Skip to content

Commit

Permalink
Destroy reactors when they finished their job
Browse files Browse the repository at this point in the history
  • Loading branch information
axmmisaka committed Sep 1, 2023
1 parent c90e73a commit a495f75
Showing 1 changed file with 52 additions and 10 deletions.
62 changes: 52 additions & 10 deletions src/benchmark/quicksort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class QuickSorter extends Reactor {
resultArr: State<number[]>;
numFragments: State<number>;

leftReactor: Reactor | undefined;
rightReactor: Reactor | undefined;
leftReactor: State<QuickSorter | undefined>;
rightReactor: State<QuickSorter | undefined>;

constructor(parent: Reactor, name = "root") {
super(parent, name);
Expand All @@ -57,6 +57,9 @@ class QuickSorter extends Reactor {
this.resultArr = new State([]);
this.numFragments = new State(0);

this.leftReactor = new State(void 114514);

Check failure on line 60 in src/benchmark/quicksort.ts

View workflow job for this annotation

GitHub Actions / lint

Expected 'undefined' and instead saw 'void'
this.rightReactor = new State(void 1919810);

Check failure on line 61 in src/benchmark/quicksort.ts

View workflow job for this annotation

GitHub Actions / lint

Expected 'undefined' and instead saw 'void'

// When the parent sends a message, we send it to children.
this.addMutation(
[this.parentReadPort],
Expand All @@ -68,7 +71,9 @@ class QuickSorter extends Reactor {
this.leftReadPort,
this.rightReadPort,
this.resultArr,
this.numFragments
this.numFragments,
this.leftReactor,
this.rightReactor
],
function (
this,
Expand All @@ -79,7 +84,9 @@ class QuickSorter extends Reactor {
leftread,
rightread,
resultArr,
numFragments
numFragments,
s_leftreactor, // This is really cursed, but s_ is to indicate that this is a state

Check warning on line 88 in src/benchmark/quicksort.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter name `s_leftreactor` must match one of the following formats: camelCase
s_rightreactor

Check warning on line 89 in src/benchmark/quicksort.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter name `s_rightreactor` must match one of the following formats: camelCase
) {
const hierarchyImplementation = (
useHierarchy
Expand Down Expand Up @@ -118,6 +125,9 @@ class QuickSorter extends Reactor {
`${this.getReactor()._name}/r`
);

s_leftreactor.set(leftReactor);
s_rightreactor.set(rightReactor);

// Connect ports accoringly
this.connect(leftWritePort, leftReactor.parentReadPort);
this.connect(rightWritePort, rightReactor.parentReadPort);
Expand All @@ -130,17 +140,26 @@ class QuickSorter extends Reactor {
}
);

this.addReaction(
this.addMutation(
[this.leftReadPort],
[
this.leftReadPort,
this.resultArr,
this.numFragments,
this.writable(this.parentWritePort)
this.writable(this.parentWritePort),
this.leftReactor
],
function (this, leftreadport, resultArr, numFragments, parentWrite) {
function (
this,
leftreadport,
resultArr,
numFragments,
parentWrite,
s_leftreactor
) {
const leftResult = leftreadport.get();
const myResult = resultArr.get();
const leftReactor = s_leftreactor.get();
if (leftResult == null) {
throw Error("Left return null");
}
Expand All @@ -149,28 +168,43 @@ class QuickSorter extends Reactor {
"Result length is 0, but should contain at least the pivots."
);
}
if (leftReactor == null) {
throw Error("Right reactor is null.");
}

console.log(`I received a result from my left! ${leftResult}!`);
resultArr.set([...leftResult, ...myResult]);

this.delete(leftReactor);
s_leftreactor.set(void "kksk");

numFragments.set(numFragments.get() + 1);
if (numFragments.get() === 3) {
parentWrite.set(resultArr.get());
}
}
);

this.addReaction(
this.addMutation(
[this.rightReadPort],
[
this.rightReadPort,
this.resultArr,
this.numFragments,
this.writable(this.parentWritePort)
this.writable(this.parentWritePort),
this.rightReactor
],
function (this, rightreadport, resultArr, numFragments, parentWrite) {
function (
this,
rightreadport,
resultArr,
numFragments,
parentWrite,
s_rightreactor
) {
const rightResult = rightreadport.get();
const myResult = resultArr.get();
const rightReactor = s_rightreactor.get();
if (rightResult == null) {
throw Error("Right return null");
}
Expand All @@ -179,10 +213,18 @@ class QuickSorter extends Reactor {
"Result length is 0, but should contain at least the pivots."
);
}
if (rightReactor == null) {
throw Error("Right reactor is null.");
}

console.log(`I received a result from my right! ${rightResult}!`);
resultArr.set([...myResult, ...rightResult]);

// Destroy right reactor and the connection

this.delete(rightReactor);
s_rightreactor.set(void "tnok");

numFragments.set(numFragments.get() + 1);
if (numFragments.get() === 3) {
parentWrite.set(resultArr.get());
Expand Down

0 comments on commit a495f75

Please sign in to comment.