Dynamic reads without locking.

Bernd Walter ticso at cicely12.cicely.de
Wed Oct 8 07:55:18 PDT 2003


On Wed, Oct 08, 2003 at 02:58:02PM +0200, Harti Brandt wrote:
> uint8_t foo;
> 
> (guaranteeing that the data type itself is atomic). But if a writer sets
> foo as above and you read foo without locking, you might get a wrong
> value:
> 
> mtx_lock(...)
> foo = 77;
> 
> 				->	bar = foo; /* bar is 77 */
> 
> foo &= 0x1f;
> mtx_unlock(...)

That part is obviuosly.

> Even if you write
> 
> 	foo = data & 0x1f
> 
> it may not help (one has to understand all that stuff about sequence
> points in the C-standard).

Unlikely, but I agree that it might cause problems.
Maybe we should have atomic_load/atomic_store without barriers to be
100% shure on that.

> So you can go without locking in the reader only if:
> 
>   - the datatype is atomic (depends on your architecture)
>   - all writers ensure that they write only consistent values to the
>     variable
> 
> The 2nd point needs very careful thinking in every case.

Agreed.

-- 
B.Walter                   BWCT                http://www.bwct.de
ticso at bwct.de                                  info at bwct.de



More information about the freebsd-hackers mailing list