Mercurial Hosting > luan
comparison src/luan/modules/Thread.luan @ 1578:c922446f53aa
immutable threading
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 08 Feb 2021 14:16:19 -0700 |
parents | d9a5405a3102 |
children | 915cb538e2a3 |
comparison
equal
deleted
inserted
replaced
1577:60e5c324adf9 | 1578:c922446f53aa |
---|---|
32 fn = safe(fn) | 32 fn = safe(fn) |
33 options = options or {} | 33 options = options or {} |
34 ThreadLuan.schedule(fn,options) | 34 ThreadLuan.schedule(fn,options) |
35 end | 35 end |
36 | 36 |
37 function Thread.schedule_closure(init_fn,options) | |
38 local function safe_init() | |
39 return safe(init_fn()) | |
40 end | |
41 options = options or {} | |
42 ThreadLuan.schedule_closure(safe_init,options) | |
43 end | |
44 | |
37 | 45 |
38 local forever = Time.period{years=100} | 46 local forever = Time.period{years=100} |
39 | 47 |
40 function Thread.global_callable(name,fns,timeout) | 48 function Thread.global_callable(name,init_fn,timeout) |
41 timeout = timeout or forever | 49 timeout = timeout or forever |
42 local callable = ThreadLuan.globalCallable(name,fns,timeout) | 50 local callable = ThreadLuan.globalCallable(name,init_fn,timeout) |
43 local mt = {} | 51 local mt = {} |
44 function mt.__index(_,key) | 52 function mt.__index(_,key) |
45 return function(...) | 53 return function(...) |
46 return callable.call(key,...) | 54 return callable.call(key,...) |
47 end | 55 end |
52 end | 60 end |
53 | 61 |
54 Thread.remove_global_callable = ThreadLuan.removeGlobalCallable | 62 Thread.remove_global_callable = ThreadLuan.removeGlobalCallable |
55 | 63 |
56 function Thread.global_map(name,timeout) | 64 function Thread.global_map(name,timeout) |
57 timeout = timeout or forever | 65 local function init() |
66 local map = {} | |
67 local fns = {} | |
58 | 68 |
59 local map = {} | 69 function fns.__index(_,key) |
60 local fns = {} | 70 return map[key] |
71 end | |
61 | 72 |
62 function fns.get(key) | 73 function fns.__new_index(_,key,value) |
63 return map[key] | 74 map[key] = value |
75 end | |
76 | |
77 return fns | |
64 end | 78 end |
65 | 79 local mt = Thread.global_callable(name,init,timeout) |
66 function fns.put(key,value) | |
67 map[key] = value | |
68 end | |
69 | |
70 local callable = ThreadLuan.globalCallable(name,fns,timeout) | |
71 local mt = {} | |
72 function mt.__index(_,key) | |
73 return callable.call("get",key) | |
74 end | |
75 function mt.__new_index(_,key,value) | |
76 return callable.call("put",key,value) | |
77 end | |
78 local tbl = {} | 80 local tbl = {} |
79 set_metatable(tbl,mt) | 81 set_metatable(tbl,mt) |
80 return tbl | 82 return tbl |
81 end | 83 end |
82 | 84 |
92 return run_in_lock(lock,time_out,fn,...) | 94 return run_in_lock(lock,time_out,fn,...) |
93 end | 95 end |
94 end | 96 end |
95 | 97 |
96 | 98 |
97 -- remove | |
98 function Thread.new_synchronizer() | |
99 local lock = ReentrantLock.new() | |
100 return function(fn) | |
101 return function(...) | |
102 return run_in_lock(lock,default_time_out,fn,...) | |
103 end | |
104 end | |
105 end | |
106 | |
107 | |
108 return Thread | 99 return Thread |