lua-users home
lua-l archive

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


I wrote a signal handler library for Lua a while ago, and it always
irked me that it was "difficult" to handle signals without corrupting
the Lua stack unless you used lua_sethook() (or so I thought). I've
recently tried making a new state via lua_newthread() and saving it to
the registry so it doesn't get collected. I've thus been able to have
a signal handler do this:

static lua_State *myL = NULL; /* set later */

static void handle(int sig)
{
  lua_pushstring(myL, LUA_SIGNAL);
  lua_gettable(myL, LUA_REGISTRYINDEX);
  lua_pushnumber(myL, sig);
  lua_gettable(myL, -2);

  lua_call(myL, 0, 0);
}

When the library loads, myL is set to the new state returned by
lua_newthread(). I haven't run into any issues yet, but I wonder if
this solution has any problems or side effects I don't see? I'd
appreciate some input :)

Cheers,

-- 
-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."

-Will Durant