A function is doing things on variables that must be protected against concurrent threads by a mutex.
The caller of this function locks a mutex, performs other things and calls this function.
Now a situation arised that requires this function to be called from another piece of code.
Of course this code could lock the mutex too but I thought perhaps the function could see if it must lock the mutex or not.
What I would need is a function that tells me if the current thread has locked the mutex. Then the function could go on without locking. Otherwise it would lock.
int need_to_lock = !my_thread_has_locked_the_mutex(&mutex);
if (need_to_lock)
mutex_lock(&mutex);
access variables;
if (need_to_lock)
mutex_unlock(&mutex);
Does my_thread_has_locked_the_mutex exist? I couldn't find anything.
Edit to comments
I didn't know about recursive mutexes. They seem to do what I thought of.
What I use now is a not recursive mutex that blocks if locked twice from the same thread.