What's the use of volatile in Wine code?
meissner at suse.de
Wed Nov 11 06:56:08 CST 2009
On Wed, Nov 11, 2009 at 11:13:06AM +0100, Joerg-Cyril.Hoehle at t-systems.com wrote:
> AJ wrote in http://www.winehq.org/pipermail/wine-devel/2009-November/079575.html
> >If there is proper synchronization you don't need
> >volatile, and if there isn't volatile won't magically fix it.
> However, mcimidi has in its code since pre 1999:
> :/* it seems that in case of multi-threading, gcc is optimizing just a little bit
> : * too much. Tell gcc not to optimize status value using volatile.
> : */
> :while (((volatile WINE_MCIMIDI*)wmm)->dwStatus == MCI_MODE_PAUSE);
> The comment is right. Any C compiler is allowed to optimize this into
> an endless loop without volatile. But I'd rather see the following:
> :volatile dwStatus = NOT_READY; /* one central declaration */
> :while (wmm->dwStatus == PAUSE) ; /* what, busy wait!?! */
> So what's the use of volatile? When is it appropriate in Wine?
As Alexandre said only in special places.
variables set out of line by signal handlers for instance.
> I found this article from MSDN about memory acquire and release
> semantics, volatile and multiple processors very interesting. MS-VC
> changed the code generator for 'volatile' between 2003 and 2005 to
> accomodate multi-processor systems:
It pretty much depends on how dwStatus is changed actually.
The code above has "wmm" changed by threads, so just reading it
might be broken anyhow, as it might only be modified half-way
by the other thread.
Using correct locking, or atomics would help and also not make
it an endless loop.
More information about the wine-devel