Changeset 5156

Show
Ignore:
Timestamp:
07/27/09 11:27:39 (4 years ago)
Author:
Cyrus
Message:

Optimize util.threadlocal, Add luci.store as global threadlocal store

Location:
luci/trunk/libs/core/luasrc
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/libs/core/luasrc/util.lua

    r5137 r5156  
    115115-- 
    116116 
    117 --- Create a new or get an already existing thread local store associated with 
    118 -- the current active coroutine. A thread local store is private a table object 
    119 -- whose values can't be accessed from outside of the running coroutine. 
    120 -- @return  Table value representing the corresponding thread local store 
    121 function threadlocal() 
    122     local tbl = {} 
    123  
    124     local function get(self, key) 
    125         local t = rawget(self, coxpt[coroutine.running()] or coroutine.running() or 0) 
     117local tl_meta = { 
     118    __mode = "k", 
     119 
     120    __index = function(self, key) 
     121        local t = rawget(self, coxpt[coroutine.running()] 
     122         or coroutine.running() or 0) 
    126123        return t and t[key] 
    127     end 
    128  
    129     local function set(self, key, value) 
     124    end, 
     125 
     126    __newindex = function(self, key, value) 
    130127        local c = coxpt[coroutine.running()] or coroutine.running() or 0 
    131128        if not rawget(self, c) then 
     
    135132        end 
    136133    end 
    137  
    138     setmetatable(tbl, {__index = get, __newindex = set, __mode = "k"}) 
    139  
    140     return tbl 
     134} 
     135 
     136--- Create a new or get an already existing thread local store associated with 
     137-- the current active coroutine. A thread local store is private a table object 
     138-- whose values can't be accessed from outside of the running coroutine. 
     139-- @return  Table value representing the corresponding thread local store 
     140function threadlocal(tbl) 
     141    return setmetatable(tbl or {}, tl_meta) 
    141142end 
    142143