Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Loading

Thread stack corruption when using a function that contanis a semaphore?

Posted by benval on October 17, 2014

Hi! I'm getting started with FreeRTOS and wrote a simple program that starts three threads that toggle an LED on a ARM Cortex-M4F. Each thread also outputs their ID over UART, I'm using tiny printf for that.

To prevent the threads from writing over each other, I guard the printf with a semaphore.

static void my_task(void *args) {
    TickType_t xNextWakeTime = xTaskGetTickCount();

    int id = (int) args;

    while (1) {
	xSemaphoreTake(printf_semaphore, portMAX_DELAY);
	printf("Hello from task %d!\n", id);
	xSemaphoreGive(printf_semaphore);

	/* Place this task in the blocked state until it is time to run again.
	The block time is specified in ticks, the constant used converts ticks
	to ms.  While in the Blocked state this task will not consume any CPU
	time. */
	vTaskDelayUntil( &xNextWakeTime, (id + 1) * 200 / portTICK_PERIOD_MS);

	switch (id) {
	case 0:
	    RED_LED_TOGGLE;
	    break;
	case 1:
	    GREEN_LED_TOGGLE;
	    break;
	case 2:
	    BLUE_LED_TOGGLE;
	    break;
	}
    }
}


int main(void)
{
	printf_semaphore = xSemaphoreCreateBinary();
	xSemaphoreGive(printf_semaphore);

    puts("Hello World!\n");

	xTaskCreate(my_task, "task 1", configMINIMAL_STACK_SIZE, (void*) 0, tskIDLE_PRIORITY + 1, NULL);
	xTaskCreate(my_task, "task 2", configMINIMAL_STACK_SIZE, (void*) 1, tskIDLE_PRIORITY + 1, NULL);
	xTaskCreate(my_task, "task 3", configMINIMAL_STACK_SIZE, (void*) 2, tskIDLE_PRIORITY + 1, NULL);
	vTaskStartScheduler();
}

This works as expected.

2014-10-17 15:57:09,334 - INFO # Hello World!
2014-10-17 15:57:09,336 - INFO # Hello from task 2!
2014-10-17 15:57:09,338 - INFO # Hello from task 0!
2014-10-17 15:57:09,339 - INFO # Hello from task 1!
2014-10-17 15:57:09,536 - INFO # Hello from task 0!
2014-10-17 15:57:09,736 - INFO # Hello from task 1!
2014-10-17 15:57:09,738 - INFO # Hello from task 0!
2014-10-17 15:57:09,935 - INFO # Hello from task 2!
2014-10-17 15:57:09,937 - INFO # Hello from task 0!
2014-10-17 15:57:10,136 - INFO # Hello from task 1!
2014-10-17 15:57:10,138 - INFO # Hello from task 0!
2014-10-17 15:57:10,336 - INFO # Hello from task 0!
2014-10-17 15:57:10,535 - INFO # Hello from task 2!
2014-10-17 15:57:10,537 - INFO # Hello from task 1!
2014-10-17 15:57:10,538 - INFO # Hello from task 0!
2014-10-17 15:57:10,736 - INFO # Hello from task 0!
2014-10-17 15:57:10,936 - INFO # Hello from task 1!
2014-10-17 15:57:10,938 - INFO # Hello from task 0!
2014-10-17 15:57:11,135 - INFO # Hello from task 2!
2014-10-17 15:57:11,137 - INFO # Hello from task 0!
2014-10-17 15:57:11,336 - INFO # Hello from task 1!
2014-10-17 15:57:11,338 - INFO # Hello from task 0!

Now instead of putting a semaphore around each printf, I would rather just add a wrapper around it.

void printf(char *fmt, ...) {
    xSemaphoreTake(printf_semaphore, portMAX_DELAY);

    va_list arg;
    va_start(arg, fmt);
    tfp_printf(fmt, arg);
    va_end(arg);

    xSemaphoreGive(printf_semaphore);
}

This however corrupts the threads' local variable:

2014-10-17 16:03:29,602 - INFO # Hello World!
2014-10-17 16:03:29,604 - INFO # Hello from task 2060!
2014-10-17 16:03:29,605 - INFO # Hello from task 892!
2014-10-17 16:03:29,607 - INFO # Hello from task 1476!
2014-10-17 16:03:29,803 - INFO # Hello from task 892!
2014-10-17 16:03:30,003 - INFO # Hello from task 1476!
2014-10-17 16:03:30,005 - INFO # Hello from task 892!
2014-10-17 16:03:30,202 - INFO # Hello from task 2060!
2014-10-17 16:03:30,204 - INFO # Hello from task 892!
2014-10-17 16:03:30,403 - INFO # Hello from task 1476!
2014-10-17 16:03:30,405 - INFO # Hello from task 892!
2014-10-17 16:03:30,603 - INFO # Hello from task 892!
2014-10-17 16:03:30,802 - INFO # Hello from task 2060!
2014-10-17 16:03:30,804 - INFO # Hello from task 1476!
2014-10-17 16:03:30,806 - INFO # Hello from task 892!
2014-10-17 16:03:31,003 - INFO # Hello from task 892!
2014-10-17 16:03:31,203 - INFO # Hello from task 1476!
2014-10-17 16:03:31,205 - INFO # Hello from task 892!
2014-10-17 16:03:31,402 - INFO # Hello from task 2060!

What is going on here?


Thread stack corruption when using a function that contanis a semaphore?

Posted by davedoors on October 17, 2014

First check all the normal things like ensuring you have stack overflow protection on, and configASSERT defined. http://www.freertos.org/Stacks-and-stack-overflow-checking.html http://www.freertos.org/a00110.html#configASSERT

Try changing your printf function so xSemaphoreTake is called after the arg variable is declared.


Thread stack corruption when using a function that contanis a semaphore?

Posted by heinbali01 on October 19, 2014

Hi Benjamin,

I haven't used tfp_printf() before, but when I look it up it says:

void tfp_printf(char *fmt, ...);

and not:

void tfp_printf(char *fmt, va_list param);

The compiler won't complain because the ellipsis (...) has no type-checking

Hein


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]


Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


Careers

FreeRTOS and other embedded software careers at AWS.



FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Renesas

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner