lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Oct 16, 2006 at 02:21:47PM -0200, Roberto Ierusalimschy wrote:
> > Let A and B be two sig_atomic_t values, each initialised to 0.
> > 
> > The reader of hook:
> >   repeat {
> >     read A
> >     read hook
> >     read B
> >   } until (A == B);
> > 
> > The writer of hook:
> >   let C = B + 1
> >   B = C
> >   write hook
> >   A = C
> > 
> > This works as long as the writer is called no more than 2^16 times  
> > between any read A and read B of a reader and as long as memory is  
> > ordered (which it is for all uniprocessor systems).

FWIW, multiprocessor systems are becoming very common at home, with
cheap dual-core processors.  I havn't heard of any of them having out-
of-order accesses, though (being x86-based).

Just a different idea:

 do
   A = read hook
 while A != read hook

If "hook" is volatile, the compiler should actually do the necessary two
reads here.  This doesn't require sig_atomic_t, which is expensive on
platforms without native atomic opcodes (may require disabling interrupts,
etc).

-- 
Glenn Maynard