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 | |
parent | 7b5cdb36b5c8d4e412eeda80f8a04a2208a329bf (diff) |
Optimize util.threadlocal, Add luci.store as global threadlocal store
-rw-r--r-- | libs/core/luasrc/store.lua | 16 | ||||
-rw-r--r-- | libs/core/luasrc/util.lua | 27 |
2 files changed, 30 insertions, 13 deletions
diff --git a/libs/core/luasrc/store.lua b/libs/core/luasrc/store.lua new file mode 100644 index 0000000000..c33ef07e11 --- /dev/null +++ b/libs/core/luasrc/store.lua @@ -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 diff --git a/libs/core/luasrc/util.lua b/libs/core/luasrc/util.lua index a3ba432468..9747a44862 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 |