diff options
| author | ruki <[email protected]> | 2022-07-23 00:36:35 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-07-23 00:36:35 +0800 |
| commit | fba309ca9f8e3fc8ff2c90f821b6ee501096fdbd (patch) | |
| tree | 0e53adfbccdddddab8dee2879d33b1a7f2d48701 | |
| parent | 5b194189014255d7d6bed74531e4610b45f2fe34 (diff) | |
add fwatcher stub
| -rw-r--r-- | xmake/core/base/fwatcher.lua | 121 | ||||
| -rw-r--r-- | xmake/core/base/poller.lua | 7 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/fwatcher.lua | 22 |
3 files changed, 147 insertions, 3 deletions
diff --git a/xmake/core/base/fwatcher.lua b/xmake/core/base/fwatcher.lua new file mode 100644 index 000000000..da6d4c2a5 --- /dev/null +++ b/xmake/core/base/fwatcher.lua @@ -0,0 +1,121 @@ +--!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-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file fwatcher.lua +-- + +-- define module: fwatcher +local fwatcher = fwatcher or {} +local _instance = _instance or {} + +-- load modules +local string = require("base/string") +local coroutine = require("base/coroutine") +local scheduler = require("base/scheduler") + +-- save original interfaces +fwatcher._open = fwatcher._open or fwatcher.open +fwatcher._wait = fwatcher._wait or fwatcher.wait +fwatcher._close = fwatcher._close or fwatcher.close + +-- get cdata of fwatcher +function _instance:cdata() + local cdata = self._CDATA + if not cdata then + cdata = fwatcher._open() + self._CDATA = cdata + end + return cdata +end + +-- get poller object type, poller.OT_FWATCHER +function _instance:otype() + return 4 +end + +-- wait event +-- +-- @param timeout the timeout +-- +-- @return ok, status +-- +function _instance:wait(timeout) + + -- ensure opened + local ok, errors = self:_ensure_opened() + if not ok then + return -1, errors + end + + -- wait events + local result = -1 + local status_or_errors = nil + if scheduler:co_running() then + result, status_or_errors = scheduler:poller_waitproc(self, timeout or -1) + else + result, status_or_errors = fwatcher._wait(self:cdata(), timeout or -1) + end + if result < 0 and status_or_errors then + status_or_errors = string.format("%s: %s", self, status_or_errors) + end + return result, status_or_errors +end + +-- close instance +function _instance:close() + + -- ensure opened + local ok, errors = self:_ensure_opened() + if not ok then + return false, errors + end + + -- cancel pipe events from the scheduler + if scheduler:co_running() then + ok, errors = scheduler:poller_cancel(self) + if not ok then + return false, errors + end + end + + -- close fwatcher + ok = fwatcher._close(self:cdata()) + if ok then + self._CDATA = nil + end + return ok +end + +-- ensure the fwatcher is opened +function _instance:_ensure_opened() + if not self:cdata() then + return false, string.format("%s: has been closed!", self) + end + return true +end + +setmetatable(_instance, { + __tostring = function() + return "<fwatcher>" + end, + __gc = function(self) + if self._CDATA and fwatcher._close(self._CDATA) then + self._CDATA = nil + end + end + }) +return _instance diff --git a/xmake/core/base/poller.lua b/xmake/core/base/poller.lua index f8e216f26..894e780c2 100644 --- a/xmake/core/base/poller.lua +++ b/xmake/core/base/poller.lua @@ -26,9 +26,10 @@ local io = require("base/io") local string = require("base/string") -- the poller object type, @see tbox/platform/poller.h -poller.OT_SOCK = 1 -poller.OT_PIPE = 2 -poller.OT_PROC = 3 +poller.OT_SOCK = 1 +poller.OT_PIPE = 2 +poller.OT_PROC = 3 +poller.OT_FWATCHER = 4 -- the poller events, @see tbox/platform/poller.h poller.EV_POLLER_RECV = 1 diff --git a/xmake/core/sandbox/modules/import/core/base/fwatcher.lua b/xmake/core/sandbox/modules/import/core/base/fwatcher.lua new file mode 100644 index 000000000..db3dbdbc5 --- /dev/null +++ b/xmake/core/sandbox/modules/import/core/base/fwatcher.lua @@ -0,0 +1,22 @@ +--!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-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file fwatcher.lua +-- + +-- return module +return require("base/fwatcher") |
