Optimize util.threadlocal, Add luci.store as global threadlocal store
[project/luci.git] / libs / core / luasrc / util.lua
index e86d5ec..9747a44 100644 (file)
@@ -114,34 +114,31 @@ end
 -- Scope manipulation routines
 --
 
+local tl_meta = {
+       __mode = "k",
+
+       __index = function(self, key)
+               local t = rawget(self, coxpt[coroutine.running()]
+                or coroutine.running() or 0)
+               return t and t[key]
+       end,
+
+       __newindex = function(self, key, value)
+               local c = coxpt[coroutine.running()] or coroutine.running() or 0
+               if not rawget(self, c) then
+                       rawset(self, c, { [key] = value })
+               else
+                       rawget(self, c)[key] = value
+               end
+       end
+}
+
 --- Create a new or get an already existing thread local store associated with
 -- the current active coroutine. A thread local store is private a table object
 -- whose values can't be accessed from outside of the running coroutine.
 -- @return     Table value representing the corresponding thread local store
-function threadlocal()
-       local tbl = {}
-
-       local function get(self, key)
-               local c = coroutine.running()
-               local thread = coxpt[c] or c or 0
-               if not rawget(self, thread) then
-                       return nil
-               end
-               return rawget(self, thread)[key]
-       end
-
-       local function set(self, key, value)
-               local c = coroutine.running()
-               local thread = coxpt[c] or c or 0
-               if not rawget(self, thread) then
-                       rawset(self, thread, {})
-               end
-               rawget(self, thread)[key] = value
-       end
-
-       setmetatable(tbl, {__index = get, __newindex = set, __mode = "k"})
-
-       return tbl
+function threadlocal(tbl)
+       return setmetatable(tbl or {}, tl_meta)
 end
 
 
@@ -219,7 +216,7 @@ end
 -- @param value        String containing the HTML text
 -- @return     String with HTML tags stripped of
 function striptags(s)
-       return pcdata(s:gsub("</?[A-Za-z][A-Za-z0-9:_%-]*[^>]*>", " "):gsub("%s+", " "))
+       return pcdata(tostring(s):gsub("</?[A-Za-z][A-Za-z0-9:_%-]*[^>]*>", " "):gsub("%s+", " "))
 end
 
 --- Splits given string on a defined separator sequence and return a table
@@ -705,7 +702,7 @@ end
 --- Returns the absolute path to LuCI base directory.
 -- @return             String containing the directory path
 function libpath()
-       return require "luci.fs".dirname(ldebug.__file__)
+       return require "nixio.fs".dirname(ldebug.__file__)
 end
 
 
@@ -772,24 +769,19 @@ function copcall(f, ...)
 end
 
 -- Handle return value of protected call
-function handleReturnValue(err, co, status, ...)
+function handleReturnValue(err, co, status, arg1, arg2, arg3, arg4, arg5)
        if not status then
-               return false, err(debug.traceback(co, (...)), ...)
+               return false, err(debug.traceback(co, arg1), arg1, arg2, arg3, arg4, arg5)
        end
-       if coroutine.status(co) == 'suspended' then
-               return performResume(err, co, coroutine.yield(...))
-       else
-               return true, ...
+
+       if coroutine.status(co) ~= 'suspended' then
+               return true, arg1, arg2, arg3, arg4, arg5
        end
+
+       return performResume(err, co, coroutine.yield(arg1, arg2, arg3, arg4, arg5))
 end
 
 -- Resume execution of protected function call
-function performResume(err, co, ...)
-       if get_memory_limit and get_memory_limit() > 0 and
-          collectgarbage("count") > (get_memory_limit() * 0.8)
-       then
-               collectgarbage("collect")
-       end
-
-       return handleReturnValue(err, co, coroutine.resume(co, ...))
+function performResume(err, co, arg1, arg2, arg3, arg4, arg5)
+       return handleReturnValue(err, co, coroutine.resume(co, arg1, arg2, arg3, arg4, arg5))
 end