Skip to content

Commit 4c2c1fa

Browse files
zephyr: display proper name for thread states (#78)
Update existing support for Zephyr RTOS to use proper thread state definitions from a modern release. Also simplify the code by using a common class for most thread states. Signed-off-by: Mathieu Choplain <[email protected]>
1 parent 11fd71e commit 4c2c1fa

File tree

1 file changed

+42
-58
lines changed

1 file changed

+42
-58
lines changed

src/rtos/rtos-zephyr.ts

Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export class RTOSZEPHYR extends RTOSCommon.RTOSBase {
217217

218218
const curTaskObjBase = await this.getVarChildrenObj(curTaskObj.base.ref, 'curTaskObjBase');
219219

220-
const thStateObject = await this.analyzeTaskState(curTaskObjBase);
220+
const thStateObject = await this.analyzeTaskState(curTaskObjBase, threadRunning);
221221
const thState = thStateObject.describe();
222222

223223
const stackInfo = await this.getStackInfo(curTaskObj);
@@ -229,11 +229,7 @@ export class RTOSZEPHYR extends RTOSCommon.RTOSBase {
229229

230230
mySetter(DisplayFields.Address, RTOSCommon.hexFormat(thAddress));
231231
mySetter(DisplayFields.TaskName, thName);
232-
mySetter(
233-
DisplayFields.Status,
234-
threadRunning ? 'RUNNING' : thState,
235-
thStateObject.fullData()
236-
);
232+
mySetter(DisplayFields.Status, thState, thStateObject.fullData());
237233
mySetter(
238234
DisplayFields.Priority,
239235
curTaskObjBase ? parseInt(curTaskObjBase.prio.val).toString() : '???'
@@ -306,15 +302,15 @@ export class RTOSZEPHYR extends RTOSCommon.RTOSBase {
306302
return timeoutValue;
307303
}
308304

309-
protected async analyzeTaskState(curTaskObjBase: RTOSCommon.RTOSStrToValueMap | null): Promise<TaskState> {
305+
protected async analyzeTaskState(curTaskObjBase: RTOSCommon.RTOSStrToValueMap | null, isCurrent: boolean): Promise<TaskState> {
310306
if (curTaskObjBase === null) {
311307
return new TaskStateInvalid();
312308
} else {
313309
const state = parseInt(curTaskObjBase.thread_state.val);
314310
const timeoutValue = await this.getTaskTimeout(curTaskObjBase);
315-
switch (state) {
316-
case OsTaskState.DUMMY:
317-
return new TaskDummy();
311+
312+
/* Ignore special DUMMY bit for state determination */
313+
switch (state & ~OsTaskState.DUMMY) {
318314
case OsTaskState.PENDING:
319315
const resultState = new TaskPending();
320316
resultState.addEventType(OsEventType.Generic);
@@ -338,18 +334,11 @@ export class RTOSZEPHYR extends RTOSCommon.RTOSBase {
338334
}
339335
}
340336
return resultState;
341-
case OsTaskState.PRESTART:
342-
return new TaskPrestart();
343-
case OsTaskState.DEAD:
344-
return new TaskStateInvalid();
345337
case OsTaskState.SUSPENDED:
346338
return new TaskSuspended(timeoutValue);
347-
case OsTaskState.ABORTING:
348-
return new TaskAborting();
349-
case OsTaskState.READY:
350-
return new TaskReady();
351339
default: {
352-
return new TaskStateInvalid();
340+
/* TODO: handle 'idle' threads special case */
341+
return new GenericTaskState(state, isCurrent);
353342
}
354343
}
355344
}
@@ -446,14 +435,20 @@ export class RTOSZEPHYR extends RTOSCommon.RTOSBase {
446435
}
447436
}
448437

438+
/**
439+
* NOTE: the base.thread_state bit _THREAD_QUEUED
440+
* is NOT cleared on the thread that is currently
441+
* running if Zephyr is built without SMP support.
442+
*/
449443
enum OsTaskState {
450-
DUMMY = 0x00 /* _THREAD_DUMMY / Not a real thread */,
451-
PENDING = 0x01 /* _THREAD_PENDING / Waiting */,
452-
PRESTART = 0x02 /* _THREAD_PRESTART / New */,
453-
DEAD = 0x04 /* _THREAD_DEAD / Terminated */,
454-
SUSPENDED = 0x10 /* _THREAD_SUSPENDED / Suspended (thread is not active until k_wakeup() is called on thread) */,
455-
ABORTING = 0x20 /* _THREAD_ABORTING / abort in progress */,
456-
READY = 0x80 /* _THREAD_QUEUED / Ready */,
444+
DUMMY = 1 << 0 /* _THREAD_DUMMY / Not a real thread */,
445+
PENDING = 1 << 1 /* _THREAD_PENDING / Waiting on object */,
446+
SLEEPING = 1 << 2 /* _THREAD_SLEEPING / Sleeping */,
447+
DEAD = 1 << 3 /* _THREAD_DEAD / Terminated */,
448+
SUSPENDED = 1 << 4 /* _THREAD_SUSPENDED / Suspended */,
449+
ABORTING = 1 << 5 /* _THREAD_ABORTING / Abort in progress */,
450+
SUSPENDING = 1 << 6 /* _THREAD_SUSPENDING / Suspend in progress */,
451+
READY = 1 << 7 /* _THREAD_QUEUED / Thread in ready queue */,
457452
}
458453

459454
enum OsEventType {
@@ -465,9 +460,28 @@ abstract class TaskState {
465460
public abstract fullData(): any;
466461
}
467462

468-
class TaskReady extends TaskState {
463+
class GenericTaskState extends TaskState {
464+
private description: string;
465+
466+
constructor(state: OsTaskState, isCurrent: boolean) {
467+
super();
468+
469+
let prefix = '';
470+
if (state & OsTaskState.DUMMY) {
471+
prefix = 'DUMMY | ';
472+
state &= ~OsTaskState.DUMMY;
473+
}
474+
475+
if (isCurrent) {
476+
this.description = prefix + 'RUNNING';
477+
} else {
478+
this.description = prefix + (OsTaskState[state] ?? '???');
479+
}
480+
481+
}
482+
469483
public describe(): string {
470-
return 'READY';
484+
return this.description;
471485
}
472486

473487
public fullData(): any {
@@ -496,36 +510,6 @@ class TaskSuspended extends TaskState {
496510
}
497511
}
498512

499-
class TaskAborting extends TaskState {
500-
public describe(): string {
501-
return 'ABORTING';
502-
}
503-
504-
public fullData(): any {
505-
return null;
506-
}
507-
}
508-
509-
class TaskPrestart extends TaskState {
510-
public describe(): string {
511-
return 'PRESTART';
512-
}
513-
514-
public fullData(): any {
515-
return null;
516-
}
517-
}
518-
519-
class TaskDummy extends TaskState {
520-
public describe(): string {
521-
return 'DUMMY';
522-
}
523-
524-
public fullData(): any {
525-
return null;
526-
}
527-
}
528-
529513
class TaskStateInvalid extends TaskState {
530514
public describe(): string {
531515
return '???';

0 commit comments

Comments
 (0)