diff src/luan/modules/ThreadLuan.java @ 1972:253f8a23e131

threading for swing
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 18 Jun 2025 18:54:38 -0600
parents 31f006c64782
children
line wrap: on
line diff
--- 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() )