how to understand the "g_running_tasks" valid only when "up_interrupt_context" is ture?

30 views
Skip to first unread message

tugouxp tugouxp

unread,
Dec 22, 2019, 9:59:47 PM12/22/19
to NuttX
143                                                                                                                                           

 Hi all:
    in nuttx code base SMP mode,  a descriptions of g_running_tasks object says that:

 /* g_running_tasks[] holds a references to the running task for each cpu.                                                                         
145  * It is valid only when up_interrupt_context() returns true.                                                                                     
146  */                                                                                                                                               
147                                                                                                                                                   
148 FAR struct tcb_s *g_running_tasks[CONFIG_SMP_NCPUS];  

So, why g_running_tasks only valid in interrupt context mode? did this means we could not use the g_running_tasks to reference the current thread in thread context?

if so , how?

thanks for your kindly help.

Gregory Nutt

unread,
Dec 23, 2019, 7:11:49 AM12/23/19
to nu...@googlegroups.com

Normally the current active task for a CPU is determined from the head of g_assignedtasks[cpu] using the this_task() macro.  g_running_tasks[cpu] is used only during interrupt handle to deal with the case where a context switch occurred and g_running_tasks[cpu] no longer refers to that task that was running when the interrupt handler was entered before the context switch.


As I recall, g_running_tasks[] is only used if a crash occurs in an interrupt handler.  Then the stack that you want to dump is the stack of the task that was running at the time of the interrupt, not the new task which has not even ran yet.

patacongo

unread,
Dec 23, 2019, 7:36:00 AM12/23/19
to nu...@googlegroups.com
Oops.. I accidentally editted my response instead of replying to it.  I will duplicate the original reply.


On Monday, December 23, 2019 at 6:11:49 AM UTC-6, patacongo wrote:

Normally the current active task for a CPU is determined from the head of g_assignedtasks[cpu] using the this_task() macro.  g_running_tasks[cpu] is used only during interrupt handle to deal with the case where a context switch occurred and g_running_tasks[cpu] no longer refers to that task that was running when the interrupt handler was entered before the context switch.


As I recall, g_running_tasks[] is only used if a crash occurs in an interrupt handler.  Then the stack that you want to dump is the stack of the task that was running at the time of the interrupt, not the new task which has not even ran yet.

This is the message I accidentally editted:

    in nuttx code base SMP mode,  a descriptions of g_running_tasks object says that:

 /* g_running_tasks[] holds a references to the running task for each cpu.                                                                         
145  * It is valid only when up_interrupt_context() returns true.                                                                                     
146  */                                                                                                                                               
147                                                                                                                                                   
148 FAR struct tcb_s *g_running_tasks[CONFIG_SMP_NCPUS];  

So, why g_running_tasks only valid in interrupt context mode? did this means we could not use the g_running_tasks to reference the current thread in thread context?

It is assigned a value only when the interrupt occurs:

sched/irq/irq_dispatch.c:  g_running_tasks[this_cpu()] = this_task();

And is invalid outside of interrupt handling.

Normally the current active task for a CPU is determined from the head of g_assignedtasks[cpu] using the this_task() macro.  g_running_tasks[cpu] is used only during interrupt handle to deal with the case where a context switch occurred and g_running_tasks[cpu] no longer refers to that task that was running when the interrupt handler was entered before the context switch.

So, you cannot use in g_running_tasks[cpu] thread code.  Use the macro this_task() instead which will safely get the head of the g_assignedtasks[cpu] array.

NOTE: These are all internal OS interfaces and none of them should be used applications.

Greg


 

patacongo

unread,
Dec 23, 2019, 8:33:19 AM12/23/19
to NuttX


On Monday, December 23, 2019 at 6:11:49 AM UTC-6, patacongo wrote:

Normally the current active task for a CPU is determined from the head of g_assignedtasks[cpu] using the this_task() macro.  g_running_tasks[cpu] is used only during interrupt handle to deal with the case where a context switch occurred and g_running_tasks[cpu] no longer refers to that task that was running when the interrupt handler was entered before the context switch.


As I recall, g_running_tasks[] is only used if a crash occurs in an interrupt handler.  Then the stack that you want to dump is the stack of the task that was running at the time of the interrupt, not the new task which has not even ran yet.

A better name for the array might be g_interrupted_tasks[]

Xiang Xiao

unread,
Dec 23, 2019, 9:40:38 PM12/23/19
to NuttX
Yes, g_interrupted_tasks Is more suitable name.
Please call running_task macro in sched/sched/sched.h if you want to get the current running task.
BTW, it's better to send the new question to d...@nuttx.apache.org, the new mail list for NuttX.
You can join this group by send an empty message to: dev-su...@nuttx.apache.org.

Thanks
Xiang
Reply all
Reply to author
Forward
0 new messages