lua-users home
lua-l archive

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


On Tue, Oct 10, 2006 at 10:46:49AM +0100, David Jones wrote:
> I don't see how lua_sethook is safe when used in two different threads.
> 
> L->hook and L->base are modified in dependent ways, for example, and  
> no attempt is made to synchronise the updates.  What if one is using  
> an architecture with relaxed memory order?

Even if writes are wrongly ordered, it seems safe for the "hook as
cancel" case.  Even if L->hookmask is set first, while L->hook is
still NULL, it'll just cause a spurious hook where hook is still NULL
(the old value)--a no-op--and the hook count of 1 will cause it to
be called again immediately.

(Other cases--calling lua_sethook from multiple threads, or calling it
from a thread while the hook is already enabled, are somewhat less safe,
though will usually at worst cause more spurious calls.)

But one thing I'm not sure about: hookmask is a byte, not a word.  It's
in the same 32-bit word, on x86 Linux, as L->allowhook.  Is it atomic
on that arch if one thread modifies allowhook while another modifies
hookmask?  L->allowhook is changed constantly, it seems.  Nonatomicity
could probably cause much bigger problems.

(Except in particularly odd cases, such as where modifying allowhook
requires reading the whole word surrounding it into a register,
modifying the register and writing it all back, I can't think of any
problems from a signal handler where the program is suspended, though.)

-- 
Glenn Maynard