lua-users home
lua-l archive

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


On Sun, Jul 5, 2020 at 2:28 AM Stefan <> wrote:
> Since the argument to a C function is the currently running thread,
> this is the only valid way, I think (probably what you mean, too):

Exactly, avoid stashing a lua_state* somewher and using it, work only
with the one passed in and,
in very specialized setups, whith aone obtained from lua_newthread/getthread.

> I've actually used the lua_resume(main_L) / lua_yield(L) quite a bit
> and it generally works (without other coroutines). I never
> suspected that the main thread cannot be used as a coroutine.
> LUA_USE_APICHECK also doesn't detect this.

This happens a lot, some APIs do not check for speed/whatever reason,
so when you do bad things it sometimes works as you expect, classic
undefined behaviour.

> lua_isyieldable seems to be wrong:
> This prints with Lua 5.4:
> lua_isyieldable(MAINL) = 1
> lua_isyieldable(NTHRL) = 1
> but with Lua 5.3:
> lua_isyieldable(MAINL) = 0
> lua_isyieldable(NTHRL) = 0

Yep, it seems it has changed somehow.

I think lua_isyieldable may be meant to be used only with the

> Maybe it is a side-effect of this change:
> Lua 5.4.0  Copyright (C) 1994-2020, PUC-Rio
> > coroutine.isyieldable(coroutine.create(function() end))
> true
> Lua 5.3.5  Copyright (C) 1994-2018, PUC-Rio
> > coroutine.isyieldable(coroutine.create(function() end))
> false

I fear you are using the docs for 5.4 and comparing apples to orange,
isyieldable does not take parameters in 5.3, so it is ignoring the
coroutine you pass and acting on the current thread:

static int luaB_yieldable (lua_State *L) {
  lua_pushboolean(L, lua_isyieldable(L));
  return 1;

static int luaB_yieldable (lua_State *L) {
  lua_State *co = lua_isnone(L, 1) ? L : getco(L);
  lua_pushboolean(L, lua_isyieldable(co));
  return 1;

And I feel isyieldable is not supposed to be used to try to make
"directed yields", but just a check to lookup whether you are going to
hit the "This function can raise an error if it is called from a
thread with a pending C call with no continuation function, or it is
called from a thread that is not running inside a resume (e.g., the
main thread). " condition ( first branch mainly ) when you have a C
function which has alternative approaches ( like an http handler,
doing a request asynchronously when it can and taking a heavier
synchonous approach when not ).

Francisco Olarte.