changeset 1372:431d5bec3ca4

add Thread.new_synchronizer
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 01 Jul 2019 15:37:51 -0600
parents 4c5548a61d4f
children 372906d1d08b
files src/luan/modules/Thread.luan src/luan/modules/ThreadLuan.java
diffstat 2 files changed, 13 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/Thread.luan	Wed Jun 26 19:14:13 2019 -0600
+++ b/src/luan/modules/Thread.luan	Mon Jul 01 15:37:51 2019 -0600
@@ -1,5 +1,6 @@
 require "java"
 local ThreadLuan = require "java:luan.modules.ThreadLuan"
+local ReentrantLock = require "java:java.util.concurrent.locks.ReentrantLock"
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 local set_metatable = Luan.set_metatable or error()
@@ -90,4 +91,14 @@
 end
 
 
+function Thread.new_synchronizer()
+	local lock = ReentrantLock.new()
+	return function(fn)
+		return function(...)
+			return run_in_lock(lock,fn,...)
+		end
+	end
+end
+
+
 return Thread
--- a/src/luan/modules/ThreadLuan.java	Wed Jun 26 19:14:13 2019 -0600
+++ b/src/luan/modules/ThreadLuan.java	Mon Jul 01 15:37:51 2019 -0600
@@ -238,13 +238,13 @@
 
 	public static final ReadWriteLock backupLock = new ReentrantReadWriteLock();
 
-	public static Object runInLock(Lock lock,LuanFunction fn)
+	public static Object runInLock(Lock lock,LuanFunction fn,Object... args)
 		throws LuanException, InterruptedException
 	{
 		if( !lock.tryLock(10,TimeUnit.MINUTES) )
 			throw new LuanException("failed to acquire lock");
 		try {
-			return fn.call();
+			return fn.call(args);
 		} finally {
 			lock.unlock();
 		}