summaryrefslogtreecommitdiffhomepage
path: root/libs/lucid/luasrc
diff options
context:
space:
mode:
authorSteven Barth <steven@midlink.org>2009-07-10 13:04:07 +0000
committerSteven Barth <steven@midlink.org>2009-07-10 13:04:07 +0000
commit7372c00dda800ee887ec63a6c3e0c3838da7897d (patch)
treeaefa327c3f94ec2db61b0b0c079e42a81ec559a2 /libs/lucid/luasrc
parent2dcd2eb9b2e3f2e437850f0645ffe9baf72efbdc (diff)
Fix LuCId behaviour if thread-limit is reached
Diffstat (limited to 'libs/lucid/luasrc')
-rw-r--r--libs/lucid/luasrc/lucid.lua35
-rw-r--r--libs/lucid/luasrc/lucid/tcpserver.lua3
2 files changed, 26 insertions, 12 deletions
diff --git a/libs/lucid/luasrc/lucid.lua b/libs/lucid/luasrc/lucid.lua
index 4963ccc30..96611d244 100644
--- a/libs/lucid/luasrc/lucid.lua
+++ b/libs/lucid/luasrc/lucid.lua
@@ -110,22 +110,25 @@ 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"))
+ local threadlimit = tonumber((cursor:get(UCINAME, "main", "threadlimit")))
while true do
- if not threadlimit or tcount < threadlimit then
- local stat, code = nixio.poll(pollt, pollint)
+ local stat, code = nixio.poll(pollt, pollint)
- if stat and stat > 0 then
- for _, polle in ipairs(pollt) do
- if polle.revents ~= 0 and polle.handler then
- polle.handler(polle)
- end
+ if stat and stat > 0 then
+ local ok = false
+ for _, polle in ipairs(pollt) do
+ if polle.revents ~= 0 and polle.handler then
+ ok = ok or polle.handler(polle)
end
- elseif stat == 0 then
- ifaddrs = nixio.getifaddrs()
- collectgarbage("collect")
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")
end
for _, cb in ipairs(tickt) do
@@ -200,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
@@ -320,4 +331,4 @@ function daemonize()
nixio.dup(devnull, nixio.stderr)
return true
-end
+end \ No newline at end of file
diff --git a/libs/lucid/luasrc/lucid/tcpserver.lua b/libs/lucid/luasrc/lucid/tcpserver.lua
index 2897ec8ea..47748e4a3 100644
--- a/libs/lucid/luasrc/lucid/tcpserver.lua
+++ b/libs/lucid/luasrc/lucid/tcpserver.lua
@@ -111,6 +111,9 @@ end
-- @param polle Poll descriptor
-- @return handler process id or nil, error code, error message
function accept(polle)
+ if not lucid.try_process() then
+ return false
+ end
local socket, host, port = polle.fd:accept()
if not socket then
return nixio.syslog("warn", "accept() failed: " .. port)