On 06/27/2011 07:52 PM, Hans-Peter Diettrich wrote:

You forget the strength of protection.
Maybe I seem to see what you mean.

You think, a critical section will not prevent that the data that is accessed by both threads is duplicated in a register or in the Data cache of the two (or more) CPUs that are trying to access the resource (variable). Thus a concurrent access might erroneously offer a not updated state to the other CPU.

But

Regarding Registers, the C language provides the "volatile" keyword to prevent the code from caching the values of such variables in registers. With Pascal (that does not have the volatile keyword), all static data is handled as volatile. This might in fact trigger a protection problem, as (AFAIK) data on the heap (e.g. class variables) are not handled as volatile. But this potential problem with threads accessing class variables or e.g. a linked list is independent from SMP. In fact I have no idea if/how this can be handled with Pascal-language means.

Regarding Data Cache, AFAIK, the hardware prevents this. If one CPU writes a value, an "invalidate" signal for the addresses in that cache line is sent to all caches, and so all other CPUs will reread the data from the main memory, which in turn will force a preliminary write from cache to main memory for the cache line of the first CPU.


If this would not be true, practically no threaded application would work, as a thread would close to never see what another thread writes, as with today's huge cache sizes normally any data written by a thread resides completely in the "personal" cache of the CPU it runs on.

Thus "Locked" instructions (that force this mechanism, guaranteeing proper read-modify-write access even if multiple instructions access the same data are issued at the same time <overlapping execution> ) only are necessary outside a critical section protecting the data in question. (And this in fact is necessary to create the MUTEX/FUTEX functionality itself, and to do code the performance/latency of which would suffer too much from introducing critical sections.)

If you think I get this wrong, please do provide an example how you think such a conflict within a critical section might happen.


My conclusion is, that the said problem with linked lists results from the pointers not being defined as "volatile". And I really don't know how this could be cured in Pascal other than doing ASM. (I was not aware of this problem and I feel it should be discussed in the FPC mailing list.)

-Michael

--
_______________________________________________
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply via email to