Lua co-routines

http://stackoverflow.com/questions/7206411/lua-co-routines

I'm trying to get an understanding of how I can use co-routines to "pause" a script and wait until some processing is done before resuming.

Perhaps I'm looking at co-routines in the wrong way. But my attempt is structured similar to the example given in this answer.

The loop in loop.lua never reaches a second iteration, and hence never reaches the i == 4condition required to exit the running loop in the C code. If I do not yield in loop.lua, then this code performs as expected.

main.cpp

#include <lua/lua.hpp>

bool running = true;

int lua_finish(lua_State *) {
    running = false;
    printf("lua_finish called\n");
    return 0;
}
int lua_sleep(lua_State *L) {
    printf("lua_sleep called\n");
    return lua_yield(L,0);
}

int main() {
    lua_State* L = lua_open();
    luaL_openlibs(L);

    lua_register(L, "sleep", lua_sleep);
    lua_register(L, "finish", lua_finish);

    luaL_dofile(L, "scripts/init.lua");

    lua_State* cL = lua_newthread(L);
    luaL_dofile(cL, "scripts/loop.lua");

    while (running) {
        int status;
        status = lua_resume(cL,0);
        if (status == LUA_YIELD) {
            printf("loop yielding\n");
        } else {
            running=false; // you can't try to resume if it didn't yield
            // catch any errors below
            if (status == LUA_ERRRUN && lua_isstring(cL, -1)) {
                printf("isstring: %s\n", lua_tostring(cL, -1));
                lua_pop(cL, -1);
            }
        }
    }

    luaL_dofile(L, "scripts/end.lua");
    lua_close(L);
    return 0;
}

loop.lua

print("loop.lua")

local i = 0
while true do
    print("lua_loop iteration")
    sleep()

    i = i + 1
    if i == 4 then
        break
    end
end

finish()

EDIT: Added a bounty, to hopefully get some help on how to accomplish this.

share improve this question
 

2 Answers

up vote 2 down vote accepted
+50

Return code 2 from lua_resume is a LUA_ERRRUN. Check the string on the top of the Lua stack to find the error message.

A similar pattern has worked for me, though I did use coroutine.yield instead of lua_yield and I'm in C rather than C++. I don't see why your solution wouldn't work

On your resume call, it's not clear if you're over simplifying for an example, but I'd make the following changes in your while loop:

int status;
status=lua_resume(cL,0);
if (status == LUA_YIELD) {
  printf("loop yielding\n");
}
else {
  running=false; // you can't try to resume if it didn't yield
  // catch any errors below
  if (status == LUA_ERRRUN && lua_isstring(cL, -1)) {
    printf("isstring: %s\n", lua_tostring(cL, -1));
    lua_pop(cL, -1);
  }
}

Edit 2:

For debugging, add the following before you run your resume. You've got a string getting pushed on the stack somewhere:

int status;
// add this debugging code
if (lua_isstring(cL, -1)) {
  printf("string on stack: %s\n", lua_tostring(cL, -1));
  exit(1);
}
status = lua_resume(cL,0);

Edit 3:

Oh good grief, I can't believe I didn't see this already, you don't want to run luaL_dofile when you're going to yield because you can't yield a pcall directly as best I know, which is what happens in the dofile (5.2 will pass it through, but I think you still need the lua_resume). Switch to this:

luaL_loadfile(cL, "scripts/loop.lua");
share improve this answer
 
 
The error code 2 only occurs when the resume code is successively run (more than one, without another yield). Any other error I get is: "attempt to call a string value". The pop code: if (lua_isstring(cL, -1)) { printf("isstring: %s\n", lua_tostring(cL, -1)); lua_pop(cL, -1); } –  dcousens  Aug 27 '11 at 13:35 
 
"attempt to call a string" means something else is on the stack other than your routine, possibly an error message. And when resume finishes with a return 0, the routine is done and you can't resume it any more, it's no longer on the stack. You can only resume again if you got a yield last time or pushed a new routine on the stack. –  BMitch  Aug 27 '11 at 17:23
 
How can I get the error message, if not through the code above? –  dcousens  Aug 28 '11 at 15:51
1 
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH. luaL_loadfile(cL, "scripts/loop.lua"); It all works now...crys. Thank you! –  dcousens  Sep 1 '11 at 2:51
1 
@Daniel, you're welcome, still can't believe I didn't see that until now. –  BMitch  Sep 1 '11 at 2:54

Last time i was messing with Lua coroutines I ended with such code

const char *program =
"function hello()\n"
"  io.write(\"Hello world 1!\")\n"
"  io.write(\"Hello world 2!\")\n"
"  io.write(\"Hello world 3!\")\n"
"end\n"
"function hate()\n"
"  io.write(\"Hate world 1!\")\n"
"  io.write(\"Hate world 2!\")\n"
"  io.write(\"Hate world 3!\")\n"
"end\n";

const char raw_program[] = 
"function hello()\n"
"  io.write(\"Hello World!\")\n"
"end\n"
"\n"
"cos = {}\n"
"\n"
"for i = 0, 1000, 1 do\n"
"  cos[i] = coroutine.create(hello)\n"
"end\n"
"\n"
"for i = 0, 1000, 1 do\n"
"  coroutine.resume(cos[i])\n"
"end";

int _tmain(int argc, _TCHAR* argv[])
{
    lua_State *L = lua_open();
    lua_State *Lt[1000];
    global_State *g = G(L);

    printf("Lua memory usage after open: %d\n", g->totalbytes);

    luaL_openlibs(L);

    printf("Lua memory usage after openlibs: %d\n", g->totalbytes);

    lua_checkstack(L, 2048);

    printf("Lua memory usage after checkstack: %d\n", g->totalbytes);

    //lua_load(L, my_lua_Reader, (void *)program, "code");
    luaL_loadbuffer(L, program, strlen(program), "line");

    printf("Lua memory usage after loadbuffer: %d\n", g->totalbytes);

    int error = lua_pcall(L, 0, 0, 0);
    if (error) {
        fprintf(stderr, "%s", lua_tostring(L, -1));
        lua_pop(L, 1);
    }

    printf("Lua memory usage after pcall: %d\n", g->totalbytes);

    for (int i = 0; i < 1000; i++) {
        Lt[i] = lua_newthread(L);
        lua_getglobal(Lt[i], i % 2 ? "hello" : "hate");
    }

    printf("Lua memory usage after creating 1000 threads: %d\n", g->totalbytes);

    for (int i = 0; i < 1000; i++) {
        lua_resume(Lt[i], 0);
    }

    printf("Lua memory usage after running 1000 threads: %d\n", g->totalbytes);

    lua_close(L);

    return 0;
}

It seems you could not load file as coroutine? but use function instead And it should be selected to the top of the stack.

lua_getglobal(Lt[i], i % 2 ? "hello" : "hate");
share improve this answer
 
1 
No need to create the coroutine in the Lua script, the lua_newthread call does that in C/C++. I use it myself without any issues. –  BMitch  Aug 30 '11 at 16:29
 
Actually I don't load raw_program. I should cut it from my post. But yes I see. The problem is with resuming coroutine. not starting them. should read original post carefully –  yatagarasu  Aug 30 '11 at 21:08
 
Just saw that you're using lua_newthread in your example. But yeah, OP has issues after yielding the coroutine. –  BMitch  Aug 30 '11 at 21:22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值