moonbridge

changeset 146:6dfe5b424b18

Proper treatment of zero/negative timeouts to moonbridge_io.poll(...); Make _yield methods yield singleton moonbridge_io.block as first yield value
author jbe
date Thu May 07 21:32:58 2015 +0200 (2015-05-07)
parents bd88dfa4f294
children 43387f11b096
files moonbridge_io.c moonbridge_io.h
line diff
     1.1 --- a/moonbridge_io.c	Sat May 02 16:25:33 2015 +0200
     1.2 +++ b/moonbridge_io.c	Thu May 07 21:32:58 2015 +0200
     1.3 @@ -37,6 +37,8 @@
     1.4  #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
     1.5  #define MOONBR_IO_LISTENER_MT_REGKEY "moonbridge_io_listener"
     1.6  
     1.7 +char moonbr_io_block_udata = 0;
     1.8 +
     1.9  typedef struct {
    1.10    int fd;
    1.11    int issock;
    1.12 @@ -332,9 +334,10 @@
    1.13        lua_replace(L, 3);
    1.14      }
    1.15      lua_pushvalue(L, 2);
    1.16 +    lua_pushlightuserdata(L, &moonbr_io_block_udata);
    1.17      lua_pushvalue(L, 1);
    1.18      lua_pushliteral(L, "r");
    1.19 -    lua_callk(L, 2, 0, ctx, moonbr_io_read_cont);
    1.20 +    lua_callk(L, 3, 0, ctx, moonbr_io_read_cont);
    1.21    }
    1.22    if (ctx == 1) {
    1.23      lua_pushvalue(L, 5);
    1.24 @@ -396,9 +399,10 @@
    1.25        lua_replace(L, 3);
    1.26      }
    1.27      lua_pushvalue(L, 2);
    1.28 +    lua_pushlightuserdata(L, &moonbr_io_block_udata);
    1.29      lua_pushvalue(L, 1);
    1.30      lua_pushliteral(L, "r");
    1.31 -    lua_callk(L, 2, 0, ctx, moonbr_io_drain_cont);
    1.32 +    lua_callk(L, 3, 0, ctx, moonbr_io_drain_cont);
    1.33    }
    1.34    lua_pushinteger(L, totallen);
    1.35    lua_pushvalue(L, -2);
    1.36 @@ -576,9 +580,10 @@
    1.37      }
    1.38      lua_pop(L, 2);
    1.39      lua_pushvalue(L, 2);
    1.40 +    lua_pushlightuserdata(L, &moonbr_io_block_udata);
    1.41      lua_pushvalue(L, 1);
    1.42      lua_pushliteral(L, "w");
    1.43 -    lua_callk(L, 2, 0, 0, moonbr_io_write_cont);
    1.44 +    lua_callk(L, 3, 0, 0, moonbr_io_write_cont);
    1.45    }
    1.46  }
    1.47  
    1.48 @@ -1285,7 +1290,11 @@
    1.49    if (!lua_isnoneornil(L, 3)) {
    1.50      lua_Number n;
    1.51      n = lua_tonumberx(L, 3, &isnum);
    1.52 -    if (isnum && n>=0 && n<100000000) {
    1.53 +    if (isnum && n<0) {
    1.54 +      lua_pushboolean(L, 0);
    1.55 +      lua_pushliteral(L, "Negative timeout");
    1.56 +      return 2;
    1.57 +    } else if (isnum && n>=0 && n<100000000) {
    1.58        timeout.tv_sec = n;
    1.59        timeout.tv_usec = 1e6 * (n - timeout.tv_sec);
    1.60      } else {
    1.61 @@ -1297,7 +1306,7 @@
    1.62    }
    1.63    if (status == -1) {
    1.64      if (errno == EINTR) {
    1.65 -      lua_pushboolean(L, 0);
    1.66 +      lua_pushnil(L);
    1.67        lua_pushliteral(L, "Signal received while polling file descriptors");
    1.68        return 2;
    1.69      } else {
    1.70 @@ -1385,16 +1394,21 @@
    1.71  
    1.72    lua_newtable(L);  // module
    1.73  
    1.74 +  lua_pushlightuserdata(L, &moonbr_io_block_udata);
    1.75 +  lua_setfield(L, -2, "block");
    1.76 +
    1.77    lua_newtable(L);  // public metatable
    1.78    lua_newtable(L);  // handle methods
    1.79    luaL_setfuncs(L, moonbr_io_handle_methods, 0);
    1.80    lua_pushvalue(L, -1);
    1.81 -  lua_setfield(L, -4, "prototype_handle");
    1.82 +  lua_setfield(L, -4, "handle_pt");
    1.83    lua_setfield(L, -2, "__index");
    1.84    lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
    1.85  
    1.86    lua_newtable(L);  // handle metatable
    1.87    luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
    1.88 +  lua_pushvalue(L, -1);
    1.89 +  lua_setfield(L, -3, "handle_mt");
    1.90    lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
    1.91  
    1.92    lua_newtable(L);  // listener metatable
    1.93 @@ -1402,8 +1416,10 @@
    1.94    lua_newtable(L);  // listener methods
    1.95    luaL_setfuncs(L, moonbr_io_listener_methods, 0);
    1.96    lua_pushvalue(L, -1);
    1.97 -  lua_setfield(L, -4, "prototype_listener");
    1.98 +  lua_setfield(L, -4, "listener_pt");
    1.99    lua_setfield(L, -2, "__index");
   1.100 +  lua_pushvalue(L, -1);
   1.101 +  lua_setfield(L, -3, "listener_mt");
   1.102    lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_LISTENER_MT_REGKEY);
   1.103  
   1.104    luaL_setfuncs(L, moonbr_io_module_funcs, 0);
     2.1 --- a/moonbridge_io.h	Sat May 02 16:25:33 2015 +0200
     2.2 +++ b/moonbridge_io.h	Thu May 07 21:32:58 2015 +0200
     2.3 @@ -1,3 +1,5 @@
     2.4 +
     2.5 +char moonbr_io_block_udata;
     2.6  
     2.7  void moonbr_io_pushhandle(lua_State *L, int fd);
     2.8  void moonbr_io_closehandle(lua_State *L, int idx, int reset);

Impressum / About Us