Skip to content

Commit

Permalink
TaskManager: Remove need for execution loop requiring wait
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisalmen committed Sep 2, 2020
1 parent 1f80005 commit 424c4fc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 60 deletions.
4 changes: 2 additions & 2 deletions examples/jsm/taskmanager/TaskManager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export class TaskManager {
registerTaskType(taskType: string, initFunction: Function, executeFunction: Function, comRoutingFunction: Function, fallback: boolean, dependencyDescriptions?: any[]): boolean;
registerTaskTypeModule(taskType: string, workerModuleUrl: string): boolean;
initTaskType(taskType: string, config: object, transferables?: any): Promise<void>;
wait(milliseconds: any): Promise<any>;
_wait(milliseconds: any): Promise<any>;
enqueueForExecution(taskType: string, config: object, assetAvailableFunction: Function, transferables?: any): Promise<any>;
_kickExecutions(): Promise<void>;
_depleteExecutions(): Promise<void>;
dispose(): TaskManager;
}
declare class WorkerTypeDefinition {
Expand Down
95 changes: 41 additions & 54 deletions examples/jsm/taskmanager/TaskManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class TaskManager {
*/
this.storedExecutions = [];
this.teardown = false;
this._kickExecutions().then( x => console.log( 'Teared down: ' + x ) );

}

Expand Down Expand Up @@ -167,7 +166,7 @@ class TaskManager {

while ( ! workerTypeDefinition.status.initComplete ) {

await this.wait( 10 );
await this._wait( 10 );

}

Expand All @@ -177,7 +176,7 @@ class TaskManager {

}

async wait ( milliseconds ) {
async _wait ( milliseconds ) {

return new Promise(resolve => {

Expand All @@ -201,85 +200,73 @@ class TaskManager {
let localPromise = new Promise( ( resolveUser, rejectUser ) => {

this.storedExecutions.push( new StoredExecution( taskType, config, assetAvailableFunction, resolveUser, rejectUser, transferables ) );
this._depleteExecutions();

} );
return localPromise;

}

async _kickExecutions () {
_depleteExecutions () {

while ( ! this.teardown ) {
let counter = 0;
while ( this.actualExecutionCount < this.maxParallelExecutions && counter < this.storedExecutions.length ) {

let counter = 0;
while ( this.storedExecutions.length > 0 ) {
// TODO: storedExecutions and results from worker seem to get mixed up
let storedExecution = this.storedExecutions[ counter ];
let workerTypeDefinition = this.taskTypes.get( storedExecution.taskType );
let taskWorker = workerTypeDefinition.getAvailableTask();
if ( taskWorker ) {

if ( this.actualExecutionCount < this.maxParallelExecutions && counter < this.storedExecutions.length ) {
this.storedExecutions.splice( counter, 1 );
this.actualExecutionCount ++;
let promiseWorker = new Promise( ( resolveWorker, rejectWorker ) => {

// TODO: storedExecutions and results from worker seem to get mixed up
let storedExecution = this.storedExecutions[ counter ];
let workerTypeDefinition = this.taskTypes.get( storedExecution.taskType );
let taskWorker = workerTypeDefinition.getAvailableTask();
if ( taskWorker ) {
taskWorker.onmessage = function ( e ) {

this.storedExecutions.splice( counter, 1 );
this.actualExecutionCount ++;
let promiseWorker = new Promise( ( resolveWorker, rejectWorker ) => {
// allow intermediate asset provision before flagging execComplete
if ( e.data.cmd === 'assetAvailable' ) {

taskWorker.onmessage = function ( e ) {
if ( storedExecution.assetAvailableFunction instanceof Function ) {

// allow intermediate asset provision before flagging execComplete
if ( e.data.cmd === 'assetAvailable' ) {
storedExecution.assetAvailableFunction( e.data );

if ( storedExecution.assetAvailableFunction instanceof Function ) {
}

storedExecution.assetAvailableFunction( e.data );
} else {

}
resolveWorker( e );

} else {
}

resolveWorker( e );
};
taskWorker.onerror = rejectWorker;

}
taskWorker.postMessage( {
cmd: "execute",
workerId: taskWorker.getId(),
config: storedExecution.config
}, storedExecution.transferables );

};
taskWorker.onerror = rejectWorker;
} );
promiseWorker.then( ( e ) => {

taskWorker.postMessage( {
cmd: "execute",
workerId: taskWorker.getId(),
config: storedExecution.config
}, storedExecution.transferables );
workerTypeDefinition.returnAvailableTask( taskWorker );
storedExecution.resolve( e.data );
this.actualExecutionCount --;
this._depleteExecutions();

} );
promiseWorker.then( ( e ) => {
} ).catch( ( e ) => {

workerTypeDefinition.returnAvailableTask( taskWorker );
storedExecution.resolve( e.data );
this.actualExecutionCount --;
storedExecution.reject( "Execution error: " + e );

} ).catch( ( e ) => {
} );

storedExecution.reject( "Execution error: " + e );
} else {

} );

} else {

counter ++;

}

} else {

counter = 0;
await this.wait( 1 );

}
counter ++;

}
await this.wait( 1 );

}

Expand Down
8 changes: 4 additions & 4 deletions examples/webgl_loader_taskmanager.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,26 @@
this.taskDescriptions.clear();
this.taskDescriptions.set( 'tmProtoExample', {
name: 'tmProtoExample',
use: false,
use: true,
fallback: false,
funcInit: WorkerFunctions.workerLegacyInit,
funcExec: WorkerFunctions.workerLegacyExec
} );
this.taskDescriptions.set( 'tmProtoExampleModule', {
name: 'tmProtoExampleModule',
use: false,
use: true,
fallback: false,
module: './jsm/taskmanager/worker/tmModuleExample.js'
} );
this.taskDescriptions.set( 'tmProtoExampleModuleNoThree', {
name: 'tmProtoExampleModuleNoThree',
use: false,
use: true,
fallback: false,
module: './jsm/taskmanager/worker/tmModuleExampleNoThree.js'
} );
this.taskDescriptions.set( 'tmProtoExampleMain', {
name: 'tmProtoExampleMain',
use: false,
use: true,
fallback: true,
funcInit: WorkerFunctions.workerLegacyInit,
funcExec: WorkerFunctions.workerLegacyExec
Expand Down

0 comments on commit 424c4fc

Please sign in to comment.