Skip to content

Commit

Permalink
Merge pull request #110 from fidlej/topic_lstop
Browse files Browse the repository at this point in the history
Raise error("interrupted!") when torch-qlua is interrupted by ctrl-C.
  • Loading branch information
koraykv committed Feb 12, 2013
2 parents 585a24c + 50a8b3c commit befa6e2
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion exe/qtlua/qlua/qluaapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#if HAVE_SIGNAL_H
# include <signal.h>
#endif

#include <QApplication>
#include <QDateTime>
Expand Down Expand Up @@ -42,7 +45,27 @@
#include "qluaconsole.h"


static lua_State *globalL = NULL;

#ifdef HAVE_SIGNAL_H
// Handling SIGINT the same way as in luajit.c.
static void lstop(lua_State *L, lua_Debug *ar)
{
(void)ar; /* unused arg. */
lua_sethook(L, NULL, 0, 0);
/* Avoid luaL_error -- a C hook doesn't add an extra frame. */
luaL_where(L, 0);
lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1));
lua_error(L);
}

static void laction(int i)
{
signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
terminate process (default action) */
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
}
#endif // HAVE_SIGNAL_H


// ------- private class
Expand Down Expand Up @@ -359,15 +382,20 @@ QLuaApplication::Private::printBadOption(const char *option)
}



int
QLuaApplication::Private::doCall(struct lua_State *L, int nargs)
{
int status;
int base = lua_gettop(L) - nargs;
lua_pushcfunction(L, luaQ_traceback);
lua_insert(L, base);
#if HAVE_SIGNAL_H
signal(SIGINT, laction);
#endif
status = luaQ_pcall(L, nargs, 0, base, theEngine);
#if HAVE_SIGNAL_H
signal(SIGINT, SIG_DFL);
#endif
lua_remove(L, base);
if (status)
lua_gc(L, LUA_GCCOLLECT, 0);
Expand Down Expand Up @@ -467,6 +495,7 @@ QLuaApplication::Private::processArguments(int argc, char **argv)

// Obtain and lock lua
QtLuaLocker lua(theEngine);
globalL = lua;

// Good time to limit access to QtLuaConsole
lua_pushcfunction(lua, hook_qluaconsole);
Expand Down

0 comments on commit befa6e2

Please sign in to comment.