Re: Volatile for shared data protected by critical section
- From: "Paul G. Tobey [eMVP]" <paul tobey _AT_ earthlink _DOT_ net>
- Date: Fri, 25 Sep 2009 18:16:06 -0700
I don't think that it optimizes register variable use across function calls.
It's been a while since I looked at it, but that's what I remember. As long
as that doesn't change, we're safe.
Paul T.
"Dean Ramsier" <ramsiernospam@xxxxxxxxxx> wrote in message
news:evdKubjPKHA.5108@xxxxxxxxxxxxxxxxxxxxxxx
Interesting, I hadn't really thought much about this. Other than
variables used for hard spin loops and register accesses, I don't recall
seeing 'volatile' used much with globals in the CE code base.
Does the current compiler implementation not attempt to optimize variables
across function calls, or are we just getting lucky because the call to
EnterCriticalSection happens to consume all the registers that could be
used for optimization?
--
Dean Ramsier - eMVP
BSQUARE Corporation
"Paul G. Tobey [eMVP]" <paultobey _at_ earthlink _dot_ net> wrote in
message news:9395D70D-BEDE-466C-BC91-AFF560B83C2E@xxxxxxxxxxxxxxxx
It should be declared volatile, regardless of whether you've properly
protected it or not. Volatile is a hint to the compiler that it should
not
assume that some previously cached (usually in a processor register),
value
will still be valid just because the compiler doesn't see any code in the
current block that will change the value. While it's doubtful that this
would ever cause you a problem if you are enclosing all code that
accesses
the variable in a critical section, future enhancements to global
optimization in the compiler might note that you are not changing that
variable value in the code block and decide that it should be cached and
only
the cached value used.
Paul T.
"shiju" wrote:
Thanks Bruce for your response.
You are right that the global data will be shared by multiple threads
but these data are protected within a critical section . But not sure
whether the global data that is protected within a critical section
needs to be declared as volatile. In some OS like linux, it says that
don't use volatile if the global data is already protected with
spin_locks etc.
(http://www.mjmwired.net/kernel/Documentation/volatile-considered-harmful.txt).
I don't see any such info for wince or windows.
Thanks,
Shiju
Bruce Eitman [eMVP] wrote:
I am not sure that I followed you completely. But, if the global data
will
be shared by multiple threads then you should use volatile. This
ensures
that the compiler knows that the data could be modified by another
thread.
It would also tell the compiler that writes must be done before the
function
call since the compiler has no idea what is being done in the critical
section functions.
.
- References:
- Volatile for shared data protected by critical section
- From: shiju
- Re: Volatile for shared data protected by critical section
- From: Bruce Eitman [eMVP]
- Re: Volatile for shared data protected by critical section
- From: shiju
- Re: Volatile for shared data protected by critical section
- From: Paul G. Tobey [eMVP]
- Re: Volatile for shared data protected by critical section
- From: Dean Ramsier
- Volatile for shared data protected by critical section
- Prev by Date: Re: [HELP! ] CE5 System always delay for a ROM HIVE device
- Next by Date: System hang before Gwes launch up
- Previous by thread: Re: Volatile for shared data protected by critical section
- Next by thread: [HELP! ] CE5 System always delay for a ROM HIVE device
- Index(es):
Relevant Pages
|