diff options
| author | ruki <[email protected]> | 2020-02-03 22:56:27 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-02-07 22:45:56 +0800 |
| commit | 0320b5a0731fca04011d5149a0c7d956033e9140 (patch) | |
| tree | 312d9193ed57a0216c48f6c5863c87cd0335a6e4 | |
| parent | cf9970fddd580f8b9bb8ed25d6d9cfa67a607b69 (diff) | |
fix pipe/socket.wait
| -rw-r--r-- | tests/modules/process/sched_process_pipe.lua | 48 | ||||
| -rw-r--r-- | xmake/core/base/pipe.lua | 6 | ||||
| -rw-r--r-- | xmake/core/base/process.lua | 2 | ||||
| -rw-r--r-- | xmake/core/base/socket.lua | 16 |
4 files changed, 61 insertions, 11 deletions
diff --git a/tests/modules/process/sched_process_pipe.lua b/tests/modules/process/sched_process_pipe.lua new file mode 100644 index 000000000..d3690d619 --- /dev/null +++ b/tests/modules/process/sched_process_pipe.lua @@ -0,0 +1,48 @@ +import("core.base.pipe") +import("core.base.bytes") +import("core.base.scheduler") + +function _session_read_pipe(id, rpipeopt) + local results = {} + local pipe = rpipeopt.pipe + print("%s/%d: read ..", pipe, id) + while not rpipeopt.stop do + local real, data = pipe:read(8192) + if real > 0 then + table.insert(results, bytes(data, 1, real)) + elseif real == 0 then + if pipe:wait(pipe.EV_READ, -1) < 0 then + break + end + else + break + end + end + if #results > 0 then + results = bytes(results) + end + print("%s/%d: read ok, size: %d", pipe, id, results:size()) + if results:size() > 0 then + results:dump() + end + pipe:close() +end + +function _session(id, program, ...) + local rpipe, wpipe = pipe.openpair(10) + local rpipeopt = {pipe = rpipe, stop = false} + scheduler.co_start(_session_read_pipe, i, rpipeopt) + local proc = process.openv(program, table.pack(...), {stdout = wpipe}) + local ok, status = proc:wait(-1) + rpipeopt.stop = true + print("%s/%d: %d, status: %d", proc, id, ok, status) + proc:close() + wpipe:close() +end + +function main(program, ...) + for i = 1, 10 do + scheduler.co_start(_session, i, program, ...) + end + scheduler.runloop() +end diff --git a/xmake/core/base/pipe.lua b/xmake/core/base/pipe.lua index 3252ef5e4..16cdf6be5 100644 --- a/xmake/core/base/pipe.lua +++ b/xmake/core/base/pipe.lua @@ -96,7 +96,7 @@ function _instance:write(data, opt) write = write + real start = start + real elseif real == 0 then - local events, waiterrs = self:wait(pipe.EV_WRITE, opt.timeout or -1) + local events, waiterrs = _instance.wait(self, pipe.EV_WRITE, opt.timeout or -1) if events ~= pipe.EV_WRITE then errors = waiterrs break @@ -148,7 +148,7 @@ function _instance:read(size, opt) table.insert(results, bytes(buff, 1, real)) self:_readbuff_clear() elseif real == 0 then - local events, waiterrs = self:wait(pipe.EV_READ, opt.timeout or -1) + local events, waiterrs = _instance.wait(self, pipe.EV_READ, opt.timeout or -1) if events ~= pipe.EV_READ then data_or_errors = waiterrs break @@ -194,7 +194,7 @@ function _instance:connect(opt) local ok, errors = io.pipe_connect(self:cdata()) if ok == 0 then opt = opt or {} - local events, waiterrs = self:wait(pipe.EV_CONN, opt.timeout or -1) + local events, waiterrs = _instance.wait(self, pipe.EV_CONN, opt.timeout or -1) if events == pipe.EV_CONN then ok, errors = io.pipe_connect(self:cdata()) else diff --git a/xmake/core/base/process.lua b/xmake/core/base/process.lua index aa442f6d7..6f5696786 100644 --- a/xmake/core/base/process.lua +++ b/xmake/core/base/process.lua @@ -147,6 +147,7 @@ end function process.open(command, opt) -- get stdout and pass to subprocess + opt = opt or {} local stdout = opt.stdout if type(stdout) == "string" then opt.outpath = stdout @@ -190,6 +191,7 @@ end function process.openv(shellname, argv, opt) -- get stdout and pass to subprocess + opt = opt or {} local stdout = opt.stdout if type(stdout) == "string" then opt.outpath = stdout diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index 079959538..803ee4f5a 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -162,7 +162,7 @@ function _instance:accept(opt) local sock, errors = io.socket_accept(self:cdata()) if not sock and not errors then opt = opt or {} - local events, waiterrs = self:wait(socket.EV_ACPT, opt.timeout or -1) + local events, waiterrs = _instance.wait(self, socket.EV_ACPT, opt.timeout or -1) if events == socket.EV_ACPT then sock, errors = io.socket_accept(self:cdata()) else @@ -191,7 +191,7 @@ function _instance:connect(addr, port, opt) local ok, errors = io.socket_connect(self:cdata(), addr, port, self:family()) if ok == 0 then opt = opt or {} - local events, waiterrs = self:wait(socket.EV_CONN, opt.timeout or -1) + local events, waiterrs = _instance.wait(self, socket.EV_CONN, opt.timeout or -1) if events == socket.EV_CONN then ok, errors = io.socket_connect(self:cdata(), addr, port, self:family()) else @@ -222,7 +222,7 @@ function _instance:connect_unix(addr, opt) opt = opt or {} local ok, errors = io.socket_connect(self:cdata(), addr, opt.is_abstract, self:family()) if ok == 0 then - local events, waiterrs = self:wait(socket.EV_CONN, opt.timeout or -1) + local events, waiterrs = _instance.wait(self, socket.EV_CONN, opt.timeout or -1) if events == socket.EV_CONN then ok, errors = io.socket_connect(self:cdata(), addr, opt.is_abstract, self:family()) else @@ -275,7 +275,7 @@ function _instance:send(data, opt) start = start + real wait = false elseif real == 0 and not wait then - local events, waiterrs = self:wait(socket.EV_SEND, opt.timeout or -1) + local events, waiterrs = _instance.wait(self, socket.EV_SEND, opt.timeout or -1) if events == socket.EV_SEND then wait = true else @@ -337,7 +337,7 @@ function _instance:sendfile(file, opt) start = start + real wait = false elseif real == 0 and not wait then - local events, waiterrs = self:wait(socket.EV_SEND, opt.timeout or -1) + local events, waiterrs = _instance.wait(self, socket.EV_SEND, opt.timeout or -1) if events == socket.EV_SEND then wait = true else @@ -393,7 +393,7 @@ function _instance:recv(size, opt) table.insert(results, bytes(buff, 1, real)) self:_recvbuff_clear() elseif real == 0 and not wait then - local events, waiterrs = self:wait(socket.EV_RECV, opt.timeout or -1) + local events, waiterrs = _instance.wait(self, socket.EV_RECV, opt.timeout or -1) if events == socket.EV_RECV then wait = true else @@ -456,7 +456,7 @@ function _instance:sendto(data, addr, port, opt) while true do send, errors = io.socket_sendto(self:cdata(), data, addr, port, self:family()) if send == 0 and not wait then - local events, waiterrs = self:wait(socket.EV_SEND, opt.timeout or -1) + local events, waiterrs = _instance.wait(self, socket.EV_SEND, opt.timeout or -1) if events == socket.EV_SEND then wait = true else @@ -511,7 +511,7 @@ function _instance:recvfrom(size, opt) self:_recvbuff_clear() break elseif recv == 0 and not wait then - local events, waiterrs = self:wait(socket.EV_RECV, opt.timeout or -1) + local events, waiterrs = _instance.wait(self, socket.EV_RECV, opt.timeout or -1) if events == socket.EV_RECV then wait = true else |
