What's the use of volatile in Wine code?

Joerg-Cyril.Hoehle at t-systems.com Joerg-Cyril.Hoehle at t-systems.com
Wed Nov 11 04:13:06 CST 2009


Hi,

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?

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:
http://msdn.microsoft.com/en-us/library/ms686355(VS.85).aspx

Regards,
	Jörg Höhle



More information about the wine-devel mailing list