atomicity of unlocked reads

Bruce Evans bde at zeta.org.au
Tue Sep 16 19:48:07 PDT 2003


What guarantees, if any, are there that an unlocked read provides a
valid value (either the current value or a previous value)?  Obviously
there are no guarantees if the size of the object being read is different
from the natural memory access size.

I'm mainly interested in atomic reads of pointers in circular buffers.
The read pointer is only written to by one thread and is locked by
lock R.  The write pointer is only written to by another thread and
is locked by a different lock W.  Each thread needs to read but not
write the other thread's pointer but doesn't care if it sees a stale
value (it will see an up to date value later), but does care if it
sees a garbage value.  It would be nice if each thread doesn't have
to use the other thread's lock, especially when one of the threads is
actually a fast interrupt handler so it can't use the other thread's
lock unless it is a spinlock but wants to be a sleep lock.

Sometimes even a garbage value from reading an object non-atomically
might not matter.  E.g., in sigpending() there seems to be no point
in locking the read, since a snapshot that is inconsistent due to not
locking during the read is little different from a snapshot that is
inconsistent due to the object changing after it is read.

Bruce


More information about the freebsd-smp mailing list