In multithreading, 1 thread is stuck in between

pradnya123 wrote on Tuesday, July 04, 2017:

Hello,
Im working with STM 32 controller. Using freeRTOS.
I have created 4 threads out of which 2 are using same semaphore while other using different semaphores. It works fine for 10 to 12 hours but after that 1 thread is stuck while other 3 are in running state. Also if i resume this thread in some other function it is restarted. but what is reason to get Hanged?
Why is this issue.
Is it related to Stack size cause I tried increaning stack size for Thread but No use.

hs2sf wrote on Tuesday, July 04, 2017:

Sounds like a deadlock bug / race condition in your code.
I think you should double-check your semaphores maybe including the interrupt handling / ISRs presumably driving the sempahore signalling.

pradnya123 wrote on Tuesday, July 04, 2017:

If it is issue related to semaphore ,any task should be stuck. But in my case only one task is stuck. it has High priority and also semaphore used is dedicated to this thread only.

pradnya123 wrote on Tuesday, July 04, 2017:

I am using HAL function to creat a task. Memory is allocated internally in these functions. Do i need to release this memory manually?
If yes how to do this?

rtel wrote on Tuesday, July 04, 2017:

Unfortunately your original post didn’t give anywhere near enough
information to provide any suggestions - since then talk about interrupt
priorities, memory allocation and stack overflows are just guesses with
no context and no reason as to why they might be being suggested.

First ensure you are using the most recent version of FreeRTOS (as the
newer the version the more debugging assistance you will get through
asserts()), then do the normal things suggested on this page:
http://www.freertos.org/FAQHelp.html (ensure configASSERT() is defined,
etc.).

If that doesn’t help then try using the http://www.freertos.org/trace
tool so you can actually see how your application is executing - it
might not be how you think it is.

If that does not help I would then suggest cutting down your application
to the bare minimum that shows this issue, then posting the code that
shows how the semaphore is being used.

pradnya123 wrote on Wednesday, July 05, 2017:

Thank you for your support.
After debugging i have found that issue is arrise in stack memory only. In thread which stuck i called function uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL ); to find out free stack.
Here stack memory reaches 0 and thread stuck.
Now i need to know can we clear Heap manually. If yes how to do it.
Here is my Code patch:
Im defining Thread using HAL function hence memory is allocated automatically . how to release it back

	 /* Create the fourth Thread */
	osThreadDef(SemaphoreThread4, GPRS_Thread, osPriorityHigh, 0, 1324);
	GPRSThread_Handle = osThreadCreate(osThread(SemaphoreThread4), (void *) osGPRSSemaphore);

void GPRS_Thread(void const *argument)
{
static unsigned char Only_once;
osSemaphoreId semaphore = (osSemaphoreId) argument;

int response = 0;
UBaseType_t uxHighWaterMark;

uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL ); 

 for(;;)
 {
	  if (semaphore != NULL)
	  {
		/* Try to obtain the semaphore. */
		if(osSemaphoreWait(semaphore , 500) == osOK)
		{
        
             /* Application Code*/
            if(osSemaphoreRelease(semaphore) != osOK)
			{
				HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, 0); //(LD1_Pin);
							while (1) {
							}
			} 
			osDelay(10); 
		}//endsemaphorewait
	  }// end if semaphore != NULL
  }//end for

}

pradnya123 wrote on Wednesday, July 05, 2017:

Thank you for your support.
After debugging i have found that issue is arrise in stack memory only. In thread which stuck i called function uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL ); to find out free stack.
Here stack memory reaches 0 and thread stuck.
Now i need to know can we clear Heap manually. If yes how to do it.
Here is my Code patch:
Im defining Thread using HAL function hence memory is allocated automatically . how to release it back

	 /* Create the fourth Thread */
	osThreadDef(SemaphoreThread4, GPRS_Thread, osPriorityHigh, 0, 1324);
	GPRSThread_Handle = osThreadCreate(osThread(SemaphoreThread4), (void *) osGPRSSemaphore);

void GPRS_Thread(void const *argument)
{
static unsigned char Only_once;
osSemaphoreId semaphore = (osSemaphoreId) argument;

int response = 0;
UBaseType_t uxHighWaterMark;

uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL ); 

 for(;;)
 {
	  if (semaphore != NULL)
	  {
		/* Try to obtain the semaphore. */
		if(osSemaphoreWait(semaphore , 500) == osOK)
		{
        
             /* Application Code*/
            if(osSemaphoreRelease(semaphore) != osOK)
			{
				HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, 0); //(LD1_Pin);
							while (1) {
							}
			} 
			osDelay(10); 
		}//endsemaphorewait
	  }// end if semaphore != NULL
  }//end for

}

richarddamon wrote on Wednesday, July 05, 2017:

If you stack high water mark gets to 0 bytes free, you need to alloocate more stack to that task or make that task take less stack.

rtel wrote on Wednesday, July 05, 2017:

As Richard D said, you have run out of stack space (the high water mark
shows no free stack) so need to increase that. I’m not sure what you
mean by ‘clear heap manually’.