On Tue, Aug 27, 2002 at 11:32:49AM -0700, Seth Arnold wrote: > On Tue, Aug 27, 2002 at 01:52:13PM -0400, anton wilson wrote: > > Does a volatile variable never reside in cache? > > It is my understanding of volatile that it will force the compiler to > never rely on a register for the value -- it must always fetch from > memory. The memory fetches will still go through cache. I think it is up > to the hardware to ensure that cache is coherent across all CPUs. As far as my understanding goes CPU will always cache it and caches will NOT be coherent between CPUs on many platforms. These platforms have an instruction that enforces the coherency at a given point. So volatile by itself gives nothing. In kernel you have to use barrier() macro (resp wmb() and rmb() macros). They both ensure CPU cache coherency and act as optimization barriers (cpu will not rely on register for any value across the barrier). That makes volatile mostly useless and it's actually used very rarely. In userland you have to rely on synchronization primitives that do act as barriers in kernel and should do the same in userland. > > struct _test{ > > volatile x; > > char arr[1024]; > > }test; > > > > if I use x, will the first part > > of the arr string get read into cache or not? > > I'd expect this to be highly dependant upon where the struct is located > in memory; there are probably places in memory where the first 28 bytes > f the array would be cached, and other places in memory where none of > the array would be cached. And, as always, this is highly platform > dependant. :) I only wonder, what could not having something go through CPU cache be good for. And I suspect, that most CPUs have access to memory only through the cache. ------------------------------------------------------------------------------- Jan 'Bulb' Hudec <bulb@ucw.cz> -- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/