libs/core: util.lua optimize get() and set() accessors of threadlocals

This commit is contained in:
Jo-Philipp Wich 2009-07-23 03:21:18 +00:00
parent 3f1393006e
commit a4f6748205

View file

@ -122,21 +122,17 @@ function threadlocal()
local tbl = {} local tbl = {}
local function get(self, key) local function get(self, key)
local c = coroutine.running() local t = rawget(self, coxpt[coroutine.running()] or coroutine.running() or 0)
local thread = coxpt[c] or c or 0 return t and t[key]
if not rawget(self, thread) then
return nil
end
return rawget(self, thread)[key]
end end
local function set(self, key, value) local function set(self, key, value)
local c = coroutine.running() local c = coxpt[coroutine.running()] or coroutine.running() or 0
local thread = coxpt[c] or c or 0 if not rawget(self, c) then
if not rawget(self, thread) then rawset(self, c, { [key] = value })
rawset(self, thread, {}) else
rawget(self, c)[key] = value
end end
rawget(self, thread)[key] = value
end end
setmetatable(tbl, {__index = get, __newindex = set, __mode = "k"}) setmetatable(tbl, {__index = get, __newindex = set, __mode = "k"})