Skip to content

Commit 5458833

Browse files
committed
Replace optional arguments for newly scheduled threads with an enum
This much better conveys the meaning of this argument, especially from the call site
1 parent fe5fe90 commit 5458833

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

rt/src/Scheduler.mts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ import {SYSTEM_PROCESS_STRING} from './Constants.mjs'
1818
import { getCliArgs, TroupeCliArg } from './TroupeCliArgs.mjs';
1919
const argv = getCliArgs();
2020

21+
/** Enum for termination statuses. */
22+
export enum ThreadType {
23+
/** System service thread. */
24+
System = -1,
25+
/** Main thread. */
26+
Main = 0,
27+
/** Other threads, spawned from 'Main' or 'System'. */
28+
Other = 1
29+
}
30+
2131
/** Enum for termination statuses. */
2232
enum TerminationStatus {
2333
/** Thread finished its computation. */
@@ -105,18 +115,16 @@ export class Scheduler implements SchedulerInterface {
105115
arg: any,
106116
pc: Level,
107117
block: Level,
108-
ismain: boolean = false,
109-
persist: boolean | null = null,
110-
isSystem: boolean = false)
118+
tType: ThreadType = ThreadType.Other)
111119
{
112120
// Create a new process ID at the given level.
113-
const pid = isSystem ? SYSTEM_PROCESS_STRING : uuidv4();
121+
const pid = tType === ThreadType.System ? SYSTEM_PROCESS_STRING : uuidv4();
114122
const pidObj = new ProcessID(this.rt_uuid, pid, this.__node);
115-
const newPid = new LVal(pidObj, pc);
123+
const newPid = new LVal(pidObj, pc);
116124

117125
// Epilogue for thread.
118-
const halt = ismain ? () => { this.haltMain (persist) } :
119-
() => { this.haltOther () };
126+
const halt = tType === ThreadType.Main ? () => { this.haltMain() }
127+
: () => { this.haltOther() };
120128

121129
// New thread
122130
const t = new Thread
@@ -197,7 +205,7 @@ export class Scheduler implements SchedulerInterface {
197205
}
198206

199207
/** Epilogue for `main` thread: notify monitors, print and persist the final value */
200-
haltMain (persist=null) {
208+
haltMain () {
201209
this.__currentThread.raiseCurrentThreadPCToBlockingLev()
202210
let retVal = new LVal (this.__currentThread.r0_val,
203211
lub(this.__currentThread.bl, this.__currentThread.r0_lev),
@@ -207,6 +215,7 @@ export class Scheduler implements SchedulerInterface {
207215

208216
delete this.__alive[this.__currentThread.tid.val.toString()];
209217
console.log(">>> Main thread finished with value:", retVal.stringRep());
218+
const persist = argv[TroupeCliArg.Persist];
210219
if (persist) {
211220
this.rtObj.persist(retVal, persist)
212221
console.log("Saved the result value in file", persist)

rt/src/runtimeMonitored.mts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { v4 as uuidv4 } from 'uuid'
44
import AggregateError from 'aggregate-error';
55
import { __unit } from './UnitVal.mjs'
66
import { Authority } from './Authority.mjs'
7-
import { Scheduler } from './Scheduler.mjs'
7+
import { Scheduler, ThreadType } from './Scheduler.mjs'
88
import { MailboxProcessor } from './MailboxProcessor.mjs'
99
import { RuntimeInterface } from './RuntimeInterface.mjs'
1010
import { LVal, MbVal } from './Lval.mjs'
@@ -445,18 +445,15 @@ export async function start(f) {
445445
, service_arg
446446
, levels.TOP
447447
, levels.BOT
448-
, false
449-
, null
450-
, true);
448+
, ThreadType.System);
451449
}
452450

453451
__sched.scheduleNewThreadAtLevel(
454452
() => f.main({__dataLevel:levels.BOT})
455453
, mainAuthority
456454
, levels.BOT
457455
, levels.BOT
458-
, true
459-
, argv[TroupeCliArg.Persist]
456+
, ThreadType.Main
460457
);
461458
__sched.loop();
462459
}

0 commit comments

Comments
 (0)