Add dfhack.with_suspend(f[, args...]) that calls f with core suspended.

The lock is properly removed in case of error, which is then propagated.
Just for fun, it also can be yielded from within in a coroutine.
develop
Alexander Gavrilov 2012-04-01 19:38:42 +04:00
parent e3d50b9b04
commit d109b6570b
2 changed files with 33 additions and 2 deletions

@ -1,8 +1,7 @@
PROJECT ( lua CXX ) PROJECT ( lua CXX )
CMAKE_MINIMUM_REQUIRED(VERSION 2.8) CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# TODO: make this RelWithDebInfo only SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-DLUA_USE_APICHECK")
ADD_DEFINITIONS(-DLUA_USE_APICHECK)
IF(WIN32) IF(WIN32)
ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE ) ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE )

@ -337,12 +337,44 @@ static int lua_dfhack_interpreter(lua_State *state)
return 1; return 1;
} }
static int lua_dfhack_with_suspend(lua_State *L)
{
int ctx;
int rv = lua_getctx(L, &ctx);
// Non-resume entry point:
if (rv == LUA_OK)
{
int nargs = lua_gettop(L);
luaL_checktype(L, 1, LUA_TFUNCTION);
Core::getInstance().Suspend();
lua_pushcfunction(L, traceback);
lua_insert(L, 1);
rv = lua_pcallk(L, nargs-1, LUA_MULTRET, 1, 0, lua_dfhack_with_suspend);
}
// Return, resume, or error entry point:
lua_remove(L, 1);
Core::getInstance().Resume();
if (rv != LUA_OK && rv != LUA_YIELD)
lua_error(L);
return lua_gettop(L);
}
static const luaL_Reg dfhack_funcs[] = { static const luaL_Reg dfhack_funcs[] = {
{ "print", lua_dfhack_print }, { "print", lua_dfhack_print },
{ "println", lua_dfhack_println }, { "println", lua_dfhack_println },
{ "printerr", lua_dfhack_printerr }, { "printerr", lua_dfhack_printerr },
{ "traceback", traceback }, { "traceback", traceback },
{ "interpreter", lua_dfhack_interpreter }, { "interpreter", lua_dfhack_interpreter },
{ "with_suspend", lua_dfhack_with_suspend },
{ NULL, NULL } { NULL, NULL }
}; };