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

RTOS (RTX) Mutex-Semaphore problem

Hi all ,

I thought the only differences between a Mutex and a semaphore are the count(Semaphore Capability) and priority inversion(Semaphore Capability) .
Today , I’ve encountered something strange which maybe is related to the priority inversion capability or something else .

Getting and releasing Mutex or Semaphores between different tasks is clear but when I use them in just one task , their behavior is different .
Using semaphore the task is locked but using Mutex the task is not locked .
Imagine there is just one task called APP_TestTask

__task void APP_TestTask (void)
{
        for (;;)
        {
                os_dly_wait (20);
                os_sem_wait(Sem_Test,0xffff);
                os_sem_send(Sem_Test);
                os_sem_wait(Sem_Test,0xffff);
                os_sem_wait(Sem_Test,0xffff);
                Test_Function();
        }
}

__task void APP_TestTask (void)
{
        for (;;)
        {
             os_dly_wait (20);
             os_mut_wait(Mut_Test,0xffff);
             os_mut_release(Mut_Test);
             os_mut_wait(Mut_Test,0xffff);
             os_mut_wait(Mut_Test,0xffff);
             Test_Function();
        }
}


Is it something natural or a bug ?
Thanks in advanced

Parents
  • Note that the RTX knows who owns the mutex, and are using a counter for number of times it has been locked.

    So the same task can claim the mutex multiple times without getting locked.

    www.keil.com/.../rlarm_os_mut_release.htm

    The goal with the mutex is just locked or not locked. So the internal counter just keeps track of number of times the same thread have locked it - expecting the same number of release calls before the mutex is released. This allows you to put the mutex lock/unlock inside a function, and then make lock/unlock outside that function call and perform multiple function calls while still owning the mutex.

    A semaphore on the other hand counts the availability of a limited resource. So for a semaphore it really do matter the number of times you claim it - it's irrelevant who claims a resource.

Reply
  • Note that the RTX knows who owns the mutex, and are using a counter for number of times it has been locked.

    So the same task can claim the mutex multiple times without getting locked.

    www.keil.com/.../rlarm_os_mut_release.htm

    The goal with the mutex is just locked or not locked. So the internal counter just keeps track of number of times the same thread have locked it - expecting the same number of release calls before the mutex is released. This allows you to put the mutex lock/unlock inside a function, and then make lock/unlock outside that function call and perform multiple function calls while still owning the mutex.

    A semaphore on the other hand counts the availability of a limited resource. So for a semaphore it really do matter the number of times you claim it - it's irrelevant who claims a resource.

Children