# HG changeset patch # User jbe # Date 1431027178 -7200 # Node ID 6dfe5b424b1839429b160c32f9c03c9f3ce019cb # Parent bd88dfa4f294e27f8cc9871bfd4646f54b6e9402 Proper treatment of zero/negative timeouts to moonbridge_io.poll(...); Make _yield methods yield singleton moonbridge_io.block as first yield value diff -r bd88dfa4f294 -r 6dfe5b424b18 moonbridge_io.c --- a/moonbridge_io.c Sat May 02 16:25:33 2015 +0200 +++ b/moonbridge_io.c Thu May 07 21:32:58 2015 +0200 @@ -37,6 +37,8 @@ #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public" #define MOONBR_IO_LISTENER_MT_REGKEY "moonbridge_io_listener" +char moonbr_io_block_udata = 0; + typedef struct { int fd; int issock; @@ -332,9 +334,10 @@ lua_replace(L, 3); } lua_pushvalue(L, 2); + lua_pushlightuserdata(L, &moonbr_io_block_udata); lua_pushvalue(L, 1); lua_pushliteral(L, "r"); - lua_callk(L, 2, 0, ctx, moonbr_io_read_cont); + lua_callk(L, 3, 0, ctx, moonbr_io_read_cont); } if (ctx == 1) { lua_pushvalue(L, 5); @@ -396,9 +399,10 @@ lua_replace(L, 3); } lua_pushvalue(L, 2); + lua_pushlightuserdata(L, &moonbr_io_block_udata); lua_pushvalue(L, 1); lua_pushliteral(L, "r"); - lua_callk(L, 2, 0, ctx, moonbr_io_drain_cont); + lua_callk(L, 3, 0, ctx, moonbr_io_drain_cont); } lua_pushinteger(L, totallen); lua_pushvalue(L, -2); @@ -576,9 +580,10 @@ } lua_pop(L, 2); lua_pushvalue(L, 2); + lua_pushlightuserdata(L, &moonbr_io_block_udata); lua_pushvalue(L, 1); lua_pushliteral(L, "w"); - lua_callk(L, 2, 0, 0, moonbr_io_write_cont); + lua_callk(L, 3, 0, 0, moonbr_io_write_cont); } } @@ -1285,7 +1290,11 @@ if (!lua_isnoneornil(L, 3)) { lua_Number n; n = lua_tonumberx(L, 3, &isnum); - if (isnum && n>=0 && n<100000000) { + if (isnum && n<0) { + lua_pushboolean(L, 0); + lua_pushliteral(L, "Negative timeout"); + return 2; + } else if (isnum && n>=0 && n<100000000) { timeout.tv_sec = n; timeout.tv_usec = 1e6 * (n - timeout.tv_sec); } else { @@ -1297,7 +1306,7 @@ } if (status == -1) { if (errno == EINTR) { - lua_pushboolean(L, 0); + lua_pushnil(L); lua_pushliteral(L, "Signal received while polling file descriptors"); return 2; } else { @@ -1385,16 +1394,21 @@ lua_newtable(L); // module + lua_pushlightuserdata(L, &moonbr_io_block_udata); + lua_setfield(L, -2, "block"); + lua_newtable(L); // public metatable lua_newtable(L); // handle methods luaL_setfuncs(L, moonbr_io_handle_methods, 0); lua_pushvalue(L, -1); - lua_setfield(L, -4, "prototype_handle"); + lua_setfield(L, -4, "handle_pt"); lua_setfield(L, -2, "__index"); lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY); lua_newtable(L); // handle metatable luaL_setfuncs(L, moonbr_io_handle_metamethods, 0); + lua_pushvalue(L, -1); + lua_setfield(L, -3, "handle_mt"); lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY); lua_newtable(L); // listener metatable @@ -1402,8 +1416,10 @@ lua_newtable(L); // listener methods luaL_setfuncs(L, moonbr_io_listener_methods, 0); lua_pushvalue(L, -1); - lua_setfield(L, -4, "prototype_listener"); + lua_setfield(L, -4, "listener_pt"); lua_setfield(L, -2, "__index"); + lua_pushvalue(L, -1); + lua_setfield(L, -3, "listener_mt"); lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_LISTENER_MT_REGKEY); luaL_setfuncs(L, moonbr_io_module_funcs, 0); diff -r bd88dfa4f294 -r 6dfe5b424b18 moonbridge_io.h --- a/moonbridge_io.h Sat May 02 16:25:33 2015 +0200 +++ b/moonbridge_io.h Thu May 07 21:32:58 2015 +0200 @@ -1,3 +1,5 @@ + +char moonbr_io_block_udata; void moonbr_io_pushhandle(lua_State *L, int fd); void moonbr_io_closehandle(lua_State *L, int idx, int reset);