Kode / Kha

Ultra-portable, high performance, open source multimedia framework.
http://kha.tech
zlib License
1.49k stars 174 forks source link

Spawning multiple kha.Worker behaves wrong on native target #1319

Closed SunDaw closed 3 years ago

SunDaw commented 3 years ago

Spawning multiple workers behaves differently between HTML5 and native targets, e.g.:

static var w1:Worker;
static var w2:Worker;
static var worker1Message:Dynamic = null;
static var worker2Message:Dynamic = null;

// Spawned workers on startup
w1 = Worker.create(Worker1);
w1.notify(data -> {
    worker1Message = data;
});
w2 = Worker.create(Worker2);
w2.notify(data -> {
    worker2Message = data;
});

Will correctly set worker1Message and worker2Message on HTML5, but will only set worker1Message on native (and only use the postMessage of Worker2)

There seem to be 2 issues:

  1. There is only one receiver function (https://github.com/Kode/Kha/blob/master/Sources/kha/Worker.hx#L108) which gets overwritten by each worker
  2. The callback set by notify is not able to distinguish which worker/thread has sent the message (https://github.com/Kode/Kha/blob/master/Sources/kha/Worker.hx#L97)
SunDaw commented 3 years ago

I managed to hack together a quick fix in https://github.com/SunDaw/Kha/commit/b50d67e6e2ea0dadb6f0c9308d6f42da9036d61f but I'm sure it can be improved on