[ntp:hackers] [Gpsd-dev] ntpd shm changes

juergen perlinger juergen.perlinger at t-online.de
Mon Mar 21 16:13:08 UTC 2011

So, putting it all together:

1.) 'volatile' helps to a certain degree, because it tells the compiler
not to be clever and not to re-order instructions (like assuming two
reads without intervening write must always produce the same result...)

2.) 'volatile' is not enough due to the different write-order models of
different CPUs. We need barrier instructions to enforce the visibility
of a write operation at a given point of time during a sequence of
operations. (That is, visibility to another thread of execution.
Self-visibility is guaranteed by the C standard.)

3.) If more than one thread of execution (loosely known as a 'thread')
modifies a memory location (either in read-modify-write style or by
simply writing) we need atomic RMW or repeated CAS (compare-and-swap)

4.) Whether we use SYSV shared mem, POSIX shared mem, Windoze shared mem
or whatever possible flavour available: This just deals with the
acquisition of the memory block, not the memory access -- that will be
the same and always subject to points 1..3.

Using a circular buffer with one writer and one reader can get us off
the hook for the RMW/CAS problem of point three, but there's no real way
to avoid memory barriers -- apart from trying without and keeping the
fingers crossed. And portable barriers are tricky, because they are
non-standard and very CPU/OS/compiler dependent.

Using synchronisation primitives (mutex, condition vars, ...) normally
ensures that the visibility problem is invisible (pun intended) because
the implementation of the primitives has to take care of it. With
lock-free programming we are on our own. Personally, I would prefer
lock-free style in NTPD for obvious reasons.

I tried to solve some of the problems with the modified SHM driver you
can find in


The ntp-dev version is from January 2011 (I might upgrade in the near
future) but it works for me... feel free to dissect what I did. Until
now there has been little feedback :-(


More information about the hackers mailing list