summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-12-14 10:03:51 +0800
committerruki <[email protected]>2019-12-14 10:03:51 +0800
commit22f2823a1b6cf40df62790e04eb770c2a6659daf (patch)
tree0e7f2e603a04643064ff84a819708ec1c3ee49ff
parent13f84aad1c1a3b5af36160f108839eae8407c555 (diff)
cancel socket from scheduler
-rw-r--r--xmake/core/base/scheduler.lua19
-rw-r--r--xmake/core/base/socket.lua10
2 files changed, 27 insertions, 2 deletions
diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua
index 5c916bdce..6aafead43 100644
--- a/xmake/core/base/scheduler.lua
+++ b/xmake/core/base/scheduler.lua
@@ -166,7 +166,7 @@ function scheduler:co_count()
end
-- wait socket events
-function scheduler:waitsock(sock, events, timeout)
+function scheduler:sock_wait(sock, events, timeout)
-- get the running coroutine
local running = self:co_running()
@@ -257,6 +257,23 @@ function scheduler:waitsock(sock, events, timeout)
return self:co_suspend()
end
+-- cancel socket events
+function scheduler:sock_cancel(sock)
+
+ -- get the previous socket events
+ local events_prev = self:_sockevents(sock:csock())
+ if events_prev ~= 0 then
+
+ -- remove the waiting socket from the poller
+ local ok, errors = poller:remove(poller.OT_SOCK, sock)
+ if not ok then
+ return false, errors
+ end
+ self:_sockevents_set(sock:csock(), 0)
+ end
+ return true
+end
+
-- sleep some times (ms)
function scheduler:sleep(ms)
diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua
index 7497d7b8a..ffae67082 100644
--- a/xmake/core/base/socket.lua
+++ b/xmake/core/base/socket.lua
@@ -482,7 +482,7 @@ function _instance:wait(events, timeout)
local result = -1
local errors = nil
if scheduler:co_running() then
- result, errors = scheduler:waitsock(self, events, timeout or -1)
+ result, errors = scheduler:sock_wait(self, events, timeout or -1)
else
result, errors = io.socket_wait(self:csock(), events, timeout or -1)
end
@@ -501,6 +501,14 @@ function _instance:close()
return false, errors
end
+ -- cancel socket events from the scheduler
+ if scheduler:co_running() then
+ ok, errors = scheduler:sock_cancel(self)
+ if not ok then
+ return false, errors
+ end
+ end
+
-- close it
ok = io.socket_close(self:csock())
if ok then