Keil Logo Arm Logo

Discussion Forum

Semaphore Problem

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Read-Only
Author
Thomas Green
Posted
4-May-2012 13:58 GMT
Toolset
ARM
New! Semaphore Problem

Hello,

I have a simple semaphore problem. I create a task (task1) and this task waits for a start event (TASK_START). Afterwards I send a start event, then this task sends a semaphore (sem1) in intervals of 1000 ms. After the start event was sent, the function "function1" is executed. This function waits for the sent semaphore 10 times. Afterwards it stops and then a stop event is sent to the created task (task1). This procedure works fine.
BUT :):
If I wait 5000 ms and repeat this procedure starting from the start event to the stop event then the os_sem_wait() function waits forever.

You can find the code below. I use the Keil 4.21 version but it should occur with higher versions as well.
hope someone can help me...

thanks

__task void init (void);

int main(void);

static OS_SEM sem1;
static OS_TID tidtask1;

#define TASK_START       0x0001
#define TASK_STOP        0x0002
#define TASK_START_STOP  0x0003

__task void Tsk_task1(void);
static bool_t function1(void);
bool stop;




int main(void)
{

    os_sys_init (init);

    return 0;
}

__task void init (void)
{

        os_tsk_prio_self(254);

        os_sem_init(sem1, 0);

        tidtask1 = os_tsk_create(Tsk_task1, 1);
        os_evt_set (TASK_START, tidtask1);
        function1();
        os_evt_set (TASK_STOP, tidtask1);
        //os_sem_init(sem1, 0);
        os_dly_wait(5000);
        os_evt_set (TASK_START, tidtask1);
        function1();
        os_evt_set (TASK_STOP, tidtask1);
//      Debuguart_WriteString("\n\nMain initialized !\n\n");
    os_tsk_delete_self() ;
}

__task void Tsk_task1(void)
{
        while(TRUE)
        {
                os_evt_wait_and(TASK_START, 0xFFFF);
                os_evt_clr(TASK_START_STOP, tidtask1);
                while (TRUE)
                        {

                                if(os_evt_wait_and(TASK_STOP, 10) == OS_R_TMO)
                                {
                                        os_dly_wait(1000);
                                        os_sem_send(sem1);
                                }
                                else
                                {
                                        u16_t event_signal;
                                        event_signal = os_evt_get();
                                        if((event_signal & TASK_STOP) == TASK_STOP)
                                        {
                                                os_evt_clr(TASK_STOP, tidtask1);
                                                break;
                                        }
                                }
                        }
        }
}
static bool_t function1(void)
{
OS_RESULT result;
int i;
i=0;
        while(stop != TRUE)
         {
                do
                {
                        result = os_sem_wait(sem1,0xFFFF);
                }
                while (result == OS_R_OK && stop != TRUE);

                if(result != OS_R_TMO)
                {
                        os_dly_wait(1000);
                        i+=1;
                        if(i==10)
                        {
                           stop = TRUE;
                        }

                }
        }

                 return TRUE;
}
Read-Only
Author
Robert McNamara
Posted
4-May-2012 21:53 GMT
Toolset
ARM
New! RE: Semaphore Problem

A valid value indicating a successfully return from the os_sem_wait() is OS_R_SEM. This indicates that you successfully obtained the semaphore but it was not there when you made the call and were blocked for some period of time. OS_R_OK is returned if the semaphore is available right away and the task is not blocked. It is best to test for != OR_R_TMO to mean success. My guess is because you changed the timing, the os_sem_wait is always return OS_R_SEM and it "looks" like it is always waiting at os_sem_wait() but in fact it is returning from os_sem_wait and going right back to it - and waiting again.

Read-Only
Author
Thomas Green
Posted
7-May-2012 08:33 GMT
Toolset
ARM
New! RE: Semaphore Problem

you're right it seems to be the timing...

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Keil logo

Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.