[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Safe way to push strings to a yielded thread
- From: Patrick Donnelly <batrick@...>
- Date: Tue, 18 Dec 2018 16:04:43 -0800
On Tue, Dec 18, 2018 at 5:49 AM Kevin Martin <kev82@khn.org.uk> wrote:
>
> Hi,
>
> I think, from the 5.3 documentation, that lua_pushstring may run a garbage collection (this is how I understand the 'm' in '[-0, +1, m]'). Hence it is possible for this function to error, e.g. in a __gc metamethod.
>
> It's not possible for me to be in a protected environment, so in order to safely push a string, I guess I need to wrap lua_pushstring in a pcall. This is fine normally, but then I am unsure what to do when I am trying to push a string onto a suspended thread, as the manual says that you can only call functions on a state when lua_status(state) == LUA_OK, which is not true for a suspended thread.
See also past threads on this topic:
http://lua-users.org/lists/lua-l/2008-12/msg00012.html
In summary: the manual is silent (i.e. undefined or unspecified) on
whether running (even protected) functions on a yielded thread is
legal. I would interpret the only safe thing to do is to keep track of
which thread is "running" and always use that for pushing state. If
necessary, you can now push a C function and lua_pcall it to put
anything on the stack which may create an error. Calling a function in
this thread on the "running" thread would always be legal.
The running thread would be the one that is most recently calling into
a C function or the main thread if there is no such thread.
> Do I need to create an extra thread initially, just for the purpose of safely pushing strings, and then lua_xmove-ing them to the thread where I want them? Is there a better solution, or have I misunderstood the manual.
Realistically speaking, an alternate thread which you can lua_resume
(from = NULL) with a C function that pushes the necessary arguments
would be safe I think.
--
Patrick Donnelly