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
Joerg-Cyril.Hoehle@t-systems.com writes:
So what's the use of volatile? When is it appropriate in Wine?
In general it's never appropriate. There are some very specific cases where it can be used, but you have to be sure to know what you are doing. Using proper synchronization instead is strongly recommended.
On Wed, Nov 11, 2009 at 11:13:06AM +0100, Joerg-Cyril.Hoehle@t-systems.com wrote:
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?
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: http://msdn.microsoft.com/en-us/library/ms686355(VS.85).aspx
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.
Ciao, Marcus
2009/11/11 Joerg-Cyril.Hoehle@t-systems.com:
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 haven't looked in detail at the context of your patch, but this looks appropriate to it: http://lkml.indiana.edu/hypermail/linux/kernel/0607.0/1566.html
Particularly the part about volatile storage vs. access.