richard_damon wrote on Wednesday, August 03, 2011:
A “routine” runs in the execution context of that which calls it, so when a tick interrupt occurs, you are not “outside of a thread”. Just because the subroutine isn’t written as part of the task function doesn’t place the code “outside the task” when the task calls it.
Wether the routine needs a mutex to protect it depends on what the routine does. Since it runs in the context of its caller, if it is called from different threads, each one will have its own set of automatic (stack based) variables. If it accesses and global resources (global or static variables, or hardware) then those MAY need to protected by a mutex (or something). If the thread “owns” that resource already, then it probably doesn’t (and then typically the thread will pass an id for that resource into the routine).
There is one exception to the concept of everything running in the context of its caller, and that is for callers that specifically setup new contexts for the routine, like the Create Task routine, which creates the new context for the task to run in, and possibly interrupts which may or may not set up a distinct context for the interrupt to run in, or may use the context of the task they interrupted, and if they perform a context switch, may return to the context of a different task.