Skip to content

Commit 4b7223b

Browse files
committed
Replace thread blocking queue with a map
There seems to be no reason why we have to have an O(n) unblocking logic. Furthermore, this is much cleaner.
1 parent 121a90f commit 4b7223b

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

rt/src/Scheduler.mts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class Scheduler implements SchedulerInterface {
4141
__funloop: Thread[];
4242

4343
/** Queue of blocked threads. */
44-
__blocked: Thread[];
44+
__blocked: { [tid in string]: Thread };
4545

4646
/** Map of alive threads from their stringified identifier, `tid`. */
4747
__alive: { [tid in string]: Thread };
@@ -64,7 +64,7 @@ export class Scheduler implements SchedulerInterface {
6464
this.rt_uuid = runId;
6565
this.rtObj = rtObj;
6666
this.__funloop = [];
67-
this.__blocked = [];
67+
this.__blocked = {};
6868
this.__alive = {};
6969
this.__currentThread = null;
7070
}
@@ -90,7 +90,7 @@ export class Scheduler implements SchedulerInterface {
9090
delete this.__alive[x];
9191
}
9292
}
93-
this.__blocked = [];
93+
this.__blocked = {};
9494
this.__funloop = [];
9595
// console.log (`The number of active threads is ${Object.keys(this.__alive).length}`)
9696
// console.log (`The number of blocked threads is ${this.__blocked.length}`)
@@ -160,18 +160,15 @@ export class Scheduler implements SchedulerInterface {
160160

161161
/** Block thread object `t`. */
162162
blockThread(t: Thread) {
163-
this.__blocked.push(t);
163+
this.__blocked[t.tid.val.toString()] = t;
164164
}
165165

166166
/** Unblock the thread with the given identifier, `pid`. */
167167
unblockThread(tid: LVal) {
168-
for (let i = 0; i < this.__blocked.length; i++) {
169-
if (pid_equals(this.__blocked[i].tid, tid)) {
170-
this.scheduleThread(this.__blocked[i]);
171-
this.__blocked.splice(i, 1);
172-
break;
173-
}
174-
}
168+
if (!this.__blocked[tid.val.toString()]) { return; }
169+
170+
this.scheduleThread(this.__blocked[tid.val.toString()]);
171+
delete this.__blocked[tid.val.toString()];
175172
}
176173

177174
/*************************************************************************************************\

0 commit comments

Comments
 (0)