lua-users home
lua-l archive

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

> It's not clear to me exactly what you mean. This may or may not work
> depending on how void handler() is written. I'll be interested in
> seeing the patch, if you ever do it.

I don't intend to update the code, since it was really just an example
of our technique to write C modules.  However, it might be useful
since libevent also has a thread safe API (which we didn't use in the

Something along these lines:

in C-Event.c, global scope:

  /* To use as a private registry key */
  static char levent_key[] = "levent";

in C-Event.c, initialize:

  /* create a place to store the dispatching state */
  lua_pushlightuserdata(L, levent_key);
  lua_newuserdata(L, sizeof(lua_State *));
  lua_rawset(L, LUA_REGISTRYINDEX);

in C-Event.c, dispatch:

  /* Store the dispatching state so the event handler can find it */
  lua_pushlightuserdata(L, levent_key);
  lua_rawget(L, LUA_REGISTRYINDEX);
  *(lua_State **)lua_touserdata(L, -1) = L;
  lua_pop(L, 1);

in C-Event.c, create:

  /* Store a reference to the state that will call dispatch */
  lua_pushlightuserdata(L, levent_key);
  lua_rawget(L, LUA_REGISTRYINDEX);
  ev->L = (lua_State **)lua_touserdata(L, -1);
  lua_pop(L, 1);

in C-Event.c, handler:

  /* Get the dispatching state */
  lua_State *L = *ev->L;