X-Git-Url: http://git.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=libs%2Flucid%2Fluasrc%2Flucid.lua;h=d743269fe3fbe2c4c0a2e3f7c3a75dcbddc9a15e;hp=b10365579a043e75108700405be4141212d19d3d;hb=8fcd841aa9af96c8a4a4d3c1a555d2d1ed42332c;hpb=f9263e00c1371eff6ace0252143236d6bf6f2ce2 diff --git a/libs/lucid/luasrc/lucid.lua b/libs/lucid/luasrc/lucid.lua index b10365579a..d743269fe3 100644 --- a/libs/lucid/luasrc/lucid.lua +++ b/libs/lucid/luasrc/lucid.lua @@ -110,16 +110,22 @@ end -- This main function of LuCId will wait for events on given file descriptors. function run() local pollint = tonumber((cursor:get(UCINAME, "main", "pollinterval"))) + local threadlimit = tonumber((cursor:get(UCINAME, "main", "threadlimit"))) while true do local stat, code = nixio.poll(pollt, pollint) if stat and stat > 0 then + local ok = false for _, polle in ipairs(pollt) do if polle.revents ~= 0 and polle.handler then - polle.handler(polle) + ok = ok or polle.handler(polle) end end + if not ok then + -- Avoid high CPU usage if thread limit is reached + nixio.nanosleep(0, 100000000) + end elseif stat == 0 then ifaddrs = nixio.getifaddrs() collectgarbage("collect") @@ -197,6 +203,14 @@ function unregister_tick(cb) return false end +--- Tests whether a given number of processes can be created. +-- @oaram num Processes to be created +-- @return boolean status +function try_process(num) + local threadlimit = tonumber((cursor:get(UCINAME, "main", "threadlimit"))) + return not threadlimit or (threadlimit - tcount) >= (num or 1) +end + --- Create a new child process from a Lua function and assign a destructor. -- @param threadcb main function of the new process -- @param waitcb destructor callback @@ -317,4 +331,4 @@ function daemonize() nixio.dup(devnull, nixio.stderr) return true -end \ No newline at end of file +end