This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Optimizer reordering question?


Hi,
  I have a question on how I can write a piece of code such that I can still
get the best performance but can also guarantee it's integrity. Below is a
simplified example of what I am try to do.

I have some data which resides in shared memory, and in order to protect
against multiple updates I use a lock, however I also need to protect
against a process dying half-way through an update. So I use code similar to
the following:

Lock(pshared);

if (pshared->UpdateInProgress)
  doRecovery(pshared);

pshared->UpdatedInProgress=TRUE;
pshared->value1 = localValue1; /* The critical updates */
pshared->value2 = localValue2;
pshared->value3 = localValue3;
pshared->UpdateInProgress=FALSE;

UnLock(pshared);

If when a process gets the lock, he sees that UpdateInProgress is TRUE it
knows it has some recovery to do. However my problem is how can I protect
against the optomizer re-ordering the code. I guess I need to define the
UpdateInProgress flag as volatile, but do I also need to declare the value1,
value2 and value3 is pshared as volatile too in order to guarantee that they
don't get re-ordered with respect to the UpdateInProgress flag?

Appologies for the rather long-winded explanation, but I figured it was the
best way of explaining what I am try to achieve. Any guidance is much
appreciated.

Thanks,

Trevor


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]