volatile (in comp.lang.c)

David Collier-Brown daveb at geac.UUCP
Thu Jun 9 21:58:44 AEST 1988


In article <1988May23.003847.1114 at utzoo.uucp> (Henry Spencer) writes:
| Unfortunately, this is not sufficient.  "Volatile" does not guarantee
| that operations are atomic.  It is entirely possible for x and/or y to
| contain trash because they caught the variable midway through the
| assignment.

In article <4922 at ihlpf.ATT.COM> nevin1 at ihlpf.UUCP (00704a-Liber,N.J.) writes:
| C itself does not guarantee that access to any particular data type,
| including char, is atomic.  My question is:  is there *any* use for 'volatile'
| which does not require 'atomicity' at some level?  If not, then 'volatile'
| doesn't really fix any of the problems we have without it.

  I suspect that, historically, C merely assumed that accesses to
atomic (pardon the pun) data types were atomic, and accesses to
composite ones were non-atomic.
  This tends to make a volatile declaration sufficient in the two
"old" cases:
	1) A device register, because the register was
	   integer-width[1], because the device-register-changer
	   part of the device handshook with the memory-access
	   hardware, because the memory-access hardware[2] mediated
	   no simultaneous accesses from simultaneously-running
	   processors[3] and two processes could not be running
	   simultaneously on the same processor.

	2) A signal handler, because a user process and a signal
	   handler could not be switched between in the "middle" of
	   an access to a memory location, and two processes could
	   not be running at the same time on the same processor.

  If one makes a minimum set of restrictions on device registers and
signal-handlers part of the language/runtime specification, volatile
is sufficient. If one does not, it isn't.

  This of course does not deal with the harder problem of
multiprocessors.
  In fact, it was not even sufficient for some of the first micros,
who had to have their device registers polled by an interrupt
handler/driver to see when the "valid to read" bit was on (:-{).

--dave (volatile is an engineering tradeoff, not an elegant invention) c-b
[1] Character-width on micros.
[2] One of the XXX-bus controllers on DEC equipment, the system
    controller on Honeybuns.
[3] Then a rara avis.
Disclaimer: the authors of the language may disagree about details,
    corrections are invited.
-- 
 David Collier-Brown.  {mnetor yunexus utgpu}!geac!daveb
 Geac Computers Ltd.,  | "His Majesty made you a major 
 350 Steelcase Road,   |  because he believed you would 
 Markham, Ontario.     |  know when not to obey his orders"



More information about the Comp.lang.c mailing list