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.
Function to check ownership of mutex Topic is solved
- Giovanni
- Site Admin
- Posts: 14455
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1076 times
- Been thanked: 922 times
- Contact:
Re: Function to check ownership of mutex
Wouldn't recursive mutexes help with that? when enabled a thread can take the same mutex multiple times.
Giovanni
Giovanni
Re: Function to check ownership of mutex
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.
- Giovanni
- Site Admin
- Posts: 14455
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1076 times
- Been thanked: 922 times
- Contact:
Re: Function to check ownership of mutex
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
Giovanni
Re: Function to check ownership of mutex
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 .
- Giovanni
- Site Admin
- Posts: 14455
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1076 times
- Been thanked: 922 times
- Contact:
Re: Function to check ownership of mutex
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
Giovanni
Re: Function to check ownership of mutex
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.
- Giovanni
- Site Admin
- Posts: 14455
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1076 times
- Been thanked: 922 times
- Contact:
- Giovanni
- Site Admin
- Posts: 14455
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1076 times
- Been thanked: 922 times
- Contact:
Return to “Small Change Requests”
Who is online
Users browsing this forum: No registered users and 7 guests