lua-users home
lua-l archive

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


* steve donovan:

> Is the issue that a __gc implemented in Lua can happen outside normal
> program flow and mess with the state, hence the need for a queue?

I think it's pretty difficult to write reentrant code in Lua.  For
instance, storing something in a table might trigger an allocation,
garbage collection, and the condition leading to the store might no
longer be true.  For instance, the following implementation of
compare-and-swap is not 100% correct (even if no metatables are
involved):

function cas(t, key, test, new)
   local old = t[key]
   if old == test then
      t[key] = new
   end
   return old
end

And if you haven't got compare-and-swap, it's going to be very
difficult to write safe __gc metamethods.