Optimize util.threadlocal, Add luci.store as global threadlocal store
authorSteven Barth <steven@midlink.org>
Mon, 27 Jul 2009 10:27:39 +0000 (10:27 +0000)
committerSteven Barth <steven@midlink.org>
Mon, 27 Jul 2009 10:27:39 +0000 (10:27 +0000)
libs/core/luasrc/store.lua [new file with mode: 0644]
libs/core/luasrc/util.lua

diff --git a/libs/core/luasrc/store.lua b/libs/core/luasrc/store.lua
new file mode 100644 (file)
index 0000000..c33ef07
--- /dev/null
@@ -0,0 +1,16 @@
+--[[
+
+LuCI - Lua Development Framework
+(c) 2009 Steven Barth <steven@midlink.org>
+(c) 2009 Jo-Philipp Wich <xm@leipzig.freifunk.net>
+
+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
+
+]]--
+
+local util = require "luci.util"
+module("luci.store", util.threadlocal)
\ No newline at end of file
index a3ba432..9747a44 100644 (file)
@@ -114,19 +114,16 @@ end
 -- Scope manipulation routines
 --
 
---- 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 tl_meta = {
+       __mode = "k",
 
-       local function get(self, key)
-               local t = rawget(self, coxpt[coroutine.running()] or coroutine.running() or 0)
+       __index = function(self, key)
+               local t = rawget(self, coxpt[coroutine.running()]
+                or coroutine.running() or 0)
                return t and t[key]
-       end
+       end,
 
-       local function set(self, key, value)
+       __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 })
@@ -134,10 +131,14 @@ function threadlocal()
                        rawget(self, c)[key] = value
                end
        end
+}
 
-       setmetatable(tbl, {__index = get, __newindex = set, __mode = "k"})
-
-       return tbl
+--- 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(tbl)
+       return setmetatable(tbl or {}, tl_meta)
 end