moonbridge

changeset 152:2a5bd37034c6

Removed moonbridge_io.run(...); yielding read and write methods do not yield any values
author jbe
date Fri May 15 16:42:50 2015 +0200 (2015-05-15)
parents 1939ffe973e0
children 08cf8e1865e9
files moonbridge_io.c moonbridge_io.h reference.txt
line diff
     1.1 --- a/moonbridge_io.c	Fri May 08 03:35:32 2015 +0200
     1.2 +++ b/moonbridge_io.c	Fri May 15 16:42:50 2015 +0200
     1.3 @@ -39,9 +39,6 @@
     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 -char moonbr_io_multiblock_udata = 0;
     1.9 -
    1.10  typedef struct {
    1.11    int fd;
    1.12    int issock;
    1.13 @@ -337,10 +334,7 @@
    1.14        lua_replace(L, 3);
    1.15      }
    1.16      lua_pushvalue(L, 2);
    1.17 -    lua_pushlightuserdata(L, &moonbr_io_block_udata);
    1.18 -    lua_pushvalue(L, 1);
    1.19 -    lua_pushliteral(L, "r");
    1.20 -    lua_callk(L, 3, 0, ctx, moonbr_io_read_cont);
    1.21 +    lua_callk(L, 0, 0, ctx, moonbr_io_read_cont);
    1.22    }
    1.23    if (ctx == 1) {
    1.24      lua_pushvalue(L, 5);
    1.25 @@ -402,10 +396,7 @@
    1.26        lua_replace(L, 3);
    1.27      }
    1.28      lua_pushvalue(L, 2);
    1.29 -    lua_pushlightuserdata(L, &moonbr_io_block_udata);
    1.30 -    lua_pushvalue(L, 1);
    1.31 -    lua_pushliteral(L, "r");
    1.32 -    lua_callk(L, 3, 0, ctx, moonbr_io_drain_cont);
    1.33 +    lua_callk(L, 0, 0, ctx, moonbr_io_drain_cont);
    1.34    }
    1.35    lua_pushinteger(L, totallen);
    1.36    lua_pushvalue(L, -2);
    1.37 @@ -583,10 +574,7 @@
    1.38      }
    1.39      lua_pop(L, 2);
    1.40      lua_pushvalue(L, 2);
    1.41 -    lua_pushlightuserdata(L, &moonbr_io_block_udata);
    1.42 -    lua_pushvalue(L, 1);
    1.43 -    lua_pushliteral(L, "w");
    1.44 -    lua_callk(L, 3, 0, 0, moonbr_io_write_cont);
    1.45 +    lua_callk(L, 0, 0, 0, moonbr_io_write_cont);
    1.46    }
    1.47  }
    1.48  
    1.49 @@ -1337,154 +1325,6 @@
    1.50    return 1;
    1.51  }
    1.52  
    1.53 -#define MOONBR_IO_RUN_STACKBASE 7
    1.54 -
    1.55 -#if LUA_VERSION_NUM >= 503
    1.56 -static int moonbr_io_run_cont(lua_State *L, int status, lua_KContext ctx) {
    1.57 -#else
    1.58 -static int moonbr_io_run_cont(lua_State *L) {
    1.59 -#endif
    1.60 -#if !(LUA_VERSION_NUM >= 503)
    1.61 -  int ctx = 0;
    1.62 -  lua_getctx(L, &ctx);
    1.63 -#endif
    1.64 -  while (1) {
    1.65 -    int work_to_do = 0;
    1.66 -    assert(lua_gettop(L) == 7);
    1.67 -    if (lua_isboolean(L, -1) && !lua_toboolean(L, -1)) return 1;
    1.68 -    lua_pop(L, 1);
    1.69 -    if (ctx) {
    1.70 -      for (lua_pushnil(L); lua_next(L, 4); lua_pop(L, 1)) {
    1.71 -        lua_pushvalue(L, -2);
    1.72 -        lua_pushnil(L);
    1.73 -        lua_rawset(L, 4);
    1.74 -      }
    1.75 -      for (lua_pushnil(L); lua_next(L, 5); lua_pop(L, 1)) {
    1.76 -        lua_pushvalue(L, -2);
    1.77 -        lua_pushnil(L);
    1.78 -        lua_rawset(L, 5);
    1.79 -      }
    1.80 -    }
    1.81 -    assert(lua_gettop(L) == 6);
    1.82 -    while (lua_next(L, 1)) {
    1.83 -      void *marker;
    1.84 -      assert(lua_gettop(L) == MOONBR_IO_RUN_STACKBASE);
    1.85 -      while (1) {
    1.86 -        lua_pushvalue(L, -2);
    1.87 -        lua_call(L, 0, LUA_MULTRET);
    1.88 -        if (!lua_checkstack(L, LUA_MINSTACK)) luaL_error(L, "Lua stack exhausted");
    1.89 -        marker = lua_touserdata(L, MOONBR_IO_RUN_STACKBASE+1);
    1.90 -        if (marker == &moonbr_io_block_udata) {
    1.91 -          const char *mode = lua_tostring(L, MOONBR_IO_RUN_STACKBASE+3);
    1.92 -          if (mode && !lua_isnoneornil(L, MOONBR_IO_RUN_STACKBASE+2)) {
    1.93 -            if (strchr(mode, 'r')) {
    1.94 -              lua_pushvalue(L, MOONBR_IO_RUN_STACKBASE+2);
    1.95 -              lua_pushboolean(L, 1);
    1.96 -              lua_rawset(L, 4);
    1.97 -            }
    1.98 -            if (strchr(mode, 'w')) {
    1.99 -              lua_pushvalue(L, MOONBR_IO_RUN_STACKBASE+2);
   1.100 -              lua_pushboolean(L, 1);
   1.101 -              lua_rawset(L, 5);
   1.102 -            }
   1.103 -          }
   1.104 -          work_to_do = 1;
   1.105 -          break;
   1.106 -        } else if (marker == &moonbr_io_multiblock_udata) {
   1.107 -          if (lua_type(L, MOONBR_IO_RUN_STACKBASE+2) == LUA_TTABLE) {
   1.108 -            for (lua_pushnil(L); lua_next(L, MOONBR_IO_RUN_STACKBASE+2); lua_pop(L, 1)) {
   1.109 -              if (lua_toboolean(L, -1)) {
   1.110 -                lua_pushvalue(L, -2);
   1.111 -                lua_pushboolean(L, 1);
   1.112 -                lua_rawset(L, 4);
   1.113 -              }
   1.114 -            }
   1.115 -          }
   1.116 -          if (lua_type(L, MOONBR_IO_RUN_STACKBASE+3) == LUA_TTABLE) {
   1.117 -            for (lua_pushnil(L); lua_next(L, MOONBR_IO_RUN_STACKBASE+3); lua_pop(L, 1)) {
   1.118 -              if (lua_toboolean(L, -1)) {
   1.119 -                lua_pushvalue(L, -2);
   1.120 -                lua_pushboolean(L, 1);
   1.121 -                lua_rawset(L, 5);
   1.122 -              }
   1.123 -            }
   1.124 -          }
   1.125 -          work_to_do = 1;
   1.126 -          break;
   1.127 -        } else if (lua_isboolean(L, MOONBR_IO_RUN_STACKBASE)) {
   1.128 -          lua_pushvalue(L, MOONBR_IO_RUN_STACKBASE-1);
   1.129 -          lua_pushnil(L);
   1.130 -          lua_rawset(L, 1);
   1.131 -          break;
   1.132 -        } else {
   1.133 -          lua_pushvalue(L, MOONBR_IO_RUN_STACKBASE);
   1.134 -          lua_insert(L, MOONBR_IO_RUN_STACKBASE+1);
   1.135 -          lua_call(L, lua_gettop(L)-1-MOONBR_IO_RUN_STACKBASE, 1);
   1.136 -          if (lua_toboolean(L, -1)) {
   1.137 -            lua_pushvalue(L, MOONBR_IO_RUN_STACKBASE-1);
   1.138 -            lua_pushnil(L);
   1.139 -            lua_rawset(L, 1);
   1.140 -            break;
   1.141 -          }
   1.142 -        }
   1.143 -        lua_settop(L, MOONBR_IO_RUN_STACKBASE);
   1.144 -      }
   1.145 -      lua_settop(L, MOONBR_IO_RUN_STACKBASE-1);
   1.146 -    }
   1.147 -    if (!work_to_do) {
   1.148 -      lua_pushboolean(L, 1);
   1.149 -      return 1;
   1.150 -    }
   1.151 -    lua_pushnil(L);
   1.152 -    assert(lua_gettop(L) == 6);
   1.153 -    ctx = 1;
   1.154 -    if (lua_isfunction(L, 2)) {
   1.155 -      lua_pushvalue(L, 2);
   1.156 -      lua_pushlightuserdata(L, &moonbr_io_multiblock_udata);
   1.157 -      lua_pushvalue(L, 4);
   1.158 -      lua_pushvalue(L, 5);
   1.159 -      lua_callk(L, 3, 1, ctx, moonbr_io_run_cont);
   1.160 -    } else {
   1.161 -      lua_pushcfunction(L, moonbr_io_poll);
   1.162 -      lua_pushvalue(L, 4);
   1.163 -      lua_pushvalue(L, 5);
   1.164 -      if (lua_isnil(L, 2)) {
   1.165 -        lua_call(L, 2, 1);
   1.166 -      } else {
   1.167 -        lua_pushvalue(L, 2);
   1.168 -        lua_pushcfunction(L, moonbr_io_timeref);
   1.169 -        lua_pushvalue(L, 3);
   1.170 -        lua_call(L, 1, 1);
   1.171 -        lua_arith(L, LUA_OPSUB);
   1.172 -        lua_call(L, 3, 1);
   1.173 -      }
   1.174 -    }
   1.175 -    assert(lua_gettop(L) == 7);
   1.176 -  }
   1.177 -}
   1.178 -
   1.179 -static int moonbr_io_run(lua_State *L) {
   1.180 -  lua_settop(L, 2);
   1.181 -  luaL_checktype(L, 1, LUA_TTABLE);
   1.182 -  if (lua_isnil(L, 2) || lua_isfunction(L, 2)) {
   1.183 -    lua_pushnil(L);
   1.184 -  } else if (!lua_isnil(L, 2)) {
   1.185 -    luaL_checknumber(L, 2);
   1.186 -    lua_pushcfunction(L, moonbr_io_timeref);
   1.187 -    lua_call(L, 0, 1);
   1.188 -  }
   1.189 -  assert(lua_gettop(L) == 3);
   1.190 -  lua_newtable(L);  /* read_fds at stack position 4 */
   1.191 -  lua_newtable(L);  /* write_fds at stack position 5 */
   1.192 -  lua_pushnil(L);  /* current thread */
   1.193 -  lua_pushnil(L);
   1.194 -#if LUA_VERSION_NUM >= 503
   1.195 -  return moonbr_io_run_cont(L, 0, 0);
   1.196 -#else
   1.197 -  return moonbr_io_run_cont(L);
   1.198 -#endif
   1.199 -}
   1.200 -
   1.201  static const struct luaL_Reg moonbr_io_handle_methods[] = {
   1.202    {"read", moonbr_io_read},
   1.203    {"read_nb", moonbr_io_read_nb},
   1.204 @@ -1536,7 +1376,6 @@
   1.205    {"tcplisten", moonbr_io_tcplisten},
   1.206    {"poll", moonbr_io_poll},
   1.207    {"timeref", moonbr_io_timeref},
   1.208 -  {"run", moonbr_io_run},
   1.209    {NULL, NULL}
   1.210  };
   1.211  
   1.212 @@ -1546,11 +1385,6 @@
   1.213  
   1.214    lua_newtable(L);  // module
   1.215  
   1.216 -  lua_pushlightuserdata(L, &moonbr_io_block_udata);
   1.217 -  lua_setfield(L, -2, "block");
   1.218 -  lua_pushlightuserdata(L, &moonbr_io_multiblock_udata);
   1.219 -  lua_setfield(L, -2, "multiblock");
   1.220 -
   1.221    lua_newtable(L);  // public metatable
   1.222    lua_newtable(L);  // handle methods
   1.223    luaL_setfuncs(L, moonbr_io_handle_methods, 0);
     2.1 --- a/moonbridge_io.h	Fri May 08 03:35:32 2015 +0200
     2.2 +++ b/moonbridge_io.h	Fri May 15 16:42:50 2015 +0200
     2.3 @@ -1,6 +1,3 @@
     2.4 -
     2.5 -char moonbr_io_block_udata;
     2.6 -char moonbr_io_multiblock_udata;
     2.7  
     2.8  void moonbr_io_pushhandle(lua_State *L, int fd);
     2.9  void moonbr_io_closehandle(lua_State *L, int idx, int reset);
     3.1 --- a/reference.txt	Fri May 08 03:35:32 2015 +0200
     3.2 +++ b/reference.txt	Fri May 15 16:42:50 2015 +0200
     3.3 @@ -121,8 +121,8 @@
     3.4  
     3.5  ### socket:flush_call(waitfunc, ...)
     3.6  
     3.7 -Same as socket:flush(...), but calls waitfunc(socket, "w") (in an infinite
     3.8 -loop) as long as the writing is blocked.
     3.9 +Same as socket:flush(...), but calls waitfunc() (in an infinite loop) as long
    3.10 +as the writing is blocked.
    3.11  
    3.12  
    3.13  ### socket:flush_nb(...)
    3.14 @@ -187,9 +187,8 @@
    3.15  
    3.16  ### socket:read_call(waitfunc, maxlen, terminator)
    3.17  
    3.18 -Same as socket:read(maxlen, terminator), but calls waitfunc(
    3.19 -moonbridge_io.block, socket, "r") (in an infinite loop) as long as the reading
    3.20 -is blocked.
    3.21 +Same as socket:read(maxlen, terminator), but calls waitfunc() (in an infinite
    3.22 +loop) as long as the reading is blocked.
    3.23  
    3.24  
    3.25  ### socket:read_nb(maxlen, terminator)
    3.26 @@ -256,8 +255,8 @@
    3.27  
    3.28  ### socket:write_call(waitfunc, ...)
    3.29  
    3.30 -Same as socket:write(...), but calls waitfunc(moonbridge_io.block, socket,
    3.31 -"w") (in an infinite loop) as long as the writing is blocked.
    3.32 +Same as socket:write(...), but calls waitfunc() (in an infinite loop) as long
    3.33 +as the writing is blocked.
    3.34  
    3.35  
    3.36  ### socket:write_nb(...)
    3.37 @@ -293,18 +292,6 @@
    3.38  listed below.
    3.39  
    3.40  
    3.41 -### moonbridge_io.block
    3.42 -
    3.43 -An opaque value (lightuserdata) used by yielding non-blocking I/O functions.
    3.44 -
    3.45 -When socket:read_yield(...) could not read from a socket, it yields the three
    3.46 -values moonbridge_io.block, the socket, and the string "r".
    3.47 -When socket:write_yield(...) could not write to a socket, it yields the three
    3.48 -values moonbridge_io.block, the socket, and the string "w".
    3.49 -
    3.50 -See reference for moonbridge_io.run(...) for further information.
    3.51 -
    3.52 -
    3.53  ### moonbridge_io.localconnect(path)
    3.54  
    3.55  Tries to connect to a local socket (also known as Unix Domain Socket). Returns
    3.56 @@ -352,12 +339,6 @@
    3.57  nil (as first return value) plus an error message (as second return value).
    3.58  
    3.59  
    3.60 -### moonbridge_io.multiblock
    3.61 -
    3.62 -An opaque value (lightuserdata) used by yielding non-blocking I/O functions.
    3.63 -See reference for moonbridge_io.run(...) for further information.
    3.64 -
    3.65 -
    3.66  ### moonbridge_io.poll(input_set, output_set, timeout)
    3.67  
    3.68  This function waits for at least one of the given file descriptors and/or
    3.69 @@ -371,77 +352,6 @@
    3.70  received.
    3.71  
    3.72  
    3.73 -### moonbridge_io.run(function_set, timeout_or_poll_func)
    3.74 -
    3.75 -Executes multiple coroutines (created via coroutine.wrap(...)) in the following
    3.76 -way:
    3.77 -
    3.78 -    function moonbridge_io.run(function_set, timeout_or_poll_func)
    3.79 -      local pairs = function(t) return next, t end  -- raw pairs
    3.80 -      local starttime = moonbridge_io.timeref()
    3.81 -      local read_fds, write_fds = {}, {}
    3.82 -      while true do
    3.83 -        local work_to_do = false
    3.84 -        for func, result_handler in pairs(function_set) do
    3.85 -          ::again::
    3.86 -          local res = table.pack(func())
    3.87 -          if res[1] == moonbridge_io.block then
    3.88 -            if string.match(res[3], "r") then
    3.89 -              read_fds[res[2]] = true
    3.90 -            end
    3.91 -            if string.match(res[3], "w") then
    3.92 -              write_fds[res[2]] = true
    3.93 -            end
    3.94 -            work_to_do = true
    3.95 -          elseif res[1] == moonbridge_io.multiblock then
    3.96 -            for fd, active in pairs(res[2]) do
    3.97 -              if active then read_fds[fd] = true end
    3.98 -            end
    3.99 -            for fd, active in pairs(res[3]) do
   3.100 -              if active then write_fds[fd] = true end
   3.101 -            end
   3.102 -            work_to_do = true
   3.103 -          else
   3.104 -            if
   3.105 -              result_handler == true or
   3.106 -              result_handler(table.unpack(res, 1, res.n))
   3.107 -            then
   3.108 -              function_set[func] = nil  -- task finished
   3.109 -            else
   3.110 -              goto again
   3.111 -            end
   3.112 -          end
   3.113 -        end
   3.114 -        if not work_to_do then
   3.115 -          return true  -- all tasks finished
   3.116 -        end
   3.117 -        if type(timeout_or_poll_func) == "function" then
   3.118 -          local poll_func = timeout_or_poll_func
   3.119 -          if
   3.120 -            poll_func(moonbridge_io.multiblock, read_fds, write_fds) == false
   3.121 -          then
   3.122 -            return false
   3.123 -          end
   3.124 -        elseif type(timeout_or_poll_func) == "number" then
   3.125 -          local timeout = timeout_or_poll_func
   3.126 -          if
   3.127 -            moonbridge_io.poll(
   3.128 -              read_fds,
   3.129 -              write_fds,
   3.130 -              timeout - moonbridge_io.timeref(starttime)
   3.131 -            ) == false
   3.132 -          then
   3.133 -            return false  -- timeout
   3.134 -          end
   3.135 -        else
   3.136 -          moonbridge_io.poll(read_fds, write_fds)
   3.137 -        end
   3.138 -        for fd in pairs(read_fds)  do read_fds[fd]  = nil end
   3.139 -        for fd in pairs(write_fds) do write_fds[fd] = nil end
   3.140 -      end
   3.141 -    end
   3.142 -
   3.143 -
   3.144  ### moonbridge_io.tcpconnect(hostname, port)
   3.145  
   3.146  Tries to open a TCP connection with the given host and TCP port number. Returns

Impressum / About Us