Skip to content

Commit

Permalink
Merge pull request #17 from CS4215-OOGA/optimize-waitgroup
Browse files Browse the repository at this point in the history
Optimise waitgroup
  • Loading branch information
JothamWong authored Apr 15, 2024
2 parents cab1fda + fb04d89 commit f0f5e69
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/parser/ooga.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,6 @@ FunctionDeclaration
};
}

// Arnav: Not sure what this is for!!
FunctionExpression
= FunctionToken __ id:(Identifier __)?
"(" __ params:(FormalParameterList __)? ")" __
Expand Down Expand Up @@ -1325,4 +1324,4 @@ BreakpointStatement
return {
tag: "BreakpointStatement"
};
}
}
15 changes: 14 additions & 1 deletion src/vm/oogavm-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ function timeoutThread() {
// Handbook for developing new built-in functions
// Builtins need to return heap addresses, if they dont return any
// useful value, return either heap.True or heap.Undefined

// Used to either block the thread or voluntarily yield thread without signalling blocked
let blockedThreadState = false;
let yieldThreadState = false;

export const builtinMappings = {
Expand Down Expand Up @@ -277,6 +280,10 @@ export const builtinMappings = {
getThreadID: () => {
return TSValueToAddress(currentThreadId);
},
blockThread: () => {
blockedThreadState = true;
return null;
},
yieldThread: () => {
yieldThreadState = true;
return null;
Expand Down Expand Up @@ -1207,9 +1214,15 @@ function runInstruction() {
const instr = instrs[PC++];
log(instr);
microcode[instr.tag](instr);

if (blockedThreadState) {
blockedThreadState = false;
blockThread();
}

if (yieldThreadState) {
yieldThreadState = false;
blockThread();
timeoutThread();
}

if (!isAtomicSection) {
Expand Down
1 change: 1 addition & 0 deletions src/vm/oogavm-typechecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ const global_type_frame = {
startAtomic: new FunctionType([], new NullType()),
endAtomic: new FunctionType([], new NullType()),
yieldThread: new FunctionType([], new NullType()),
blockThread: new FunctionType([], new NullType()),
getThreadID: new FunctionType([], new IntegerType()),
oogaError: new FunctionType([], new NullType()),
};
Expand Down
3 changes: 2 additions & 1 deletion std/ooga-std.ooga
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (wg *WaitGroup) Done() {

func (wg *WaitGroup) Wait() {
for wg.counter > 0 {
yieldThread(); // yield to avoid wasteful loop
}
}

Expand All @@ -45,7 +46,7 @@ func (m *Mutex) Lock() {
}
// else
endAtomic();
yieldThread(); // block and loop until can pick up lock
blockThread(); // block and loop until can pick up lock
}
m.currentThread = getThreadID();
endAtomic();
Expand Down

0 comments on commit f0f5e69

Please sign in to comment.