vTaskSuspend(h) release mutexes of h

I use mutex to get acsess to SPI via motor task. If user press button, I need stop all actions. So controlTask uses

  1. vTaskSuspend(MotorTask)
  2. ask motors to stop via SPI

But if MotorTask is going to sleep with SPI mutex, all my next SPI communications fails. It is not obvious that a mutex is not freed. I suggest add one #if at tasks.c:

void vTaskSuspend( TaskHandle_t xTaskToSuspend )
{
TCB_t *pxTCB;

	taskENTER_CRITICAL();
	{
		/* If null is passed in here then it is the running task that is
		being suspended. */
		pxTCB = prvGetTCBFromHandle( xTaskToSuspend );

#if ( configUSE_TRACE_FACILITY == 1 ) //by DungeonLords
configASSERT( !pxTCB->uxMutexesHeld ); //by DungeonLords
#endif //by DungeonLords

		traceTASK_SUSPEND( pxTCB );

Now If there are some mutexes at suspended task debugger stops at this configASSERT

modified file tasks.c

If anything releases a mutex other than the task that took it, then the mutex will no longer provide the desired protection.

In your case, if the MotorTask is in the middle of a SPI transaction, and another task suspends it, and then tries to send a SPI message, you are apt to put the SPI controller into a odd state.

In general, randomly suspending a task is not a good idea, you really want should send that task a message via some method and have it do what is needed or pause itself and indicate that it has done so.

1 Like

Richard is fully right. Suspending / resuming tasks is too often mistaken (read: abused) as task synchronization mechanism … and should get removed. No - not really :wink:
Believe it or not, but I‘ve never used suspend / resume in all my projects. This feature is kind of special and rarely used except for special cases.

1 Like

Thanks for the suggestion - but in this case I think this is making assumptions about how individual applications are using the code as there will be applications where suspending a task that holds a mutex is what the application writer intended.

Besides of suspend/resume tasks, create/delete tasks also has the similar problem.{Problem understanding vTaskDelete()} ,
the last reply of the link by Richard Damon,he points out the danger.
I think it is very convenient to use “suspend/resume” ,“create/delete”;because it can save ram. and it is similar to this: when using computer, opening a app like excel, and close it after one finished work.is it cool?
Care to taking is not forgetting the danger. And using same size stack for every dynamic tasks attempting to prevent mem leak.