changeset 1322:d5739c101bd6

improve Thread
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 06 Feb 2019 01:11:12 -0700
parents 307e76ccd0d6
children 3860ed4b8552
files conv.txt src/luan/modules/Thread.luan src/luan/modules/ThreadLuan.java src/luan/modules/http/tools/Shell.luan
diffstat 4 files changed, 36 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/conv.txt	Tue Feb 05 22:36:55 2019 -0700
+++ b/conv.txt	Wed Feb 06 01:11:12 2019 -0700
@@ -1,3 +1,5 @@
+Thread.global_callable
+Thread.global
 Logging.luanhost_logger
 Logging.init
 authorization user
--- a/src/luan/modules/Thread.luan	Tue Feb 05 22:36:55 2019 -0700
+++ b/src/luan/modules/Thread.luan	Wed Feb 06 01:11:12 2019 -0700
@@ -4,6 +4,7 @@
 local error = Luan.error
 local set_metatable = Luan.set_metatable or error()
 local try = Luan.try or error()
+local Time = require "luan:Time.luan"
 local Logging = require "luan:logging/Logging.luan"
 local logger = Logging.logger "Thread"
 
@@ -34,35 +35,11 @@
 end
 
 
---[[
-Thread.global = {}  -- shared across threads
-local map = ThreadLuan.GlobalMap.new()
-local global_mt = {}
-set_metatable(Thread.global,global_mt)
-
-function global_mt.__index(_,key)
-	return map.get(key)
-end
-
-function global_mt.__new_index(_,key,value)
-	map.put(key,value)
-end
+local forever = Time.period{years=100}
 
-function Thread.get_global_timeout()
-	return map.timeout
-end
-
-function Thread.set_global_timeout(timeout)
-	map.timeout = timeout
-end
-
-function Thread.global_put(key,value)
-	return map.put(key,value)
-end
-]]
-
-function Thread.global_callable(name,timeout,fns)
-	local callable = ThreadLuan.globalCallable(name,timeout,fns)
+function Thread.global_callable(name,fns,timeout)
+	timeout = timeout or forever
+	local callable = ThreadLuan.globalCallable(name,fns,timeout)
 	local mt = {}
 	function mt.__index(_,key)
 		return function(...)
@@ -76,35 +53,31 @@
 
 Thread.remove_global_callable = ThreadLuan.removeGlobalCallable
 
+function Thread.global_map(name,timeout)
+	timeout = timeout or forever
 
-local map = {}
-local fns = {}
+	local map = {}
+	local fns = {}
+
+	function fns.get(key)
+		return map[key]
+	end
 
-function fns.get(key)
-	return map[key]
-end
+	function fns.put(key,value)
+		map[key] = value
+	end
 
-function fns.put(key,value)
-	map[key] = value
+	local callable = ThreadLuan.globalCallable(name,fns,timeout)
+	local mt = {}
+	function mt.__index(_,key)
+		return callable.call("get",key)
+	end
+	function mt.__new_index(_,key,value)
+		return callable.call("put",key,value)
+	end
+	local tbl = {}
+	set_metatable(tbl,mt)
+	return tbl
 end
 
-Thread.global = {}  -- shared across threads
-local global_mt = {}
-set_metatable(Thread.global,global_mt)
-
-function global_mt.__index(_,key)
-	return global_mt.fns.get(key)
-end
-
-function global_mt.__new_index(_,key,value)
-	global_mt.fns.put(key,value)
-end
-
-function Thread.set_global_timeout(timeout)
-	global_mt.fns = Thread.global_callable("Thread.global",timeout,fns)
-end
-
-Thread.set_global_timeout(60000)  -- one minute
-
-
 return Thread
--- a/src/luan/modules/ThreadLuan.java	Tue Feb 05 22:36:55 2019 -0700
+++ b/src/luan/modules/ThreadLuan.java	Wed Feb 06 01:11:12 2019 -0700
@@ -17,6 +17,7 @@
 import luan.LuanTable;
 import luan.LuanException;
 import luan.LuanCloner;
+import luan.LuanCloneable;
 
 
 public final class ThreadLuan {
@@ -155,9 +156,9 @@
 			}
 			return a;
 		} else {
-			if( v==null || v instanceof String || v instanceof Boolean || v instanceof Number )
-				return v;
-			return new Unsafe("type "+Luan.type(v));
+			if( v instanceof LuanCloneable )
+				return new Unsafe("type "+Luan.type(v));
+			return v;
 		}
 	}
 
@@ -201,7 +202,7 @@
 		}
 	}
 
-	public static synchronized Callable globalCallable(LuanState luan,String name,long timeout,LuanTable fns) {
+	public static synchronized Callable globalCallable(LuanState luan,String name,LuanTable fns,long timeout) {
 		Callable callable = callableMap.get(name);
 		if( callable == null ) {
 			sweep();
--- a/src/luan/modules/http/tools/Shell.luan	Tue Feb 05 22:36:55 2019 -0700
+++ b/src/luan/modules/http/tools/Shell.luan	Wed Feb 06 01:11:12 2019 -0700
@@ -21,9 +21,8 @@
 
 local Shell = {}
 
-local forever = Time.period{days=1000000}
 local count = 0
-local new_session = Thread.global_callable("shell.new_session",forever,{next=function()
+local new_session = Thread.global_callable("shell.new_session",{next=function()
 	count = count + 1
 	return to_string(count)
 end}).next
@@ -57,7 +56,7 @@
 local timeout = Time.period{hours=10}
 
 local function get_session(session_id)
-	return Thread.global_callable("shell.session"..session_id,timeout,fns)
+	return Thread.global_callable("shell.session"..session_id,fns,timeout)
 end
 
 function Shell.respond()