Re:Secrets of dispatch_once; x86 performance trickery
by quotemstr on Friday June 06, 2014 @09:32PM
(#7860962)
Relying on an implementation detail of the cpuid instruction seems like a very bad idea. It'd be safer, I think, and just as fast, to initially use the atomic version of the code, then NOP out the LOCK prefix once initialization is complete.
[ Parent
| on HN ]
Re:Secrets of dispatch_once; x86 performance trickery
by userbinator on Friday June 06, 2014 @10:23PM
(#7861071)
Intel specifies CPUID to be serialising, this is unlikely to change in the future.
Re:Secrets of dispatch_once; x86 performance trickery
by cfallin on Saturday June 07, 2014 @01:01AM
(#7861346)
It's actually part of the architectural definition, so Intel could not change this without breaking ISA compatibility. And they take x86 compatibility very seriously!
Re:Secrets of dispatch_once; x86 performance trickery
by jsnell on Friday June 06, 2014 @09:57PM
(#7861014)
That can't possibly work. There's just one dispatch_one function but many callsites, and for each callsite you could have multiple distinct predicates.
Re:Secrets of dispatch_once; x86 performance trickery
by quotemstr on Friday June 06, 2014 @10:04PM
(#7861037)
You'd allocate a trampoline for each predicate, jump unconditionally into that, then jump unconditionally back to mainline code. Each trampoline can be modifier individually. Of course that doesn't work with the current dispatch_once API, though.
Re:Secrets of dispatch_once; x86 performance trickery
by mikeash on Friday June 06, 2014 @10:16PM
(#7861058)
Doesn't this have all the same problems as the way they currently do it? The only difference between the two is that Apple's implementation puts the flag in a variable, while you're basically proposing to put the flag in code. You still need to ensure proper ordering on both reads and writes and ensure that speculative execution doesn't end up giving you the wrong answer.
Re:Secrets of dispatch_once; x86 performance trickery
by mikeash on Friday June 06, 2014 @10:12PM
(#7861048)
The rules are different when you're an OS vendor who makes all of the hardware that the OS officially supports.
It would be a bad idea for you or I, but for Apple there's no problem. If Intel ever ships a CPU that needs something else, then Apple can update their code for the OS release that goes out on the corresponding Macs.
Re:Secrets of dispatch_once; x86 performance trickery
by adamjernst on Friday June 06, 2014 @10:14PM
(#7861052)
Requires writing to executable pages, which iOS doesn't have.