From 0c913b6043226b3303f66da17c21e589040e36b4 Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Fri, 24 Sep 2021 01:09:14 +0200 Subject: [PATCH] BookInfoManager: Minor fixes & tweaks in `collectSubprocesses` * Don't run multiple collectSubprocesses tasks in parallel. UIManager:scheduleIn doesn't return anything, using that as a boolean trap made no sense. * Make the in-place removal of collected pids use a slightly more common idiom (table.remove in a reverse iteration of said array). --- .../coverbrowser.koplugin/bookinfomanager.lua | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/plugins/coverbrowser.koplugin/bookinfomanager.lua b/plugins/coverbrowser.koplugin/bookinfomanager.lua index 59e1e3792..a925fd416 100644 --- a/plugins/coverbrowser.koplugin/bookinfomanager.lua +++ b/plugins/coverbrowser.koplugin/bookinfomanager.lua @@ -617,25 +617,27 @@ end -- Background extraction management function BookInfoManager:collectSubprocesses() + self.subprocesses_collector = nil + -- We need to regularly watch if a sub-process has terminated by -- calling waitpid() so this process does not become a zombie hanging -- around till we exit. if #self.subprocesses_pids > 0 then - local i = 1 - while i <= #self.subprocesses_pids do -- clean in-place + -- In-place removal, hence the reverse iteration. + for i = #self.subprocesses_pids, 1, -1 do local pid = self.subprocesses_pids[i] if FFIUtil.isSubProcessDone(pid) then table.remove(self.subprocesses_pids, i) -- Prevent has been issued for each bg task spawn, we must allow for each death too. UIManager:allowStandby() - else - i = i + 1 end end if #self.subprocesses_pids > 0 then -- still some pids around, we'll need to collect again - self.subprocesses_collector = UIManager:scheduleIn( - self.subprocesses_collect_interval, function() + self.subprocesses_collector = true + UIManager:scheduleIn( + self.subprocesses_collect_interval, + function() self:collectSubprocesses() end ) @@ -650,7 +652,6 @@ function BookInfoManager:collectSubprocesses() -- we'll collect them next time we're run end else - self.subprocesses_collector = nil if self.delayed_cleanup then self.delayed_cleanup = false -- No more subprocesses = no more crengine indexing, we can remove our @@ -661,7 +662,9 @@ function BookInfoManager:collectSubprocesses() end -- We're done, back to a single core - Device:enableCPUCores(1) + if #self.subprocesses_pids == 0 then + Device:enableCPUCores(1) + end end function BookInfoManager:terminateBackgroundJobs() @@ -722,8 +725,10 @@ function BookInfoManager:extractInBackground(files) -- and fill linux processes table) -- We set a single scheduled action for that if not self.subprocesses_collector then -- there's not one already scheduled - self.subprocesses_collector = UIManager:scheduleIn( - self.subprocesses_collect_interval, function() + self.subprocesses_collector = true + UIManager:scheduleIn( + self.subprocesses_collect_interval, + function() self:collectSubprocesses() end )