lua-users home
lua-l archive

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


is it possible to yield and resume the main thread, even if the
yielding function runs in another coroutine?

(related: )

My Lua script runs in an event-driven environment and I need to
halt it to until an external resource is ready. This works really
nice as long as the state is started with lua_resume and the
C callbacks yield back to the "ground" layer of C.

But when the script calls these functions in a coroutine, they just
yield the script's coroutine and don't stop the execution of Lua.

I tried to yield the main thread directly, but resuming it continues
execution at the wrong point:

print 'A'
cb() --> lua_yield(main thread) / Should pause the script
print 'B'
   print '   X'
   cb() --> lua_yield(main thread) / Should pause the script
   print '   Y'
print 'C'

Prints (Y is missing):

[Resume state]
[Resume state]
[Resume state]

Now my questions are:
A) Can a CFunction call lua_yield with anything other than the
   lua_State passed as argument?
B) If not, can the entire script be yielded/resumed directly in
   standard Lua?

I'm sorry for the naive questions, but I couldn't find a direct
statement in the manual.

Here is the C test program:

// gcc -Wall -Ilua-5.4.0/src main.c lua-5.4.0/src/liblua.a -ldl -lm
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

lua_State* L;

int luacb(lua_State* T)
    return lua_yield(L, 0);

int main()
    L = luaL_newstate();
    lua_register(L, "cb", luacb);
    const char script[] =
        "print 'A'\n"
        "print 'B'\n"
        "   print '   X'\n"
        "   cb()\n"
        "   print '   Y'\n"
        "print 'C'\n";
    if (luaL_loadbufferx (L, script, sizeof script - 1, "script", "t")
!= LUA_OK) {
        printf("[Error] %s\n", lua_tostring(L, -1));
        goto close_state;
    for (;;) {
        printf("[Resume state]\n");
        int nresults = 0;
        int lr = lua_resume(L, NULL, 0, &nresults);
        switch (lr) {
            case LUA_OK:
                lua_pop(L, nresults);
                goto close_state;
            case LUA_YIELD:
                lua_pop(L, nresults);
            default: {
                printf("[Error] %s\n", lua_tostring(L, -1));
                goto close_state;