lua-users home
lua-l archive

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


Hi!

In my game-like application I am trying to implement a kind of "synchronous" (i.e. blocking) execution of lua script.

To accomplish this my program does the following steps:

1) C++ part creates new lua thread, loads script into it, and resumes it.
2) Script runs, registers some callbacks and yields. Control goes back to C++.
3) C++ runs "main loop" with message processing.
4) At some point in future C++ triggers a callback mentioned in (2).
5) This callback resumes the thread - and thread proceedes further through its code. From the point of view of lua script that yield'ing was like a blocking operation (and this is what I need).

The problem: when my C++ part calls previously registered lua callback - this callback doesn't run in the context of calling (main) thread! Instead, after calling luabind::call_function(luabind_object_with_callback_function, args) the callback is started already in the context of that thread! (and resuming the same thread becomes a nonsense).

What I have found is that luabind::call_function() pushes onto the lua stack obj.interpreter(). And this results in running that function not in the context of main thread, but in the context of that interpreter (thread).

So, the question is: how can I register a callback inside a thread, then do yield() and have that callback be executed in the context of main thread and resume "my" thread in response?

(also posted at stackoverflow, but nobody answered yet, and I am a litle bit in a hurry)
(http://stackoverflow.com/questions/22021530/calling-lua-function-stored-in-luabindobject-through-lua-state-boundary-reall)

Thanks for your help!