diff options
author | Steven Barth <steven@midlink.org> | 2009-07-27 10:27:39 +0000 |
---|---|---|
committer | Steven Barth <steven@midlink.org> | 2009-07-27 10:27:39 +0000 |
commit | ce98fdd80f549ab72c25c5cd799d4946433e4e2c (patch) | |
tree | bc486cc070bd29da74c09519f9afda32470898e2 /libs/core/luasrc/util.lua | |
parent | 7b5cdb36b5c8d4e412eeda80f8a04a2208a329bf (diff) |
Optimize util.threadlocal, Add luci.store as global threadlocal store
Diffstat (limited to 'libs/core/luasrc/util.lua')
-rw-r--r-- | libs/core/luasrc/util.lua | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/libs/core/luasrc/util.lua b/libs/core/luasrc/util.lua index a3ba43246..9747a4486 100644 --- a/libs/core/luasrc/util.lua +++ b/libs/core/luasrc/util.lua @@ -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 |