Skip to content

Commit

Permalink
Swapping between threads in Blockly
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienTainon committed May 6, 2024
1 parent 8bfddd3 commit 3fed1e4
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 28 deletions.
1 change: 1 addition & 0 deletions frontend/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ log.getLogger('editor').setDefaultLevel('info');
log.getLogger('hints').setDefaultLevel('info');
log.getLogger('layout').setDefaultLevel('info');
log.getLogger('libraries').setDefaultLevel('info');
log.getLogger('multithread').setDefaultLevel('info');
log.getLogger('performance').setDefaultLevel('info');
log.getLogger('platform').setDefaultLevel('info');
log.getLogger('player').setDefaultLevel('info');
Expand Down
18 changes: 18 additions & 0 deletions frontend/stepper/abstract_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default abstract class AbstractRunner {
public _nbActions = 0;
public _allowStepsWithoutDelay = 0;
protected _timeouts = [];
protected threads: any[] = [];
protected currentThreadId = null;

constructor(context) {
context.runner = this;
Expand Down Expand Up @@ -119,4 +121,20 @@ export default abstract class AbstractRunner {

public createNewThread(threadData: any) {
}

public registerNewThread(threadData: any) {
this.threads.push(threadData);
this.currentThreadId = this.threads.length - 1;
}

public getAllThreads() {
return this.threads;
}

public getCurrentThreadId() {
return this.currentThreadId;
}

public swapCurrentThreadId(newThreadId: number) {
}
}
2 changes: 1 addition & 1 deletion frontend/stepper/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ async function executeSingleStep(stepperContext: StepperContext) {
stepperContext.hasMadeFinalInteract = false;

if (stepperContext.quickAlgoContext) {
await stepperContext.quickAlgoContext.checkEventListeners();
await stepperContext.quickAlgoContext.multiThreadingPreExecute();
}

await Codecast.runner.runNewStep(stepperContext, stepperContext.noInteractive);
Expand Down
28 changes: 19 additions & 9 deletions frontend/stepper/js/blockly_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,12 @@ export default class BlocklyRunner extends AbstractRunner {
break;
}
if (!interpreter.step() || this.toStopInterpreter[iInterpreter]) {
log.getLogger('blockly_runner').debug('interpreter not running');
this.isRunningInterpreter[iInterpreter] = false;
log.getLogger('blockly_runner').debug('interpreter not running', this.toStopInterpreter[iInterpreter]);
this._stepInProgress = false;
this.currentThreadFinished();
if (this.executeOnResolve) {
this.executeOnResolve();
}
return;
}
if (interpreter.paused_) {
Expand Down Expand Up @@ -522,6 +526,7 @@ export default class BlocklyRunner extends AbstractRunner {
this.nodesReady.push(true);
this.isRunningInterpreter[iInterpreter] = true;
this.toStopInterpreter[iInterpreter] = false;
this.registerNewThread(this.interpreters[0].stateStack);

if(iInterpreter > 0) {
// This is a fix for pseudoToNative identity comparisons (===),
Expand Down Expand Up @@ -650,20 +655,25 @@ export default class BlocklyRunner extends AbstractRunner {
}

public createNewThread(threadData: any) {
console.log('create thread', threadData);
log.getLogger('multithread').debug('[multithread] create thread', threadData);

const globalScope = this.interpreters[0].global;
const node = threadData.node;
const stack = new window.Interpreter.State(node['body'], globalScope);
console.log('stack', stack);
log.getLogger('multithread').debug('[multithread] stack', stack);

this.swapCurrentThread(stack);
this.registerNewThread([stack]);
}

return 5;
public swapCurrentThreadId(newThreadId: number) {
this.currentThreadId = newThreadId;
const threads = this.getAllThreads();
const stack = threads[newThreadId];
log.getLogger('multithread').debug('[multithread] change current thread', stack);
this.interpreters[0].stateStack = stack;
}

public swapCurrentThread(stack) {
// var stack = threads[n];
this.interpreters[0].stateStack = [stack];
public currentThreadFinished() {
this.threads.splice(this.currentThreadId, 1);
}
}
20 changes: 16 additions & 4 deletions frontend/stepper/python/python_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ export default class PythonRunner extends AbstractRunner {
}

this._resetInterpreterState();
this.registerNewThread(this._debugger.suspension_stack);

Sk.running = true;
this._isRunning = true;
Expand Down Expand Up @@ -736,11 +737,22 @@ export default class PythonRunner extends AbstractRunner {
}

public createNewThread(threadData) {
const result = threadData();
console.log('callback result', result);
result
const promise = threadData();
log.getLogger('multithread').debug('create new thread -> promise', promise);

this.registerNewThread(promise);

promise
.then((aaa) => {
console.log('aaa', aaa);
log.getLogger('multithread').debug('end of thread', aaa);
})
}

public swapCurrentThreadId(newThreadId: number) {
this.currentThreadId = newThreadId;
const threads = this.getAllThreads();
const stack = threads[newThreadId];
console.log('change current thread', stack);

}
}
29 changes: 20 additions & 9 deletions frontend/task/libs/quickalgo_library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,23 +415,26 @@ export abstract class QuickAlgoLibrary {
callback,
justActivated: false,
});
console.log('event listeners', this.eventListeners);
log.getLogger('multithread').debug('[multithread] event listeners', this.eventListeners);
}

async multiThreadingPreExecute() {
log.getLogger('multithread').debug('[multithread] multi-threading pre-execute');
await this.checkEventListeners();
this.scheduleAppropriateThread();
}

async checkEventListeners() {
console.log('check event listeners', this.waitingEventListener);
if (this.waitingEventListener) {
return;
}
log.getLogger('multithread').debug('[multithread] check event listeners');
for (let listener of this.eventListeners) {
const result = await this.isEventListenerConditionActive(listener);
console.log('button is ', result);
log.getLogger('multithread').debug('button is ', result);
if (result) {
if (!listener.justActivated) {
listener.justActivated = true;
console.log('before trigger event listener');
log.getLogger('multithread').debug('[multithread] before trigger event listener');
this.triggerEventListener(listener);
console.log('after trigger event listener');
log.getLogger('multithread').debug('[multithread] after trigger event listener');
}
} else {
listener.justActivated = false;
Expand All @@ -447,9 +450,17 @@ export abstract class QuickAlgoLibrary {

triggerEventListener(listener: LibraryEventListener) {
// this.waitingEventListener = true;
console.log('trigger event listener', listener.callback);
log.getLogger('multithread').debug('[multithread] trigger event listener', listener.callback);

this.runner.createNewThread(listener.callback);
}

scheduleAppropriateThread() {
const threads = this.runner.getAllThreads();
let currentThreadId = this.runner.getCurrentThreadId();
currentThreadId = (currentThreadId + 1) % threads.length;
log.getLogger('multithread').debug('[multithread] schedule thread', {threads, currentThreadId});
this.runner.swapCurrentThreadId(currentThreadId);
}
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@fortawesome/free-solid-svg-icons": "^5.15.4",
"@fortawesome/react-fontawesome": "^0.1.16",
"@france-ioi/persistent-c": "11.2.10",
"@france-ioi/skulpt": "1.3.8",
"@france-ioi/skulpt": "1.3.10",
"@hedgedoc/html-to-react": "^1.4.6",
"@reduxjs/toolkit": "^1.7.1",
"@taquito/michel-codec": "^17.0.0",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1370,10 +1370,10 @@
immutable "^3.7.6"
text-encoding-utf-8 "^1.0.1"

"@france-ioi/[email protected].8":
version "1.3.8"
resolved "https://npm.pkg.github.com/download/@France-ioi/skulpt/1.3.8/a8caed60ded9e69b7dc2d315ae61bdfc24ecf1af#a8caed60ded9e69b7dc2d315ae61bdfc24ecf1af"
integrity sha512-ErceTl8muiM3S9UiRNTTkZJ3IfCBQ9lpDTq5mJyU9oZBkA1FWZ5Rt3/B4jlDhu3ju7FzUzaGJTcBhg/IH8BFXQ==
"@france-ioi/[email protected].10":
version "1.3.10"
resolved "https://npm.pkg.github.com/download/@France-ioi/skulpt/1.3.10/934e5404b802a902147e737c258ce384b69a0922#934e5404b802a902147e737c258ce384b69a0922"
integrity sha512-cckVPAXBP/UjFzE2rFkItbWNWCguk7AiuWfuJUcfXtOqBOnI+BUk4HcAMtTZ3gSIC1tL58CQSw7OMIdRqYB2xg==
dependencies:
jsbi "^3.1.4"
uuid "^8.2.0"
Expand Down

0 comments on commit 3fed1e4

Please sign in to comment.