diff 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
line wrap: on
line diff
--- a/src/luan/modules/Thread.luan	Sun Jan 31 16:04:39 2021 -0700
+++ b/src/luan/modules/Thread.luan	Mon Feb 08 14:16:19 2021 -0700
@@ -34,12 +34,20 @@
 	ThreadLuan.schedule(fn,options)
 end
 
+function Thread.schedule_closure(init_fn,options)
+	local function safe_init()
+		return safe(init_fn())
+	end
+	options = options or {}
+	ThreadLuan.schedule_closure(safe_init,options)
+end
+
 
 local forever = Time.period{years=100}
 
-function Thread.global_callable(name,fns,timeout)
+function Thread.global_callable(name,init_fn,timeout)
 	timeout = timeout or forever
-	local callable = ThreadLuan.globalCallable(name,fns,timeout)
+	local callable = ThreadLuan.globalCallable(name,init_fn,timeout)
 	local mt = {}
 	function mt.__index(_,key)
 		return function(...)
@@ -54,27 +62,21 @@
 Thread.remove_global_callable = ThreadLuan.removeGlobalCallable
 
 function Thread.global_map(name,timeout)
-	timeout = timeout or forever
+	local function init()
+		local map = {}
+		local fns = {}
 
-	local map = {}
-	local fns = {}
-
-	function fns.get(key)
-		return map[key]
-	end
+		function fns.__index(_,key)
+			return map[key]
+		end
 
-	function fns.put(key,value)
-		map[key] = value
-	end
+		function fns.__new_index(_,key,value)
+			map[key] = value
+		end
 
-	local callable = ThreadLuan.globalCallable(name,fns,timeout)
-	local mt = {}
-	function mt.__index(_,key)
-		return callable.call("get",key)
+		return fns
 	end
-	function mt.__new_index(_,key,value)
-		return callable.call("put",key,value)
-	end
+	local mt = Thread.global_callable(name,init,timeout)
 	local tbl = {}
 	set_metatable(tbl,mt)
 	return tbl
@@ -94,15 +96,4 @@
 end
 
 
--- remove
-function Thread.new_synchronizer()
-	local lock = ReentrantLock.new()
-	return function(fn)
-		return function(...)
-			return run_in_lock(lock,default_time_out,fn,...)
-		end
-	end
-end
-
-
 return Thread