MUTEX: Introduce simple mutex implementation
Our Fearless Leader, in a fit of madness, intoned:
Quoted 7 lines
A real semaphore is counting.
Dammit, unless the pure mutex has a _huge_ performance advantage on major architectures, we're not changing it. There's absolutely zero point. A counting semaphore is a perfectly fine mutex - the fact that it can _also_ be used to allow more than 1 user into a critical region and generally do other things is totally immaterial.
You're being thick today. Pay attention to the arguments.
A counting semaphore is NOT a perfectly fine mutex, and it SHOULD be changed.
People are indeed unhappy with the naming, and whether patching 95% of the callers of up() and down() is a good idea is a valid and active subject of debate. (For an out-of-tree -rt patch, is was certaintly an extremely practical solution.)
But regardless of the eventual naming convention, mutexes are a good idea. A mutex is *safer* than a counting semaphore. That's the main benefit. Indeed, unless there's a performance advantage to a counting semaphore, you should use a mutex!
It documents in the source what you're doing. Using a counting semaphore for a mutex is as silly as using a float for a loop index. Even if there isn't much speed penalty on modern processors.
Or perhaps I should compare it to using void * everywhere. That's a perfectly fine pointer; why type-check it?
A separate mutex type allows extra error-checking. You can keep track of the current holder (since there can be only one) and check that the same person released it and didn't try to double-acquire it.
You can do priority inheritance, which is the main motivation for doing this work in the -rt patches.
This isn't about speed, it's about bug-free code. And having a mutex abstraction distinct from a general counting semaphore is damn useful error-checking, even if it is simply a thin wrapper over a counting semaphore. The only reason the code is full of counting semaphores right now is that that's all people had.
Better to give them the right tool.
Quoted 2 lines · On Thu, 15 Dec 2005, [email protected] wrote:
A counting semaphore is NOT a perfectly fine mutex, and it SHOULD be changed.
Don't be silly.
First off, the data structure is called a "semaphore", and always has been. It's _never_ been called a "mutex" in the first place, and the operations have been called "down()" and "up()", because I thought calling them P() and V() was just too damn traditional and confusing (I don't speak dutch, and even if I did, I think shortening names to that degree is just evil).
And dammit, a counting semaphore (and usually you don't even say the "counting" part, since counting is really always there) is just about _the_ classical mutual exclusion mechanism. If somebody doesn't know that, he has absolutely _no_ place talking about mutexes etc.
And a semaphore _is_ a mutex. Anybody who disputes that is just being a total troll. Even classically, the case where the semaphore was initialized to 1 is very very traditional, and is very much part of the whole point of a semaphore. Sometimes they are called "binary semaphores", but dammit, they are just the same thing.
A patch that
- creates a non-counting mutex
- .. that is SLOWER than the current counting one
- .. and keeps the old "semaphore" and "up/down" naming
is simply INCREDIBLY BROKEN. It has absolutely _zero_ redeeming features. I can't understand how there are a hundred emails in my mailbox even discussing it.
And I can't understand how somebody has the balls to even say that a semaphore isn't a mutex. That's like saying that an object of type "long" isn't an integer, because only "int" objects are integers. That's just INSANE.
Quoted 4 lines
People are indeed unhappy with the naming, and whether patching 95% of the callers of
up()anddown()is a good idea is a valid and active subject of debate. (For an out-of-tree -rt patch, is was certaintly an extremely practical solution.)
Whatever people you claim are unhappy with the naming are
- obviously totally unaware of very basic synchronization primitives used in concurrent programming
- likely haven't spent any time at all looking at the kernel source code.
- haven't _ever_ complained that I've seen before this totally made-up discussion.
In other words, you are
(a) totally making up the claim that people are really unhappy
(b) jerking people around who _do_ know about semaphores and _have_ worked with the kernel locking primitives and understand them well
So tell me, what do you think about your own arguments in that light?
Quoted 4 lines
But regardless of the eventual naming convention, mutexes are a good idea. A mutex is *safer* than a counting semaphore. That's the main benefit. Indeed, unless there's a performance advantage to a counting semaphore, you should use a mutex!
Hey, feel free to introduce a mutex, but DAMMIT, just call it that, instead of switching people over.
And even then, it should damn well also:
- really _be_ faster. On platforms that matter.
- have enough real other advantages that it's worth introducing another abstraction, and more conceptual complexity. At least the RT patches had a reason for them.
And besides, all your "safer" arguments are pretty damn pointless in the face of the fact that we have basically had zero bugs with the semaphores. This is not where the bugs happen. Yeah, yeah, double releases can happen, but it sure as hell isn't on my radar of things I remember people doing.
So when you say "This isn't about speed, this is about bug-free code", you're just making that up. It's doubly silly when your "safer" implementation uses totally illogical names. THAT is what creates bugs.
So go away.
Come back if you have pondered, and accepted reality, and perhaps have an acceptable patch that introduces a separate data structure.
And no, we're not switching users over whole-sale. First you introduce the new concept. Only THEN can you can switch over INDIVIDUAL LOCKS with reasons for why it's worth it.
And hell yes, performance does matter.
Linus
Quoted 6 lines · Linus Torvalds
First off, the data structure is called a "semaphore", and always has been. It's _never_ been called a "mutex" in the first place, and the operations have been called "
down()" and "up()", because I thought calling themP()andV()was just too damn traditional and confusing (I don't speak dutch, and even if I did, I think shortening names to that degree is just evil).
Just FYI, according to Dijkstra[1] V means "verhoog" which is dutch for "increase". P means "prolaag" which isn't a dutch word, just something Dijkstra invented. I guess he did that because "decrease" is "verlaag" in dutch and that would give you the confusing V() and V() operations...
Other explanations you see in dutch CS courses are "passeer" (pass), "probeer" (try), "vrijgave" (unlock).
I do agree that Dijkstra should have used Prolaag() and Verhoog(), but I guess those operations wouldn't have sticked in the english CS literature.
Erik
[1] http://www.cs.utexas.edu/users/EWD/ewd00xx/EWD74.PDF
Signature · 2 lines
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 -- | Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
Erik Mouw <[email protected]> wrote:
| Just FYI, according to Dijkstra[1] V means "verhoog" which is dutch for | "increase". P means "prolaag" which isn't a dutch word, just something | Dijkstra invented. I guess he did that because "decrease" is "verlaag" | in dutch and that would give you the confusing V() and V() | operations... | | Other explanations you see in dutch CS courses are "passeer" (pass), | "probeer" (try), "vrijgave" (unlock).
As far as I can remember, P() stands for "pakken" (grab) and V() stands for "vrijgeven" (release).
Signature · 3 lines
Dick Streefland //// Altium BV [email protected] (@ @) http://www.altium.com --------------------------------oOO--(_)--OOo---------------------------
Quoted 5 lines · Erik Mouw
Just FYI, according to Dijkstra[1] V means "verhoog" which is dutch for "increase". P means "prolaag" which isn't a dutch word, just something Dijkstra invented. I guess he did that because "decrease" is "verlaag" in dutch and that would give you the confusing
V()andV()operations...
Last night I've been browsing a little more through Dijkstra's papers, and in a completely unrelated paper[1] about a now obsolete computer I found that "prolaag" is a neologism coming from "probeer te verlagen", which means "try and decrease".
Erik
[1] http://www.cs.utexas.edu/users/EWD/transcriptions/EWD00xx/EWD51.html
Signature · 2 lines
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 -- | Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
Erik Mouw wrote (ao):
Quoted 11 lines · Erik Mouw
Quoted 5 lines
Just FYI, according to Dijkstra[1] V means "verhoog" which is dutch for "increase". P means "prolaag" which isn't a dutch word, just something Dijkstra invented. I guess he did that because "decrease" is "verlaag" in dutch and that would give you the confusing
V()andV()operations...Last night I've been browsing a little more through Dijkstra's papers, and in a completely unrelated paper[1] about a now obsolete computer I found that "prolaag" is a neologism coming from "probeer te verlagen", which means "try and decrease".
"probeer te verlagen" translates to "try to decrease".
"try and decrease" would be "probeer en verlaag".
Signature · 2 lines
Humilis IT Services and Solutions http://www.humilis.net
Quoted 10 lines · Sander
Erik Mouw wrote (ao):
Quoted 4 lines
Last night I've been browsing a little more through Dijkstra's papers, and in a completely unrelated paper[1] about a now obsolete computer I found that "prolaag" is a neologism coming from "probeer te verlagen", which means "try and decrease".
"probeer te verlagen" translates to "try to decrease".
"try and decrease" would be "probeer en verlaag".
Just in case you don't know, "try and" in English is an informal equivalent of "try to". I agree your translation is probably better. :)
-Doug
Douglas McNaught wrote (ao):
Quoted 7 lines
Quoted 6 lines · Sander
Erik Mouw wrote (ao):
Quoted 4 lines
Last night I've been browsing a little more through Dijkstra's papers, and in a completely unrelated paper[1] about a now obsolete computer I found that "prolaag" is a neologism coming from "probeer te verlagen", which means "try and decrease".
Quoted 6 lines
Quoted 3 lines
"probeer te verlagen" translates to "try to decrease".
"try and decrease" would be "probeer en verlaag".
Just in case you don't know, "try and" in English is an informal equivalent of "try to". I agree your translation is probably better. :)
I didn't, so thanks for the education :-)
I read it as two things (try something, and decrease), but I see what is meant now. And I'm sure Erik Mouw knows his was around in both Dutch and English :-)
Signature · 2 lines
Humilis IT Services and Solutions http://www.humilis.net
Quoted 9 lines · Sander
Erik Mouw wrote (ao):
Quoted 4 lines
Last night I've been browsing a little more through Dijkstra's papers, and in a completely unrelated paper[1] about a now obsolete computer I found that "prolaag" is a neologism coming from "probeer te verlagen", which means "try and decrease".
"probeer te verlagen" translates to "try to decrease".
"try and decrease" would be "probeer en verlaag".
I know, but that's how Dijkstra translates it. I guess he knew what he meant.
Erik
Signature · 4 lines
+-- Erik Mouw -- www.harddisk-recovery.nl -- 0800 220 20 20 -- | Eigen lab: Delftechpark 26, 2628 XH, Delft, Nederland | Files foetsie, bestanden kwijt, alle data weg?! | Blijf kalm en neem contact op met Harddisk-recovery.nl!
Quoted 15 lines · Linus Torvalds
Quoted 2 lines · On Thu, 15 Dec 2005, [email protected] wrote:
A counting semaphore is NOT a perfectly fine mutex, and it SHOULD be changed.
Don't be silly.
First off, the data structure is called a "semaphore", and always has been. It's _never_ been called a "mutex" in the first place, and the operations have been called "
down()" and "up()", because I thought calling themP()andV()was just too damn traditional and confusing (I don't speak dutch, and even if I did, I think shortening names to that degree is just evil).
Quoted 4 lines
And dammit, a counting semaphore (and usually you don't even say the "counting" part, since counting is really always there) is just about _the_ classical mutual exclusion mechanism. If somebody doesn't know that, he has absolutely _no_ place talking about mutexes etc.
Dijkstra (that cannot talk about this due to much more serious reasons) didn't know this, because semaphores were initially used as a wait/signal mechanism to provide concurrency control between "process context" and "interrupts" however they were called at the time, and calling this "just mutual exclusion" is stretching a bit far.
Mutex implies usage pattern much narrower than generic semaphore.
Quoted 2 lines
And a semaphore _is_ a mutex.
Nope, a mutex is a semaphore and not other way around. For one thing, a notion of ownership is well-defined for the mutex, but it is not for a semaphore. This is what they call "sub-type".
Quoted 2 lines
Anybody who disputes that is just being a
total troll.
Oh wait... what is that thing on the right of my screen? This is... gnome task-list!
[...]
Quoted 5 lines
And I can't understand how somebody has the balls to even say that a semaphore isn't a mutex. That's like saying that an object of type "long" isn't an integer, because only "int" objects are integers. That's just INSANE.
And the person that claims that "long" is an "int" is non-portable. :-)
[...]
Quoted 2 lines
Linus
Nikita.
Quoted 4 lines
And I can't understand how somebody has the balls to even say that a semaphore isn't a mutex. That's like saying that an object of type "long" isn't an integer, because only "int" objects are integers. That's just INSANE.
I didn't say it isn't a mutex, I said it isn't a GOOD one!
The fundamental reason is that a semaphore doesn't have an owner, and a mutex does. And you can do a lot when you know who owns the lock.
Quoted 7 lines
Quoted 4 lines
People are indeed unhappy with the naming, and whether patching 95% of the callers of
up()anddown()is a good idea is a valid and active subject of debate. (For an out-of-tree -rt patch, is was certaintly an extremely practical solution.)In other words, you are
(a) totally making up the claim that people are really unhappy
Huh? I thought *you* were violently unhappy with the idea of naming mutex acquire and release down() and up(), and your e-mail is an example of this unhapiness.
Am I making it up that you are unhappy with usurping the down() and up() names for mutex use? If this is you being happy, I'd hate to see unhappy.
Quoted 1 line
So tell me, what do you think about your own arguments in that light?
I think they're still completely valid. Nothing you've said even seems to address the points I've raised.
Quoted 7 lines
Quoted 4 lines
But regardless of the eventual naming convention, mutexes are a good idea. A mutex is *safer* than a counting semaphore. That's the main benefit. Indeed, unless there's a performance advantage to a counting semaphore, you should use a mutex!
Hey, feel free to introduce a mutex, but DAMMIT, just call it that, instead of switching people over.
As I said, as long as the -rt patch was not in the main tree, taking advantage of the fact that 95% of the down() and up() callers just want a mutex was a sensible implementation tradeoff. For merging it into the tree, it's ugly, and people don't like that. The -rt folks have gotten used to their naming perversions and so don't feel as much repugnance.
Quoted 5 lines
And even then, it should damn well also:
- really _be_ faster. On platforms that matter.
- have enough real other advantages that it's worth introducing another abstraction, and more conceptual complexity. At least the RT patches had a reason for them.
Agreed. A mutex that's slower than a counting semaphore needs to be dragged out behind the wodshed and strangled. If you can't do any better, it can just *be* a counting semaphore.
Quoted 4 lines
And besides, all your "safer" arguments are pretty damn pointless in the face of the fact that we have basically had zero bugs with the semaphores. This is not where the bugs happen. Yeah, yeah, double releases can happen, but it sure as hell isn't on my radar of things I remember people doing.
There haven't been problems with the semaphore *implementation*, but people screw up and deadlock themselves often enough. I sure remember double-acquire lockups. Forgive me if I don't grep the archives, but I remember people showing code paths that led to them.
Admittedly, lock *ordering* problems are the most common deadlock situtation but hey, guess what! Priority inheritance code can be extended to notice that, too. (There's a performance hit, so it'd be a debug option.)
But all of this requires that a lock have an identifiable owner, which is something hat a mutex has and a semaphore fundamentally doesn't.
Quoted 5 lines
So when you say "This isn't about speed, this is about bug-free code", you're just making that up.
It's doubly silly when your "safer"
implementation uses totally illogical names. THAT is what creates bugs.
If you want to argue about names, go discuss gay marriage.
I don't care what it's *called*. I care that we have stronger conditions that we can test for correctness.
Quoted 4 lines
So go away.
Come back if you have pondered, and accepted reality, and perhaps have an acceptable patch that introduces a separate data structure.
Ha! I still say you're wrong, and I'm not going to fold over an obvious technical point just because of flaming.
Are we having some communication problems? I find it hard to believe that you're actually this *stupid*, but we might not be talking about the same thing.
I took your posting to say that
a) Using the names "struct semaphore", "up()" and "down()" for a mutex is monumentally brain-dead. I'm not arguing, although I understand the pragmatic reasons for the original abuse of notation.
b) There is no need for a mutex implementation, because a semaphore can do anything that a mutex can. Here, I absolutely disagree. There are things you can do with a mutex that you CANNOT do with a general semaphore, because a mutex has stronger invariants.
A counting semaphore can do MOST of what a mutex does, and is demonstrably close enough for a lot of uses.
Quoted 3 lines
And no, we're not switching users over whole-sale. First you introduce the new concept. Only THEN can you can switch over INDIVIDUAL LOCKS with reasons for why it's worth it.
Given that 95% of callers are using it as mutex, you're making this 20 times more work than necessary. Convert 'em all and change the 5% that need the counting back.
Quoted 1 line
And hell yes, performance does matter.
I'm not arguing, but this seems to be at odds with your earlier statement:
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Dammit, unless the pure mutex has a _huge_ performance advantage on major architectures, we're not changing it.
There is obviously no reason to accept a performance *decrease*, but any potential performance *increase* is unimportant and incidental.
Quoted 2 lines · Which is exactly what I
Quoted 2 lines
Indeed, unless there's a performance advantage to a counting semaphore, you should use a mutex!
Quoted 4 lines · On Thu, 15 Dec 2005, [email protected] wrote:
Huh? I thought *you* were violently unhappy with the idea of naming mutex acquire and release
down()andup(), and your e-mail is an example of this unhapiness.
Ahh, I thought you were considering naming unhappiness to be a reason _for_ the mutex change.
down() and up() aren't the traditional names for the operations, P()/V() are, and I thought you were arguing that some people might dislike the _current_ naming.
Quoted 4 lines
There haven't been problems with the semaphore *implementation*, but people screw up and deadlock themselves often enough. I sure remember double-acquire lockups. Forgive me if I don't grep the archives, but I remember people showing code paths that led to them.
Double aquires certainly occasionally happen, but they are (assuming it's a real double aquire, and not a race due to lock ordering) easy to see, since it just hangs the process and you get a traceback and find it.
But mutexes don't help either. A mutex will hang exactly the same way, with exactly the same behaviour, and aren't any easier to debug (as mentioned, a hung semaphore isn't exactly hard to debug).
Or maybe you're talking about a _recursive_ mutex, which is something that actually has totally different semantics. We've discussed adding them, but pretty much every time it was the result of some really really bad locking design, so at least so far we've instead decided to bite the bullet and fix the locking.
So yes, recursive mutexes can be easier to use, but they really do allow (and thus indirectly encourage) bad locking. So I'm not convinced we want one.
Quoted 2 lines
But all of this requires that a lock have an identifiable owner, which is something hat a mutex has and a semaphore fundamentally doesn't.
Actually, we've certainly had semaphore debugging patches which consider the last person who successfully got a semaphore to be the "owner".
Sure, it's not well-defined for the generic semaphore case, but so what? In the generic case, you can't use mutexes anyway. For _debugging_ ownership as in "who got this lock last" is still useful. I know I've cooked up trivial patches to do that when I was trying to figure out a lock ordering bug.
Google is your friend. Just try "semaphore owner debug", and the #2 hit is a patch that does exactly that for Linux.
Quoted 2 lines
I don't care what it's *called*. I care that we have stronger conditions that we can test for correctness.
Hey, if so, please don't encourage the current patch.
We can certainly add a new lockign mechanism, I'm just not at all convinced that it's warranted. I certainly disagree with you that using semaphores would somehow be less easy to test for correctness.
Linus
Quoted 2 lines
Ahh, I thought you were considering naming unhappiness to be a reason _for_ the mutex change.
Good gods, no. The best mnemonics for P() and V() I've ever seen were in the Amiga which called them Procure() and Vacate(), but the names still suck mightily.
Quoted 3 lines
Double aquires certainly occasionally happen, but they are (assuming it's a real double aquire, and not a race due to lock ordering) easy to see, since it just hangs the process and you get a traceback and find it.
Ah, now we get to the valid point.
Quoted 3 lines
But mutexes don't help either. A mutex will hang exactly the same way, with exactly the same behaviour, and aren't any easier to debug (as mentioned, a hung semaphore isn't exactly hard to debug).
Well, a mutex can detect it immediately, rather than via a timeout, but that's a matter of a few seconds, and the vast majority of the evidence you want is frozen by the deadlock itself.
Quoted 3 lines
So yes, recursive mutexes can be easier to use, but they really do allow (and thus indirectly encourage) bad locking. So I'm not convinced we want one.
Agreed. Maybe someone will someday find an application where there's really no way around it, but avoiding the need is generally better design.
Quoted 7 lines
Quoted 2 lines
But all of this requires that a lock have an identifiable owner, which is something hat a mutex has and a semaphore fundamentally doesn't.
Actually, we've certainly had semaphore debugging patches which consider the last person who successfully got a semaphore to be the "owner".
Sure, it's not well-defined for the generic semaphore case, but so what?
An excellent point. As long as it's only used for post-mortem debugging, and not to verify invariants at run-time, you can keep track of "who would be the woner if this were a mutex".
Quoted 8 lines
Quoted 2 lines
I don't care what it's *called*. I care that we have stronger conditions that we can test for correctness.
Hey, if so, please don't encourage the current patch.
We can certainly add a new locking mechanism, I'm just not at all convinced that it's warranted. I certainly disagree with you that using semaphores would somehow be less easy to test for correctness.
Let's go through what you lose if you give up a known lock owner and just use something probabilistic....
Detecting deadlocks - can be done immediately and definitively with a lock owner, and only via timeouts without. Still, not a deal-breaker.
Multi-lock deadlocks - same, although the detection code is more complex so probably shouldn't be enabled all the time.
Double-release: can be instantly detected with a known mutex, but will produce really odd misbehaviour with a semaphore. Still, you're quite right that failing to release on a failure path is by far the more common bug.
Priority inheritance. This is the original reason that the -rt patch implemented mutexes, and requires accurate lock owner information. Not having it is a show-stopper here.
Source documentation. This is more a style thing, but I really like putting as much information into the source as possible. (If comments worked, we wouldn't need sparse.)
So the latter two are the only really good reasons. Still, I think they're persuasive.
Can anyone think of any other benefits?
Quoted 8 lines · On Thu, 2005-12-15 at 14:09 -0500, [email protected] wrote:
Quoted 1 line
As I said, as long as the -rt patch was not in the main tree, taking advantage of the fact that 95% of the
down()andup()callers just want a mutex was a sensible implementation tradeoff. For merging it into the tree, it's ugly, and people don't like that. The -rt folks have gotten used to their naming perversions and so don't feel as much repugnance.
The naming in the -rt side is to try to keep things as much in parallel to the mainline as possible. I don't think Ingo would have a problem with up / down just being used for semaphores, and having true mutex names for just that. In fact that would help us a lot. A lot of bugs fixes that I send to Ingo, is finding places that use mutex when they are really counting semaphores, and thus cant have PI.
Quoted 1 line
...
Quoted 8 lines
Quoted 6 lines
So when you say "This isn't about speed, this is about bug-free code", you're just making that up.
It's doubly silly when your "safer"
implementation uses totally illogical names. THAT is what creates bugs.If you want to argue about names, go discuss gay marriage.
Are you suggesting a "mutex union"?
Quoted 3 lines
I don't care what it's *called*. I care that we have stronger conditions that we can test for correctness.
Well a name is helpful in understanding what's going on. Especially if we want new up and coming kernel programmers to help out. Instead of staying with what is there now.
Also, while we're at it, lets fix that damn down_trylock (or mutex_trylock) to return 1 on success, 0 on contention, just like the spin_trylock does!!!
Actually, that alone is a good argument to not keep the same names. We can keep down_trylock as the same perverted self, and have mutex_trylock do it right. Of course, special care is needed when doing this conversion, but a wrong pick should show itself right away.
Quoted 27 lines
Quoted 5 lines
So go away.
Come back if you have pondered, and accepted reality, and perhaps have an acceptable patch that introduces a separate data structure.
Ha! I still say you're wrong, and I'm not going to fold over an obvious technical point just because of flaming.
Are we having some communication problems? I find it hard to believe that you're actually this *stupid*, but we might not be talking about the same thing.
I took your posting to say that
a) Using the names "struct semaphore", "
up()" and "down()" for a mutex is monumentally brain-dead. I'm not arguing, although I understand the pragmatic reasons for the original abuse of notation.b) There is no need for a mutex implementation, because a semaphore can do anything that a mutex can. Here, I absolutely disagree. There are things you can do with a mutex that you CANNOT do with a general semaphore, because a mutex has stronger invariants.
A counting semaphore can do MOST of what a mutex does, and is demonstrably close enough for a lot of uses.
Quoted 7 lines
Quoted 3 lines
And no, we're not switching users over whole-sale. First you introduce the new concept. Only THEN can you can switch over INDIVIDUAL LOCKS with reasons for why it's worth it.
Given that 95% of callers are using it as mutex, you're making this 20 times more work than necessary. Convert 'em all and change the 5% that need the counting back.
I disagree with doing that. Especially since I've argued that a mutex is a semaphore, but a semaphore is not a mutex. So I rather go slowly changing the semaphores that are acting as mutexes, (since those that are not changed are not broken) than doing the change all mutexes to semaphores, where a mutex can not always act like a semaphore, and then go and break those 5%.
In reality, this is what the RT patch did. All semaphores (up / down) became mutexes, and then we manually found the counting semaphores and started switching them to compat_semaphores (what semaphore is today). I'm still sending in patches to fix these.
-- Steve
Quoted 5 lines · Linus Torvalds
Quoted 2 lines · On Thu, 15 Dec 2005, [email protected] wrote:
A counting semaphore is NOT a perfectly fine mutex, and it SHOULD be changed.
Quoted 8 lines
Don't be silly.
First off, the data structure is called a "semaphore", and always has been. It's _never_ been called a "mutex" in the first place, and the operations have been called "
down()" and "up()", because I thought calling themP()andV()was just too damn traditional and confusing (I don't speak dutch, and even if I did, I think shortening names to that degree is just evil).
Thank you for the down and up, I always had problems way back when I was in college. I could never remember which was which (between P and V).
Quoted 10 lines
And dammit, a counting semaphore (and usually you don't even say the "counting" part, since counting is really always there) is just about _the_ classical mutual exclusion mechanism. If somebody doesn't know that, he has absolutely _no_ place talking about mutexes etc.
And a semaphore _is_ a mutex. Anybody who disputes that is just being a total troll. Even classically, the case where the semaphore was initialized to 1 is very very traditional, and is very much part of the whole point of a semaphore. Sometimes they are called "binary semaphores", but dammit, they are just the same thing.
<Total Troll> ;)
Uh, I think that a semaphore is _not_ a mutex. As someone early said, that a mutex is a semaphore, but not the other way around. I have used counting semaphores for resource allocations, not the semaphore=1 kind. This is not a mutual exclusion, its shared.
Also a semaphore can be declared locked, a mutex cant.
Of course, if you said "binary semaphores" is a mutex, then I would agree with you.
</Total Troll>
Quoted 9 lines
A patch that
- creates a non-counting mutex
- .. that is SLOWER than the current counting one
- .. and keeps the old "semaphore" and "up/down" namingis simply INCREDIBLY BROKEN. It has absolutely _zero_ redeeming features. I can't understand how there are a hundred emails in my mailbox even discussing it.
ACK
But that was just somebody's (David) first crack at this patch. But I've been pushing that he should first submit the mutex, where everyone else can help make it really fast, and then submit the case by case places that the semaphores should be replaced with mutex. That's the most logical way I see it. And yes, even if that means lots of patches, but it makes it easier for more than one person to submit that, and review.
Quoted 5 lines
And I can't understand how somebody has the balls to even say that a semaphore isn't a mutex. That's like saying that an object of type "long" isn't an integer, because only "int" objects are integers. That's just INSANE.
No it's like saying a integer is a long. Wait did someone else say that already?
Quoted 6 lines
Quoted 5 lines
People are indeed unhappy with the naming, and whether patching 95% of the callers of
up()anddown()is a good idea is a valid and active subject of debate. (For an out-of-tree -rt patch, is was certaintly an extremely practical solution.)
Quoted 6 lines
Whatever people you claim are unhappy with the naming are
- obviously totally unaware of very basic synchronization primitives used in concurrent programming
- likely haven't spent any time at all looking at the kernel source code.
- haven't _ever_ complained that I've seen before this totally made-up discussion.
:( I'm unhappy with calling a mutex down and up. But lets see if I am any of the above?
1. Being the one to remove the global PI lock in the rt patch, gives me some credit that I understand basic synchronization primitives used in concurrent programming.
2. Have been playing with the Linux kernel source since 1998 (just not publicly until 2003).
3. OK, you got me there ;)
Quoted 5 lines
In other words, you are
(a) totally making up the claim that people are really unhappy
(b) jerking people around who _do_ know about semaphores and _have_ worked with the kernel locking primitives and understand them well
I'm one that would like to see semaphores turn to mutex when they really are one. But I'd like to keep up / down for semaphores (as they are today) and introduce a new mutex api mutex_lock / mutex_unlock, since I think that is the best way to explain what's going on.
Quoted 10 lines
So tell me, what do you think about your own arguments in that light?
Quoted 4 lines
But regardless of the eventual naming convention, mutexes are a good idea. A mutex is *safer* than a counting semaphore. That's the main benefit. Indeed, unless there's a performance advantage to a counting semaphore, you should use a mutex!
Hey, feel free to introduce a mutex, but DAMMIT, just call it that, instead of switching people over.
ACK
Quoted 6 lines
And even then, it should damn well also:
- really _be_ faster. On platforms that matter.
- have enough real other advantages that it's worth introducing another abstraction, and more conceptual complexity. At least the RT patches had a reason for them.
Actually, I would like this change to make it in mainline to help with the RT patches.
Quoted 12 lines
And besides, all your "safer" arguments are pretty damn pointless in the face of the fact that we have basically had zero bugs with the semaphores. This is not where the bugs happen. Yeah, yeah, double releases can happen, but it sure as hell isn't on my radar of things I remember people doing.
So when you say "This isn't about speed, this is about bug-free code", you're just making that up. It's doubly silly when your "safer" implementation uses totally illogical names. THAT is what creates bugs.
So go away.
Quoted 8 lines
Come back if you have pondered, and accepted reality, and perhaps have an acceptable patch that introduces a separate data structure.
And no, we're not switching users over whole-sale. First you introduce the new concept. Only THEN can you can switch over INDIVIDUAL LOCKS with reasons for why it's worth it.
And hell yes, performance does matter.
And so do a lot of other things.
-- Steve