Mercurial Hosting > luan
changeset 1099:a5406f076726
improve Thread.global
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 05 Apr 2017 16:24:02 -0600 |
parents | bae624e455e2 |
children | ad6b3b9fef40 |
files | src/luan/modules/Thread.luan src/luan/modules/ThreadLuan.java |
diffstat | 2 files changed, 23 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
diff -r bae624e455e2 -r a5406f076726 src/luan/modules/Thread.luan --- a/src/luan/modules/Thread.luan Wed Mar 29 19:54:45 2017 -0600 +++ b/src/luan/modules/Thread.luan Wed Apr 05 16:24:02 2017 -0600 @@ -12,7 +12,7 @@ Thread.global = {} -- shared across threads -local map = ThreadLuan.StringMap.new() +local map = ThreadLuan.GlobalMap.new() local global_mt = {} set_metatable(Thread.global,global_mt) @@ -21,16 +21,19 @@ end function global_mt.__new_index(_,key,value) - map.set(key,value) + map.put(key,value) end -function global_mt.get_timeout() +function Thread.get_global_timeout() return map.timeout end -function global_mt.set_timeout(timeout) +function Thread.set_global_timeout(timeout) map.timeout = timeout end +function Thread.global_put(key,value) + return map.put(key,value) +end return Thread
diff -r bae624e455e2 -r a5406f076726 src/luan/modules/ThreadLuan.java --- a/src/luan/modules/ThreadLuan.java Wed Mar 29 19:54:45 2017 -0600 +++ b/src/luan/modules/ThreadLuan.java Wed Apr 05 16:24:02 2017 -0600 @@ -71,14 +71,14 @@ } - public static class StringMap { + public static class GlobalMap { private static class Value { final long time = System.currentTimeMillis(); - final String s; + final Object v; - Value(String s) { - this.s = s; + Value(Object v) { + this.v = v; } } @@ -89,13 +89,21 @@ } }; - public synchronized String get(String key) { + public synchronized Object get(String key) { Value val = map.get(key); - return val==null ? null : val.s; + return val==null ? null : val.v; } - public synchronized void set(String key,String s) { - map.put(key,new Value(s)); + public synchronized Object put(String key,Object v) throws LuanException { + Value val; + if( v == null ) { + val = map.remove(key); + } else { + if( !(v instanceof String || v instanceof Boolean || v instanceof Number) ) + throw new LuanException("can't assign type "+Luan.type(v)+" to Thread.global"); + val = map.put(key,new Value(v)); + } + return val==null ? null : val.v; } } }