diff options
| author | ruki <[email protected]> | 2019-12-13 00:54:52 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-12-12 23:29:13 +0800 |
| commit | 48c503cbca85ecea9e2ef9b5215f2e5c99c8bb24 (patch) | |
| tree | feffd02ceb2904d256f5216a93c09a5f500c9c62 | |
| parent | 880b3b96f8222a027b0387bd1bfbbd793267d5a6 (diff) | |
add poller.support
| -rw-r--r-- | core/src/xmake/io/poller_support.c | 51 | ||||
| -rw-r--r-- | core/src/xmake/machine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 1 | ||||
| -rw-r--r-- | xmake/core/base/poller.lua | 8 | ||||
| -rw-r--r-- | xmake/core/base/scheduler.lua | 34 |
5 files changed, 91 insertions, 5 deletions
diff --git a/core/src/xmake/io/poller_support.c b/core/src/xmake/io/poller_support.c new file mode 100644 index 000000000..b3b349242 --- /dev/null +++ b/core/src/xmake/io/poller_support.c @@ -0,0 +1,51 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015 - 2019, TBOOX Open Source Group. + * + * @author ruki + * @file poller_support.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "poller_support" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "poller.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +// io.poller_support(events) +tb_int_t xm_io_poller_support(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // get events + tb_size_t events = (tb_size_t)luaL_checknumber(lua, 1); + + // support events for poller + lua_pushboolean(lua, tb_poller_support(xm_io_poller(), events)); + return 1; +} + diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index dd606f0b6..4fdc73204 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -128,6 +128,7 @@ tb_int_t xm_io_socket_close(lua_State* lua); tb_int_t xm_io_poller_insert(lua_State* lua); tb_int_t xm_io_poller_modify(lua_State* lua); tb_int_t xm_io_poller_remove(lua_State* lua); +tb_int_t xm_io_poller_support(lua_State* lua); tb_int_t xm_io_poller_wait(lua_State* lua); // the path functions @@ -278,6 +279,7 @@ static luaL_Reg const g_io_functions[] = , { "poller_insert", xm_io_poller_insert } , { "poller_modify", xm_io_poller_modify } , { "poller_remove", xm_io_poller_remove } +, { "poller_support", xm_io_poller_support } , { "poller_wait", xm_io_poller_wait } , { tb_null, tb_null } }; diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 9c2a5d15b..67a340933 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -65,6 +65,7 @@ xmake_C_FILES += \ io/poller_insert \ io/poller_remove \ io/poller_modify \ + io/poller_support \ io/socket_open \ io/socket_rawfd \ io/socket_wait \ diff --git a/xmake/core/base/poller.lua b/xmake/core/base/poller.lua index 222081ae7..48e22e6ab 100644 --- a/xmake/core/base/poller.lua +++ b/xmake/core/base/poller.lua @@ -112,6 +112,14 @@ function poller:_remove_sock(sock) return true end +-- support events? +function poller:support(otype, events) + if otype == poller.OT_SOCK then + return io.poller_support(events) + end + return false, string.format("invalid poller object type(%d)!", otype) +end + -- insert object events to poller function poller:insert(otype, obj, events, udata) if otype == poller.OT_SOCK then diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua index 187b56599..f091c05dd 100644 --- a/xmake/core/base/scheduler.lua +++ b/xmake/core/base/scheduler.lua @@ -29,6 +29,7 @@ local string = require("base/string") local poller = require("base/poller") local timer = require("base/timer") local coroutine = require("base/coroutine") +local bit = require("bit") -- new a coroutine instance function _coroutine.new(name, thread) @@ -96,7 +97,7 @@ end -- get socket events function scheduler:_sockevents(csock) - return self._SOCKEVENTS and self._SOCKEVENTS[csock] or nil + return self._SOCKEVENTS and self._SOCKEVENTS[csock] or 0 end -- set socket events @@ -161,16 +162,39 @@ function scheduler:waitsock(sock, events, timeout) return -1, "we must call waitsock() in coroutine with scheduler!" end + -- enable edge-trigger mode if be supported + if poller:support(poller.OT_SOCK, poller.EV_SOCK_CLEAR) then + events = bit.bor(events, poller.EV_SOCK_CLEAR) + end + -- the socket events callback - local function sockevents_cb(events) + local function sockevents_cb(sockevents) - -- TODO - self:co_resume(running, events) + -- get the previous socket events + local events_prev = self:_sockevents(sock:csock()) + local events_wait = bit.band(events_prev, 0xffff) + local events_save = bit.rshift(events_prev, 16) + + -- TODO is waiting? + if true then + + -- eof for edge trigger? + if bit.band(sockevents, poller.EV_SOCK_EOF) ~= 0 then + -- cache this eof as next recv/send event + sockevents = bit.band(sockevents, bit.bnot(poller.EV_SOCK_EOF)) + events_save = bit.bor(events_save, events_wait) + self:_sockevents_set(sock:csock(), bit.bor(bit.lshift(events_save, 16), events_wait)) + end + self:co_resume(running, (bit.band(sockevents, poller.EV_SOCK_ERROR) ~= 0) and -1 or sockevents) + else + -- cache socket events + -- TODO + end end -- get the previous socket events local events_prev = self:_sockevents(sock:csock()) - if events_prev then + if events_prev ~= 0 then -- TODO print("not impl") else |
