Question regarding mutex locking
If a particular routine needs to lock a mutex, but it may be entered with that mutex already locked, would the following code be SMP safe?
hold_lock = mutex_trylock()
...
if (hold_lock)
mutex_unlock()
Thanks,
Larry
El Tue, Nov 27, 2007 at 10:37:00PM -0600 Larry Finger ha dit:
Quoted 9 lines
If a particular routine needs to lock a mutex, but it may be entered with that mutex already locked, would the following code be SMP safe?
hold_lock=mutex_trylock()...
if (
hold_lock)
mutex_unlock()
this is wont work, a mutex must not be released from another context than the one that acquired it.
Signature · 12 lines
Matthias Kaehlcke
Linux Application Developer
Barcelona
Don't walk behind me, I may not lead
Don't walk in front of me, I may not follow
Just walk beside me and be my friend
(Albert Camus)
.''`.
using free software / Debian GNU/Linux | http://debian.org : :' :
`. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4 `-Quoted 7 lines · Matthias Kaehlcke
El Tue, Nov 27, 2007 at 10:37:00PM -0600 Larry Finger ha dit:
Quoted 5 lines
If a particular routine needs to lock a mutex, but it may be entered with that mutex already locked, would the following code be SMP safe?
hold_lock=mutex_trylock()
Quoted 7 lines
Quoted 4 lines
...
if (
hold_lock)
mutex_unlock()this is wont work, a mutex must not be released from another context than the one that acquired it.
I thought that mutex_trylock() returned 1 if it got the lock and 0 if not. If that is true, wouldn't the if statement only unlock if the lock was obtained in this routine?
El Wed, Nov 28, 2007 at 08:46:51AM -0600 Larry Finger ha dit:
Quoted 10 lines
Quoted 9 lines · Matthias Kaehlcke
El Tue, Nov 27, 2007 at 10:37:00PM -0600 Larry Finger ha dit:
Quoted 7 lines
If a particular routine needs to lock a mutex, but it may be entered with that mutex already locked, would the following code be SMP safe?
hold_lock=mutex_trylock()...
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
if (
hold_lock)
mutex_unlock()this is wont work, a mutex must not be released from another context than the one that acquired it.
I thought that
mutex_trylock()returned 1 if it got the lock and 0 if not. If that is true, wouldn't the if statement only unlock if the lock was obtained in this routine?
you're right, sorry i read to fast and interpreted that you want to release the mutex in any case
Signature · 10 lines
Matthias Kaehlcke
Linux Application Developer
Barcelona
You must have a plan. If you don't have a plan,
you'll become part of somebody else's plan
.''`.
using free software / Debian GNU/Linux | http://debian.org : :' :
`. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4 `-Quoted 9 lines · Larry Finger
If a particular routine needs to lock a mutex, but it may be entered with that mutex already locked, would the following code be SMP safe?
hold_lock=mutex_trylock()...
if (
hold_lock)
mutex_unlock()
When two CPUs may enter the critical region at the same time, what is the point of the mutex? Also, the first CPU may unlock the mutex while the second one is still inside the critical region.
Andreas.
Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected] SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany PGP key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
Quoted 7 lines · Andreas Schwab
Quoted 5 lines · Larry Finger
If a particular routine needs to lock a mutex, but it may be entered with that mutex already locked, would the following code be SMP safe?
hold_lock=mutex_trylock()
Quoted 8 lines
Quoted 4 lines
...
if (
hold_lock)
mutex_unlock()When two CPUs may enter the critical region at the same time, what is the point of the mutex? Also, the first CPU may unlock the mutex while the second one is still inside the critical region.
Thank you for that answer. I think that I'm finally beginning to understand.
Larry
Larry Finger wrote, On 11/28/2007 04:41 PM:
Quoted 10 lines
Quoted 9 lines · Andreas Schwab
Quoted 7 lines · Larry Finger
If a particular routine needs to lock a mutex, but it may be entered with that mutex already locked, would the following code be SMP safe?
hold_lock=mutex_trylock()...
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
if (
hold_lock)
mutex_unlock()When two CPUs may enter the critical region at the same time, what is the point of the mutex? Also, the first CPU may unlock the mutex while the second one is still inside the critical region.
Thank you for that answer. I think that I'm finally beginning to understand.
Probably it would be faster without these "...", which look like no man's land...
hold_lock = mutex_trylock()
if (hold_lock) {
/* SMP safe */
...
mutex_unlock()
} else {
/* SMP unsafe */
...
/* maybe try again after some break or check */
}Regards,
Jarek P.
Jarek Poplawski wrote, On 11/28/2007 11:45 PM:
Quoted 23 lines
Larry Finger wrote, On 11/28/2007 04:41 PM:
Quoted 8 lines
Quoted 7 lines · Andreas Schwab
Quoted 5 lines · Larry Finger
If a particular routine needs to lock a mutex, but it may be entered with that mutex already locked, would the following code be SMP safe?
hold_lock=mutex_trylock()Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
...
if (
hold_lock)
mutex_unlock()When two CPUs may enter the critical region at the same time, what is the point of the mutex? Also, the first CPU may unlock the mutex while the second one is still inside the critical region.
Thank you for that answer. I think that I'm finally beginning to understand.
Probably it would be faster without these "...", which look like no man's land...
hold_lock=mutex_trylock()
Quoted 8 lines
if (hold_lock) {
/* SMP safe */
...
mutex_unlock()
} else {
/* SMP unsafe */
...
/* maybe try again after some break or check */OOPS! Of course, since it can be called with this lock held, any break is not enough: we can only check if there is a possibility that another thread is holding the lock.
Quoted 4 lines
}
Regards,
Jarek P.
Jarek Poplawski wrote, On 11/28/2007 11:56 PM:
Quoted 30 lines
Jarek Poplawski wrote, On 11/28/2007 11:45 PM:
Quoted 28 lines
Larry Finger wrote, On 11/28/2007 04:41 PM:
Quoted 8 lines
Quoted 7 lines · Andreas Schwab
Quoted 5 lines · Larry Finger
If a particular routine needs to lock a mutex, but it may be entered with that mutex already locked, would the following code be SMP safe?
hold_lock=mutex_trylock()Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
...
if (
hold_lock)
mutex_unlock()When two CPUs may enter the critical region at the same time, what is the point of the mutex? Also, the first CPU may unlock the mutex while the second one is still inside the critical region.
Thank you for that answer. I think that I'm finally beginning to understand.
Probably it would be faster without these "...", which look like no man's land...
hold_lock=mutex_trylock()if (hold_lock) { /* SMP safe */ ... mutex_unlock() } else { /* SMP unsafe */
...But, not for sure! If our caller holds the lock and we can check this...
Quoted 14 lines
Quoted 2 lines
... /* maybe try again after some break or check */OOPS! Of course, since it can be called with this lock held, any break is not enough: we can only check if there is a possibility that another thread is holding the lock.
Quoted 4 lines
}
Regards,
Jarek P.
Quoted 34 lines · On Wed, 28 Nov 2007 23:45:13 +0100 Jarek Poplawski <[email protected]> wrote:
Larry Finger wrote, On 11/28/2007 04:41 PM:
Quoted 10 lines
Quoted 9 lines · Andreas Schwab
Quoted 7 lines · Larry Finger
If a particular routine needs to lock a mutex, but it may be entered with that mutex already locked, would the following code be SMP safe?
hold_lock=mutex_trylock()...
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
if (
hold_lock)
mutex_unlock()When two CPUs may enter the critical region at the same time, what is the point of the mutex? Also, the first CPU may unlock the mutex while the second one is still inside the critical region.
Thank you for that answer. I think that I'm finally beginning to understand.
Probably it would be faster without these "...", which look like no man's land...
hold_lock=mutex_trylock()if (hold_lock) { /* SMP safe */ ... mutex_unlock() } else { /* SMP unsafe */ ... /* maybe try again after some break or check */ }
Quoted 2 lines
Regards,
Jarek P.
WTF are you teaching a lesson on how NOT to do locking?
Any code which has this kind of convoluted dependency on conditional locking is fundamentally broken.
Signature · 1 line
Stephen Hemminger <[email protected]>
On Wed, Nov 28, 2007 at 03:33:12PM -0800, Stephen Hemminger wrote:
...
Quoted 5 lines
WTF are you teaching a lesson on how NOT to do locking?
Any code which has this kind of convoluted dependency on conditional locking is fundamentally broken.
As a matter of fact I've been thinking, about one more Re: to myself to point this all is a good example how problematic such solution would be, but I've decided it's rather apparent. IMHO learning needs bad examples too - to better understand why they should be avoided.
On the other hand, I've seen quite a lot of fundamentally right, but practically broken code, so I'm not sure what's better. And, btw., I guess this 'fundamentally broken' type of locking could be found in the kernel too, but I'd prefer not too look after this now.
Thanks,
Jarek P.
Quoted 4 lines · Larry Finger
If a particular routine needs to lock a mutex, but it may be entered with that mutex already locked, would the following code be SMP safe?
hold_lock=mutex_trylock()
The common way to deal with this is first to restructure your function into two. One always acquires the lock, and the other (often written with a "__" prefix) never acquires it. The never-acquire code does the actual work, and the always-acquire function calls it.
You then refactor the callers so that you don't have any code paths on which you can't predict whether or not the lock will be held.
<b