Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

a semaphore problem

Posted by Nobody/Anonymous on November 1, 2004
dear all:
I have a problem about the semaphore. When A task be locked by a semaphore, and B task is running with the same semaphore. But if B task is yielded by vTaskdelay(), then A task will fail immediately without blocking to get the semaphore.

RE: a semaphore problem

Posted by Richard on November 1, 2004
Hi,

Can you clarify your question - I don't quite follow what you are asking.

>I have a problem about the semaphore. When A
>task be locked by a semaphore,

Does this mean that task A is blocked waiting for a semaphore to become available?

>and B task is
>running with the same semaphore.

Does this mean that task B has control of the semaphore that task A is blocked on?

>But if B task
>is yielded by vTaskdelay(), then A task will fail
>immediately without blocking to get the
>semaphore.

So task B calls vTaskDelay() while it has control of the semaphore?

What do you mean by "fail"?

If you can clarify these points I will be pleased assist. Maybe you could provide a snippet of source code to demonstrate your question? For example:

// Assume two tasks of the same priority. The
// semaphore has already been created.

// First vTest1 executes and 'takes' the semaphore.
// vTest1 then blocks by calling vTaskDelay.

// As vTest1 has blocked, vTest2 executes. It
// tries to 'take' the semphore but cannot (vTask1
// has it). It therefore blocks to wait for the
// semaphore.

// Now both tasks are blocked so the idle task
// executes until the 500 delay in vTask1 expires.

void vTest1( void *pvParameters )
{
for( ;; )
{
// Take the semaphore.
cSemaphoreTake( xSem, 100000 );
vTaskDelay( 500 );
}
}

void vTest1( void *pvParameters )
{
for( ;; )
{
// Take the semaphore.
cSemaphoreTake( xSem, 100000 );
vTaskDelay( 500 );
}
}

RE: a semaphore problem

Posted by Nobody/Anonymous on November 2, 2004
please refer to the following code.

void taskA
{
for(;;) {
vIOfunc();
.......
}
}

void taskB
{
for(;;) {
vIOfunc();
.......
}
}

bool vIOfunc()
{
if(cSemaphoreTake(xSemaphore, _1_Sec) != pdFAIL) {
while(!IO_Ready) vTaskDelay(_50_mSec);
........
while(IO_Command_Busy) vTaskDelay(_50_mSec);
........
do_IO_job;
cSemaphoreGive(xSemaphore);
return SUCCESS;
}
return FAIL;
}

I need you help, thanks.

RE: a semaphore problem

Posted by Nobody/Anonymous on November 2, 2004
comment:
if taskA has control of xsemaphore, then taskB will receive pdFAIL less then 1 sec. Because taskA is yielded by vTaskDelay, and make taskB get pdFAIL immediately. I am sure 1 second is enough to do the IO job.

RE: a semaphore problem

Posted by Nobody/Anonymous on November 2, 2004
This behavior could be correct depending on the priority of your tasks.

Below is a simple test program that demonstrates the behavior.

1) Main creates the semaphore, then creates the two tasks.

2) Task B starts executing first, and obtains the semaphore.

3) Task B delays - causing Task A to execute.

4) Task A attempts to obtain the semaphore, but Task B has it so Task A blocks.

5) Eventually Task B is able to continue and does its 'job'.

6) Task B gives up the semaphore. As Task A is blocked on the semaphore it is unblocked when the semaphore is given up.

If Task A had a higher priority than Task B it would now preempt Task B, obtain the semaphore, and do its 'job'.

However, as in this demo Task A has the same priority as Task B, Task A does not preempt Task B and Task B continues to execute.

7) Task B immediately obtains the semaphore again.

8) Task B then blocks waiting to be able to do it's task. Now Task B is blocked Task A can execute.

9) Task A attempts to obtain the semaphore, but it is unavailable (Task B has it again), so the call to cSemaphoreTake() fails!



Solutions (in order of preference):

a) Rearrange lIOFunc so that there is no delay while the semaphore is locked. This could be done if the semaphore is only released when the IO 'job' becomes available.

b) Add a call to portYIELD immediately after the call to lIOFunc(). This would force a context switch meaning that Task A and Task B would then alternate in obtaining the semaphore.

c) Lower the priority of the task once it has the semaphore (could mean the semaphore is locked for longer depending on other task priorities), then raise it again once it is released.





/////
TEST PROGRAM
/////

xSemaphoreHandle xSemaphore;

portLONG lIOfunc( void )
{
portLONG l;

if( cSemaphoreTake( xSemaphore, 1000UL ) != pdFAIL )
{
// Simulate a delay;
for( l = 0; l < 0x2; l++ )
{
vTaskDelay( 2 );
}

// Do job

cSemaphoreGive( xSemaphore );

return pdPASS;
}

return pdFAIL;
}


void vTaskA( void *pvParameters )
{
for( ;; )
{
lIOfunc();
}
}

void vTaskB( void *pvParameters )
{
for( ;; )
{
lIOfunc();
}
}

int main( void )
{
prvSetupHardware();

vSemaphoreCreateBinary( xSemaphore );

sTaskCreate( vTaskA, "A", 128, NULL, 1, NULL );
sTaskCreate( vTaskB, "B", 128, NULL, 1, NULL );

vTaskStartScheduler( portUSE_PREEMPTION );

/* Should never reach here! */
return 0;
}

RE: a semaphore problem

Posted by Nobody/Anonymous on November 2, 2004
> Solutions (in order of perference)

Alternatively; if you want the unblocked task to preempt the unblocking task even when the priorities are the same then change the line

if( pxUnblockedTCB->ucPriority > pxCurrentTCB->ucPriority )

to

if( pxUnblockedTCB->ucPriority >= pxCurrentTCB->ucPriority )


in cTaskRemoveFromEventList() in tasks.c.


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




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists