brute force kill/recreate worker for cancellation

This commit is contained in:
Stewart Allen 2018-03-08 10:42:26 -05:00
commit c85bad54ca
4 changed files with 36 additions and 82 deletions

View file

@ -257,15 +257,11 @@ var gs_kiri_slicer = exports;
}
}
/* -- synchronous slice -- can't be cancelled --
// create a Slice for each z offset in the zIndexes array
for (var i = 0; i < zIndexes.length; i++) {
// slice next layer and add to slices[] array
sliceZ(zIndexes[i], zHeights[i]);
// kill slicing if onupdate() returns 42
if (onupdate(i / zIndexes.length) === 42) {
return ondone(null);
}
onupdate(i / zIndexes.length);
}
// for cam, mark top and bottom as mandatory (hasFlats)
@ -284,39 +280,6 @@ var gs_kiri_slicer = exports;
// pass Slices array back to ondone function
ondone(slices);
*/
var sliceIndex = 0;
function kickSlice() {
sliceZ(zIndexes[sliceIndex], zHeights[sliceIndex]);
// kill slicing if onupdate() returns 42
if (onupdate(sliceIndex / zIndexes.length) === 42) {
return ondone(null);
}
if (++sliceIndex < zIndexes.length) {
setTimeout(kickSlice, 0);
} else {
// for cam, mark top and bottom as mandatory (hasFlats)
if (options.cam && slices.length > 0) {
slices[0].hasFlats = true;
slices[slices.length-1].hasFlats = true;
}
// connect slices into linked list for island/bridge projections
for (i=1; i<slices.length; i++) {
slices[i-1].up = slices[i];
slices[i].down = slices[i-1];
}
slices.slice_time = time() - timeStart;
// pass Slices array back to ondone function
ondone(slices);
}
}
kickSlice();
/** ***** SLICING FUNCTIONS ***** */

View file

@ -87,8 +87,6 @@ var gs_kiri_widget = exports;
progress: 0
};
this.saved = false;
// cancel slice/print ops
this.cancel = false;
}
/** ******************************************************************
@ -530,7 +528,6 @@ var gs_kiri_widget = exports;
startTime = UTIL.time();
widget.settings = settings;
widget.cancel = false;
onupdate(0.0001, "slicing");
@ -548,10 +545,9 @@ var gs_kiri_widget = exports;
if (reply.slices) { widget.clearSlices(); widget.slices = [] };
if (reply.slice) widget.slices.push(KIRI.codec.decode(reply.slice, {mesh:widget.mesh}));
if (reply.error) {
widget.cancel = true;
ondone(false, reply.error);
}
if (reply.done && !widget.cancel) {
if (reply.done) {
widget.modified = false;
ondone(true);
}
@ -577,12 +573,6 @@ var gs_kiri_widget = exports;
var catchupdate = function(progress, message) {
onupdate(progress, message);
if (widget.cancel) {
ondone("canceled");
return 42;
} else {
return 0;
}
};
var driver = null;

View file

@ -16,7 +16,8 @@ if (self.window) {
BASE = self.base,
seqid = 1,
running = {},
worker = new Worker(pre + "/code/worker.js/" + exports.VERSION);
slicing = {},
worker = null;
// new moto.Ajax(function(body) {
// console.log({body:body});
@ -24,20 +25,6 @@ if (self.window) {
// worker = new Worker(URL.createObjectURL(blob));
// }).request(pre + "/code/worker.js/");
worker.onmessage = function(e) {
var now = time(),
reply = e.data,
record = running[reply.seq],
onreply = record.fn;
if (reply.done) delete running[reply.seq];
// calculate and replace recv time
reply.time_recv = now - reply.time_recv;
onreply(reply.data, reply);
};
function send(fn, data, onreply, async, zerocopy) {
var seq = seqid++;
@ -52,6 +39,32 @@ if (self.window) {
}
KIRI.work = {
restart : function() {
if (worker) worker.terminate();
for (var key in slicing) {
slicing[key]({error: "cancelled"});
}
slicing = {};
running = {};
worker = new Worker(pre + "/code/worker.js/" + exports.VERSION);
worker.onmessage = function(e) {
var now = time(),
reply = e.data,
record = running[reply.seq],
onreply = record.fn;
if (reply.done) delete running[reply.seq];
// calculate and replace recv time
reply.time_recv = now - reply.time_recv;
onreply(reply.data, reply);
};
},
decimate : function(vertices, callback) {
var vertices = vertices.buffer.slice(0);
send("decimate", vertices, function(output) {
@ -65,18 +78,16 @@ if (self.window) {
});
},
cancel : function(widget) {
send("cancel", widget ? {id:widget.id} : {});
},
slice : function(settings, widget, callback) {
var vertices = widget.getGeoVertices().buffer.slice(0);
slicing[widget.id] = callback;
send("slice", {
id: widget.id,
settings: settings,
vertices: vertices,
position: widget.mesh.position
}, function(reply) {
if (reply.done || reply.error) delete slicing[widget.id];
callback(reply);
}, null, [vertices]);
},
@ -115,6 +126,9 @@ if (self.window) {
}
};
// start first worker
KIRI.work.restart();
} else {
module = { exports: {} };
@ -164,19 +178,6 @@ if (self.window) {
send.done(vertices);
},
cancel: function(data) {
if (data.id) {
var widget = cache[data.id];
if (widget) widget.cancel = true;
} else {
for (var id in cache) {
if (cache.hasOwnProperty(id)) {
cache[id].cancel = true;
}
}
}
},
slice: function(data, send) {
var settings = data.settings,
vertices = new Float32Array(data.vertices),

View file

@ -2551,7 +2551,7 @@ self.kiri.license = exports.LICENSE;
// hide all dialogs
hideDialog();
// cancel slicing
KIRI.work.cancel();
KIRI.work.restart();
break;
}
return false;