From 4e96ff73864ea29e5ec7730e5e08a20cf42e9b5f Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 6 Nov 2019 00:36:43 +0800 Subject: add io poller --- core/src/xmake/io/poller.c | 65 +++++++++++++++++++++++++++++++++++++++ core/src/xmake/io/poller.h | 41 ++++++++++++++++++++++++ core/src/xmake/io/poller_insert.c | 59 +++++++++++++++++++++++++++++++++++ core/src/xmake/io/poller_modify.c | 59 +++++++++++++++++++++++++++++++++++ core/src/xmake/io/poller_remove.c | 56 +++++++++++++++++++++++++++++++++ core/src/xmake/io/stdfile.c | 2 +- core/src/xmake/makefile | 4 +++ 7 files changed, 285 insertions(+), 1 deletion(-) create mode 100644 core/src/xmake/io/poller.c create mode 100644 core/src/xmake/io/poller.h create mode 100644 core/src/xmake/io/poller_insert.c create mode 100644 core/src/xmake/io/poller_modify.c create mode 100644 core/src/xmake/io/poller_remove.c (limited to 'core/src') diff --git a/core/src/xmake/io/poller.c b/core/src/xmake/io/poller.c new file mode 100644 index 000000000..de514d9ac --- /dev/null +++ b/core/src/xmake/io/poller.c @@ -0,0 +1,65 @@ +/*!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.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "poller" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "poller.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// the singleton type of poller +#define XM_IO_POLLER (TB_SINGLETON_TYPE_USER + 4) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * private implementation + */ +static tb_handle_t xm_io_poller_instance_init(tb_cpointer_t* ppriv) +{ + // init poller + tb_poller_ref_t poller = tb_poller_init(tb_null); + tb_assert_and_check_return_val(poller, tb_null); + + // attach poller to the current thread + tb_poller_attach(poller); + return (tb_handle_t)poller; +} +static tb_void_t xm_io_poller_instance_exit(tb_handle_t poller, tb_cpointer_t priv) +{ + if (poller) tb_poller_exit((tb_poller_ref_t)poller); +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_poller_ref_t xm_io_poller() +{ + return (tb_poller_ref_t)tb_singleton_instance(XM_IO_POLLER, xm_io_poller_instance_init, xm_io_poller_instance_exit, tb_null, tb_null); +} + diff --git a/core/src/xmake/io/poller.h b/core/src/xmake/io/poller.h new file mode 100644 index 000000000..6ce82e0dd --- /dev/null +++ b/core/src/xmake/io/poller.h @@ -0,0 +1,41 @@ +/*!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.h + * + */ +#ifndef XM_IO_POLLER_H +#define XM_IO_POLLER_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* get io poller + * + * @return the io poller + */ +tb_poller_ref_t xm_io_poller(tb_noarg_t); + +#endif + + diff --git a/core/src/xmake/io/poller_insert.c b/core/src/xmake/io/poller_insert.c new file mode 100644 index 000000000..eeb6fd824 --- /dev/null +++ b/core/src/xmake/io/poller_insert.c @@ -0,0 +1,59 @@ +/*!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_insert.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "poller_insert" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "poller.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +// io.poller_insert(sock, events) +tb_int_t xm_io_poller_insert(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is user data? + if (!lua_isuserdata(lua, 1)) + return 0; + + // get socket + tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_check_return_val(sock, 0); + + // get events + tb_size_t events = (tb_size_t)luaL_checknumber(lua, 2); + + // insert events to poller + lua_pushboolean(lua, tb_poller_insert(xm_io_poller(), sock, events, tb_null)); + return 1; +} + diff --git a/core/src/xmake/io/poller_modify.c b/core/src/xmake/io/poller_modify.c new file mode 100644 index 000000000..24a27e241 --- /dev/null +++ b/core/src/xmake/io/poller_modify.c @@ -0,0 +1,59 @@ +/*!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_modify.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "poller_modify" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "poller.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +// io.poller_modify(sock, events) +tb_int_t xm_io_poller_modify(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is user data? + if (!lua_isuserdata(lua, 1)) + return 0; + + // get socket + tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_check_return_val(sock, 0); + + // get events + tb_size_t events = (tb_size_t)luaL_checknumber(lua, 2); + + // modify events in poller + lua_pushboolean(lua, tb_poller_modify(xm_io_poller(), sock, events, tb_null)); + return 1; +} + diff --git a/core/src/xmake/io/poller_remove.c b/core/src/xmake/io/poller_remove.c new file mode 100644 index 000000000..cf6232fce --- /dev/null +++ b/core/src/xmake/io/poller_remove.c @@ -0,0 +1,56 @@ +/*!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_remove.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "poller_remove" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "poller.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +// io.poller_remove(sock) +tb_int_t xm_io_poller_remove(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is user data? + if (!lua_isuserdata(lua, 1)) + return 0; + + // get socket + tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_check_return_val(sock, 0); + + // remove events from poller + lua_pushboolean(lua, tb_poller_remove(xm_io_poller(), sock)); + return 1; +} + diff --git a/core/src/xmake/io/stdfile.c b/core/src/xmake/io/stdfile.c index 8f466fa7a..f91048a46 100644 --- a/core/src/xmake/io/stdfile.c +++ b/core/src/xmake/io/stdfile.c @@ -36,7 +36,7 @@ #endif /* ////////////////////////////////////////////////////////////////////////////////////// - * macors + * macros */ // the singleton type of stdfile diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index e2660128a..960f5ef8b 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -60,6 +60,10 @@ xmake_C_FILES += \ io/filelock_unlock \ io/filelock_trylock \ io/filelock_close \ + io/poller \ + io/poller_insert \ + io/poller_remove \ + io/poller_modify \ io/socket_open \ io/socket_rawfd \ io/socket_wait \ -- cgit v1.3.1