This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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;
}

0