Function to check ownership of mutex

Use this forum for requesting small changes in ChibiOS. Large changes should be discussed in the development forum. This forum is NOT for support.
faisal
Posts: 201
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 30 times
Been thanked: 25 times

Function to check ownership of mutex

Postby faisal » Tue Aug 07, 2018 10:38 pm

Something like return chThdGetSelfX() == mutex.owner .

This is useful in designing APIs where you want to lock down an entire module. You would do something like module_lock(), call a bunch of module_api() functions (each of which checks to make sure the calling thread owns the mutex), and then at the end do a module_unlock().

I haven't seen anything in the Chibi Mutex API that would enable this.

User avatar
Giovanni
Site Admin
Posts: 11385
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 430 times
Been thanked: 362 times
Contact:

Re: Function to check ownership of mutex

Postby Giovanni » Wed Aug 08, 2018 6:45 am

Wouldn't recursive mutexes help with that? when enabled a thread can take the same mutex multiple times.

Giovanni

faisal
Posts: 201
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 30 times
Been thanked: 25 times

Re: Function to check ownership of mutex

Postby faisal » Wed Aug 08, 2018 1:43 pm

Giovanni wrote:Wouldn't recursive mutexes help with that? when enabled a thread can take the same mutex multiple times.

Giovanni


Recursive mutexes are useful, but not in the scenario described. I want to ensure thread-safety between multiple of a module's API calls. In order to do that, I need to take a mutex, and then in each API call check if the calling thread owns the mutex. If it doesn't own it, it returns in error.

The specific use is case is of a file-like object, shared between multiple threads ... which has methods like lseek, read, and write. As an example, I want to do an lseek followed by a read - and ensure that no other thread modifies the file offset (via lseek) between the lseek and read.

User avatar
Giovanni
Site Admin
Posts: 11385
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 430 times
Been thanked: 362 times
Contact:

Re: Function to check ownership of mutex

Postby Giovanni » Wed Aug 08, 2018 1:51 pm

Wouldn't this be an use case for chMtxTryLock()? You try locking, if it is not locked then you lock the mutex else you get an error without waiting.

Giovanni

faisal
Posts: 201
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 30 times
Been thanked: 25 times

Re: Function to check ownership of mutex

Postby faisal » Wed Aug 08, 2018 2:04 pm

Giovanni wrote:Wouldn't this be an use case for chMtxTryLock()? You try locking, if it is not locked then you lock the mutex else you get an error without waiting.

Giovanni


No, if it is not locked - then I want to return in error. I want to ensure thread safety between several module API calls - thus the module has to already be locked by the calling thread before calling the module API functions.

Code: Select all

module_lock(); // acquire mutex
module_api1(); // check if I own mutex, otherwise return in error. This is successful.
module_api2(); // check if I own mutex, otherwise return in error. This is successful.
module_api3(); // check if I own mutex, otherwise return in error. This is successful.
module_api2(); // check if I own mutex, otherwise return in error. This is successful.
module_unlock(); // release mutex

module_api(); // returns in error, as I haven't locked down the module by acquiring mutex


Each of the module_apix() functions check if the mutex is already owned by the calling thread. If it is owned by the calling thread, the function proceeds. Otherwise returns in error.

There could be other applications as well to check if the current thread owns a mutex. I think it's a small change - hence filed in small change requests :).

User avatar
Giovanni
Site Admin
Posts: 11385
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 430 times
Been thanked: 362 times
Contact:

Re: Function to check ownership of mutex

Postby Giovanni » Wed Aug 08, 2018 2:10 pm

As workaround you could unlock in case the try is successful. Adding a new functions should not be a problem, the code is there already.

Giovanni

faisal
Posts: 201
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 30 times
Been thanked: 25 times

Re: Function to check ownership of mutex

Postby faisal » Sat Aug 11, 2018 2:20 am

Giovanni wrote:As workaround you could unlock in case the try is successful. Adding a new functions should not be a problem, the code is there already.

Giovanni


Yes, that would work as a work around for now. Thanks! Once this is implemented, it can be used in the HAL to enforce the xxxAcquireBus() and xxxReleaseBus() API with an assert (by making the public functions check for mutex ownership). As it is, it is not enforced and can lead to weird behavior since many functions are not thread safe.


Return to “Small Change Requests”

Who is online

Users browsing this forum: No registered users and 0 guests