MUTEX time based locking crash the MBED

Hi ;
My question is related to MUTEX

  1. I am using trylock_for() and trylock_until() function to protect my critical section. But after sometime my MBED os crash !!! why ??? I have attached image of terminal for more information.

  1. I cant understand how these function work. according to me when first thread enter in to critical section at that time MUTEX flag set for time mentioned in function bracket. After time elapsed mutex automatically unlock that . so next thread can enter into block. Am I right ???

But when next thread (suppose T2) enter at same time all other threads (which are standing at door) also enter in my critical section. Why ? why second thread not set mutex lock for other ?

I attached my code here :

<>

#include “mbed.h”

Mutex Lock;
Thread T1,T2;

void common_block(const char *name,int state)
{
printf(“\n This %s Thread arrive at door with state %d \n”,name,state);
Lock.trylock_until(1); //lock for 3000 ms
printf(“\n Inside the door %s with state %d \n”,name,state);
wait(02); //some how process stuck in wait state
printf(“\n Thread %s is Ouside wait period with state %d \n”,name,state);

}
void fun(void const *arg)
{
while(true)
{
common_block((const char *)arg,0); //PASS STATE 0
wait(2);
common_block((const char *)arg,1);// PASS STATE 1
wait(3);
}
}

int main()
{
T1.start(callback(fun,(void *)“T1”));
T2.start(callback(fun,(void *)“T2”));
fun((void *)“main”);
}

<>

Why ??? OS crashed not immediately ???

Hello Jaydeep,

Your code doesn’t compile with Mbed OS 6 because of calling the wait function.
Which Mbed OS version do you use?

NOTE: After copy&paste you can prepend and append your code (and serial terminal printouts) with three back-tick characters (```) on separate line to get it formatted.

Best regards, Zoltan

Hi;
Thanks for quick reply dear.
I am using online MBED platform to write and compile . How can I check version ?I clicked checkbox “update code as per latest library and version” while creating the program

Hello Jaydeep,

There is an issue with the online compiler when updating the mbed-os library.

  • Right-click on the mbed-os library in your project and select Revisions...
  • Click on the last revision (‘tag: mbed-os-6.2.0’) and click on the Switch button (on the toolbar).

Best regards, Zoltan

Hi dear;
I have updated my code and use Thisthread::sleep_for() instead of wait
But after 2 to 3 minutes , OS crash again…:cry:

Hello Jaydeep,

I am using trylock_for() and trylock_until() function to protect my critical section. I cant understand how these function work. according to me when first thread enter in to critical section at that time MUTEX flag set for time mentioned in function bracket. After time elapsed mutex automatically unlock that . so next thread can enter into block. Am I right ???

My understanding of these functions is a bit different:

bool trylock_for(Kernel::Clock::duration_u32 rel_time);

Tries to lock the mutex. If it succeeds within the specified time frame (interval) the mutex is locked and stays locked until unlocked by calling the unlock function.

bool trylock_until(Kernel::Clock::time_point abs_time); 

Tries to lock the mutex. If it succeeds before the given time point is reached it locks the mutex and the mutex stays locked until unlocked by calling the unlock function.

Usage:

void common_block(const char* name, int state)
{
    printf("\n This %s Thread arrive at door with state %d \n", name, state);
    if (Lock.trylock_for(100)) { // try to lock the mutex: timeout = 100 ms
        printf("\n Inside the door %s with state %d \n", name, state);
        ThisThread::sleep_for(2);               //some how process stuck in wait state
        Lock.unlock();
    }
    printf("\n Thread %s is Ouside wait period with state %d \n", name, state);
}

Best regards, Zoltan

Thanks dear;

Got it…
But why os not crashed instantly ?? Why it took some time to display the error.?

Hello Jaydeep,

Honestly, I don’t know why it takes 3 minutes. The comment in the mutex.h says:

    /** Try to lock the mutex until specified time
      @param   abs_time  absolute timeout time, referenced to Kernel::get_ms_count()
      @return true if the mutex was acquired, false otherwise.
      @note the underlying RTOS may have a limit to the maximum wait time
            due to internal 32-bit computations, but this is guaranteed to work if the
            wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
            the lock attempt will time out earlier than specified.

      @note You cannot call this function from ISR context.
     */
    bool trylock_until(Kernel::Clock::time_point abs_time);

Best regards, Zoltan

1 Like

Thanks for quick reply

Have you checked ??
Are you facing same problem of OS crashing ???

Hello Jaydeep,

After replacing in your code wait with ThisThread::sleep_forit compiled but crashed after more than 2 minutes with this message:

++ MbedOS Error Info ++
Error Status: 0x80010133 Code: 307 Module: 1
Error Message: Mutex: 0x20000CB8, Unknown
Location: 0x80034E9
Error Value: 0x20000CB8
Current Thread: application_unnamed_thread Id: 0x20000D50 Entry: 0x8005AB9 StackSize: 0x1000 StackMem: 0x20006908 SP: 0x2001FF34 
For more info, visit: https://mbed.com/s/error?error=0x80010133&tgt=STM32F407VE
-- MbedOS Error Info --

However, after replacing:

Lock.trylock_until(1);
printf("\n Inside the door %s with state %d \n", name, state);
ThisThread::sleep_for(2);               //some how process stuck in wait state

with

if (Lock.trylock_until(1)) {
    printf("\n Inside the door %s with state %d \n", name, state);
    ThisThread::sleep_for(2);               //some how process stuck in wait state
    Lock.unlock();
}

The program didn’t crash.

Best regards, Zoltan