Commit 945aac1a authored by NzSN's avatar NzSN

Add CpySchedule.

parent ee63ae87
...@@ -22,19 +22,32 @@ function makeMessage(msgType, datas) { ...@@ -22,19 +22,32 @@ function makeMessage(msgType, datas) {
return {type:msgType, data: datas}; return {type:msgType, data: datas};
} }
class CpySchedule {
first = { pos: 0, size: 0 }
second = { pos: 0, size: 0 }
}
/* An abstraction that help to /* An abstraction that help to
* easily to transfer datas to target worker * easily to transfer datas to target worker
* in real time manner */ * in real time manner */
export class Channel { export class Channel {
/* | ReadPointer (4 bytes) | WritePointer (4 Bytes) | Data Area (1-n bytes) */ /* */
/* Size of memory area that help to /* Size of memory area that help to
* maintain Channel. Types of meta info * maintain Channel. Types of meta info
* are shown below: * are shown below:
* *
* 1.Write Position * 1.Write Position
* 2.Read Position */ * 2.Read Position
*
* Figure:
* ------------------------------------------------------------------------
* | ReadPointer (4 bytes) | WritePointer (4 Bytes) | Data Area (N bytes) |
* -------------------------------------------------------------------------
* where N >= 2
*
* */
#numOfMetaField = 2 #numOfMetaField = 2
#fieldSize = 0; #fieldSize = 0;
...@@ -54,6 +67,9 @@ export class Channel { ...@@ -54,6 +67,9 @@ export class Channel {
// size's unit is byte // size's unit is byte
// bufferType parameter is mainly for testability. // bufferType parameter is mainly for testability.
constructor(WW, size, bufferType = SharedArrayBuffer) { constructor(WW, size, bufferType = SharedArrayBuffer) {
assert(size >= 2, `Channel require its data area has at least 2 Bytes.`)
this.#WW = WW; this.#WW = WW;
this.#size = size; this.#size = size;
...@@ -70,6 +86,8 @@ export class Channel { ...@@ -70,6 +86,8 @@ export class Channel {
this.#view.setUint32(0, this.#metaSize); this.#view.setUint32(0, this.#metaSize);
this.#view.setUint32(4, this.#metaSize); this.#view.setUint32(4, this.#metaSize);
// FIXME: Need to transfer channel itself to worker.
} }
#readPos() { #readPos() {
...@@ -104,20 +122,21 @@ export class Channel { ...@@ -104,20 +122,21 @@ export class Channel {
* make sure there has enough spaces.*/ * make sure there has enough spaces.*/
#cpySchedule(size) { #cpySchedule(size) {
let firstCpySize = 0, secondCpySize = 0, spaceToTail = 0; let firstCpySize = 0, secondCpySize = 0, spaceToTail = 0;
let schedule; let schedule = new CpySchedule();
if (this.#writePos >= this.#readPos()) { if (this.#writePos >= this.#readPos()) {
spaceToTail = this.#endPos - this.#writePos; spaceToTail = this.#endPos - this.#writePos;
firstCpySize = Math.min(size, spaceToTail); firstCpySize = Math.min(size, spaceToTail);
secondCpySize = firstCpySize < size ? size - firstCpySize : 0; secondCpySize = firstCpySize < size ? size - firstCpySize : 0;
schedule = { schedule.first.pos = this.#writePos;
first : { pos: this.#writePos, size: firstCpySize }, schedule.first.size = firstCpySize;
second: { pos: secondCpySize > 0 ? this.#metaSize : 0, size: secondCpySize } schedule.second.pos = secondCpySize > 0 ? this.#metaSize : 0;
}; schedule.second.size = secondCpySize;
} else { } else {
schedule = { first : { pos: this.#writePos, size: firstCpySize }, schedule.first.pos = this.#writePos;
second: { pos: 0, size: 0 } }; schedule.first.size = firstCpySize;
} }
return schedule; return schedule;
...@@ -182,15 +201,17 @@ export class Channel { ...@@ -182,15 +201,17 @@ export class Channel {
let schedule = this.#cpySchedule(data.byteLength); let schedule = this.#cpySchedule(data.byteLength);
// Perfrom write schedule // Perfrom write schedule
let srcPos = 0; let srcPos = 0, plan;
for (let key in schedule) { for (let key in schedule) {
let plan = schedule[key]; plan = schedule[key];
if (plan.size == 0) if (plan.size == 0)
continue; continue;
let d_ = data.slice(srcPos, srcPos+plan.size); this.#buffer.set(
this.#buffer.set(d_, plan.pos) data.slice(srcPos, srcPos+plan.size), plan.pos)
srcPos = srcPos + plan.size; srcPos += plan.size;
writePos = plan.pos+plan.size; writePos = plan.pos+plan.size;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment