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
Would you please show the use of a semaphore to print A1 => A2 => B1 => B2 for sure ?
Thanks
It's just a question if you take the mutex outside of the print calls, then you can have multiple print calls within the same critical section.
If the print functions internally uses a mutex, then they guarantee to not garble the output of the individual calls. But two calls to that function can have the output from another thread in between.
Since the mutex keeps track of which thread owns it, and allows multiple calls from same thread (as long as you unlock the same number of times), you can even do both.
So your print() functions can make use of a mutex, and single calls to print() can manage without issues. When a task needs to perform multiple print() without someone getting in between, that task can use the same mutex as your print() function.