changeset 1972:253f8a23e131

threading for swing
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 18 Jun 2025 18:54:38 -0600
parents c691cf51c2a7
children 2f8ab1aa8c57
files src/luan/Luan.java src/luan/modules/Thread.luan src/luan/modules/ThreadLuan.java src/luan/modules/swing/Launcher.luan
diffstat 4 files changed, 37 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
diff -r c691cf51c2a7 -r 253f8a23e131 src/luan/Luan.java
--- a/src/luan/Luan.java	Thu Jun 12 17:05:37 2025 -0600
+++ b/src/luan/Luan.java	Wed Jun 18 18:54:38 2025 -0600
@@ -477,8 +477,12 @@
 		check(luan,2,op,args);
 	}
 
+	public Security getSecurity() {
+		return (Security)registry.get(SECURITY_KEY);
+	}
+
 	private static void check(Luan luan,int i,String op,Object... args) throws LuanException {
-		Security s = (Security)luan.registry.get(SECURITY_KEY);
+		Security s = luan.getSecurity();
 		if( s!=null )
 			s.check(luan,luan.peek(i),op,args);
 	}
diff -r c691cf51c2a7 -r 253f8a23e131 src/luan/modules/Thread.luan
--- a/src/luan/modules/Thread.luan	Thu Jun 12 17:05:37 2025 -0600
+++ b/src/luan/modules/Thread.luan	Wed Jun 18 18:54:38 2025 -0600
@@ -14,6 +14,7 @@
 local Thread = {}
 
 Thread.sleep = ThreadLuan.sleep
+Thread.thread_safe_function = ThreadLuan.thread_safe_function
 
 function Thread.current_thread_name()
 	return ThreadJava.currentThread().getName()
@@ -30,14 +31,9 @@
 end
 Thread.safe = safe
 
-function Thread.run(fn)
+function Thread.run(fn,clean)
 	fn = safe(fn)
-	ThreadLuan.run(fn)
-end
-
-function Thread.fork(fn)
-	logger.error(new_error("Thread.fork is obsolete"))
-	Thread.run(fn)
+	ThreadLuan.run(fn,clean)
 end
 
 function Thread.schedule(fn,options)
diff -r c691cf51c2a7 -r 253f8a23e131 src/luan/modules/ThreadLuan.java
--- a/src/luan/modules/ThreadLuan.java	Thu Jun 12 17:05:37 2025 -0600
+++ b/src/luan/modules/ThreadLuan.java	Wed Jun 18 18:54:38 2025 -0600
@@ -53,12 +53,37 @@
 		};
 	}
 
-	public static void run(Luan luan,LuanFunction fn) throws LuanException {
-		luan = new Luan(luan);
+	private static Luan newLuan(Luan luan,Boolean clean) {
+		if( Boolean.TRUE.equals(clean) ) {
+			Luan.Security security = luan.getSecurity();
+			luan = new Luan();
+			if( security != null )
+				Luan.setSecurity(luan,security);
+			return luan;
+		} else {
+			return new Luan(luan);
+		}
+	}
+
+	public static void run(Luan luan,LuanFunction fn,Boolean clean) throws LuanException {
+		luan = newLuan(luan,clean);
 		LuanMutable.makeImmutable(fn);
 		exec.execute(runnable(luan,fn));
 	}
 
+	public static LuanFunction thread_safe_function(final Luan luan,final LuanFunction fn) {
+		final Thread thread = Thread.currentThread();
+		return new LuanFunction() {
+			@Override public Object call(Luan luan2,Object[] args) throws LuanException {
+				if( thread != Thread.currentThread() )
+					throw new LuanException("called function from another thread");
+				if( luan != luan2 )
+					throw new LuanException("called function from another luan");
+				return fn.call(luan,args);
+			}
+		};
+	}
+
 	private static void cancel(ScheduledFuture sf,String src) {
 		boolean b = sf.cancel(false);
 		if( !sf.isCancelled() )
diff -r c691cf51c2a7 -r 253f8a23e131 src/luan/modules/swing/Launcher.luan
--- a/src/luan/modules/swing/Launcher.luan	Thu Jun 12 17:05:37 2025 -0600
+++ b/src/luan/modules/swing/Launcher.luan	Wed Jun 18 18:54:38 2025 -0600
@@ -27,18 +27,10 @@
 		return
 	end
 
-	local Swing_runner = require "luan:swing/Swing_runner.luan"
-	local swing_run = Swing_runner.run or error()
-	local swing_run_later = Swing_runner.run_later or error()
-
 	Rpc.functions.reopen = function(args)
-		swing_run_later( function()
-			reopen(args)
-		end )
+		reopen(args)
 	end
-	swing_run( function()
-		open(args)
-	end )
+	open(args)
 	Rpc.serve_socket(server_socket,Rpc.server_handler)
 end