Mercurial Hosting > luan
view src/luan/modules/ThreadLuan.java @ 1353:8d95711f6615
replace java() with require "java"
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 20 Mar 2019 17:03:29 -0600 |
parents | e0cf0d108a77 |
children | ae2321a09723 |
line wrap: on
line source
package luan.modules; import java.io.Closeable; import java.util.Arrays; import java.util.Iterator; import java.util.Map; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import luan.Luan; import luan.LuanFunction; import luan.LuanTable; import luan.LuanException; import luan.LuanCloner; import luan.LuanCloneable; public final class ThreadLuan { private static final Executor exec = Executors.newCachedThreadPool(); public static final ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1); public static void fork(LuanFunction fn) { LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); final LuanFunction newFn = (LuanFunction)cloner.get(fn); exec.execute(new Runnable(){public void run() { try { newFn.call(); } catch(LuanException e) { e.printStackTrace(); } }}); } /* public static LuanFunction synchronized_(final Luan luan,final LuanFunction fn) throws LuanException { Utils.checkNotNull(fn); return new LuanFunction() { @Override public Object call(Luan ingored,Object[] args) throws LuanException { synchronized(luan) { return fn.call(luan,args); } } }; } */ public static void schedule(long delay,LuanFunction fn,String repeating) throws LuanException { Luan luan = fn.luan(); LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); final Luan newLuan = (Luan)cloner.clone(luan); final LuanFunction newFn = (LuanFunction)cloner.get(fn); Runnable r = new Runnable(){public void run() { try { newFn.call(); } catch(LuanException e) { e.printStackTrace(); } }}; final ScheduledFuture sf; if( repeating==null ) { sf = scheduler.schedule(r,delay,TimeUnit.MILLISECONDS); } else if( repeating.equals("with_fixed_delay") ) { sf = scheduler.scheduleWithFixedDelay(r,delay,delay,TimeUnit.MILLISECONDS); } else if( repeating.equals("at_fixed_rate") ) { sf = scheduler.scheduleAtFixedRate(r,delay,delay,TimeUnit.MILLISECONDS); } else { throw new LuanException("repeating must be nil or 'with_fixed_delay' or 'at_fixed_rate'"); } final Closeable c = new Closeable(){public void close(){ boolean b = sf.cancel(false); }}; newLuan.registry().put(luan,luan); // prevent gc luan.registry().put(c,c); // prevent gc luan.onClose(c); } /* public static class GlobalMap { private static class Value { final long time = System.currentTimeMillis(); final Object v; Value(Object v) { this.v = v; } } public long timeout = 60000L; // one minute private Map<String,Value> map = new LinkedHashMap<String,Value>() { protected boolean removeEldestEntry(Map.Entry<String,Value> eldest) { return eldest.getValue().time < System.currentTimeMillis() - timeout; } }; public synchronized Object get(String key) { Value val = map.get(key); return val==null ? null : val.v; } public synchronized Object put(String key,Object v) throws LuanException { Value val; if( v == null ) { val = map.remove(key); } else { if( !(v instanceof String || v instanceof Boolean || v instanceof Number) ) throw new LuanException("can't assign type "+Luan.type(v)+" to Thread.global"); val = map.put(key,new Value(v)); } return val==null ? null : val.v; } } */ public static void sleep(long millis) throws InterruptedException { Thread.sleep(millis); } private static class Unsafe { private final String reason; Unsafe(String reason) { this.reason = reason; } } private static Object makeSafe(Luan luan,Object v) throws LuanException { if( v instanceof LuanTable ) { LuanTable tbl = (LuanTable)v; if( tbl.getMetatable() != null ) return new Unsafe("table with metatable"); LuanTable rtn = new LuanTable(luan); for( Map.Entry entry : tbl.rawIterable() ) { Object key = makeSafe( luan, entry.getKey() ); if( key instanceof Unsafe ) return key; Object value = makeSafe( luan, entry.getValue() ); if( value instanceof Unsafe ) return value; rtn.rawPut(key,value); } return rtn; } else if( v instanceof Object[] ) { Object[] a = (Object[])v; for( int i=0; i<a.length; i++ ) { Object obj = makeSafe(luan,a[i]); if( obj instanceof Unsafe ) return obj; a[i] = obj; } return a; } else { if( v instanceof LuanCloneable ) return new Unsafe("type "+Luan.type(v)); return v; } } public static final class Callable { private long expires; private final Luan luan = new Luan(); private final LuanTable fns; Callable(LuanTable fns) { LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); this.fns = (LuanTable)cloner.get(fns); } public synchronized Object call(Luan callerLuan,String fnName,Object... args) throws LuanException { Object obj = makeSafe(luan,args); if( obj instanceof Unsafe ) throw new LuanException("can't pass "+((Unsafe)obj).reason+" to global_callable "+Arrays.asList(args)); args = (Object[])obj; Object f = fns.get(fnName); if( f == null ) throw new LuanException("function '"+fnName+"' not found in global_callable"); if( !(f instanceof LuanFunction) ) throw new LuanException("value of '"+fnName+"' not a function in global_callable"); LuanFunction fn = (LuanFunction)f; Object rtn = fn.call(args); rtn = makeSafe(callerLuan,rtn); if( rtn instanceof Unsafe ) throw new LuanException("can't return "+((Unsafe)rtn).reason+" from global_callable"); return rtn; } } private static Map<String,Callable> callableMap = new HashMap<String,Callable>(); private static void sweep() { long now = System.currentTimeMillis(); for( Iterator<Callable> iter = callableMap.values().iterator(); iter.hasNext(); ) { Callable callable = iter.next(); if( callable.expires < now ) iter.remove(); } } public static synchronized Callable globalCallable(String name,LuanTable fns,long timeout) { Callable callable = callableMap.get(name); if( callable == null ) { sweep(); callable = new Callable(fns); callableMap.put(name,callable); } callable.expires = System.currentTimeMillis() + timeout; return callable; } public static synchronized void removeGlobalCallable(String name) { callableMap.remove(name); } }