comparison src/luan/modules/ThreadLuan.java @ 1335:e0cf0d108a77

major cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Feb 2019 03:10:45 -0700
parents 25746915a241
children ae2321a09723
comparison
equal deleted inserted replaced
1334:c88b486a9511 1335:e0cf0d108a77
21 21
22 public final class ThreadLuan { 22 public final class ThreadLuan {
23 private static final Executor exec = Executors.newCachedThreadPool(); 23 private static final Executor exec = Executors.newCachedThreadPool();
24 public static final ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1); 24 public static final ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1);
25 25
26 public static void fork(Luan luan,LuanFunction fn) { 26 public static void fork(LuanFunction fn) {
27 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); 27 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
28 final Luan newLuan = (Luan)cloner.clone(luan);
29 final LuanFunction newFn = (LuanFunction)cloner.get(fn); 28 final LuanFunction newFn = (LuanFunction)cloner.get(fn);
30 exec.execute(new Runnable(){public void run() { 29 exec.execute(new Runnable(){public void run() {
31 try { 30 try {
32 newFn.call(newLuan); 31 newFn.call();
33 } catch(LuanException e) { 32 } catch(LuanException e) {
34 e.printStackTrace(); 33 e.printStackTrace();
35 } 34 }
36 }}); 35 }});
37 } 36 }
45 } 44 }
46 } 45 }
47 }; 46 };
48 } 47 }
49 */ 48 */
50 public static void schedule(Luan luan,long delay,LuanFunction fn,String repeating) 49 public static void schedule(long delay,LuanFunction fn,String repeating)
51 throws LuanException 50 throws LuanException
52 { 51 {
52 Luan luan = fn.luan();
53 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); 53 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
54 final Luan newLuan = (Luan)cloner.clone(luan); 54 final Luan newLuan = (Luan)cloner.clone(luan);
55 final LuanFunction newFn = (LuanFunction)cloner.get(fn); 55 final LuanFunction newFn = (LuanFunction)cloner.get(fn);
56 Runnable r = new Runnable(){public void run() { 56 Runnable r = new Runnable(){public void run() {
57 try { 57 try {
58 newFn.call(newLuan); 58 newFn.call();
59 } catch(LuanException e) { 59 } catch(LuanException e) {
60 e.printStackTrace(); 60 e.printStackTrace();
61 } 61 }
62 }}; 62 }};
63 final ScheduledFuture sf; 63 final ScheduledFuture sf;
164 public static final class Callable { 164 public static final class Callable {
165 private long expires; 165 private long expires;
166 private final Luan luan = new Luan(); 166 private final Luan luan = new Luan();
167 private final LuanTable fns; 167 private final LuanTable fns;
168 168
169 Callable(Luan luan,LuanTable fns) { 169 Callable(LuanTable fns) {
170 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); 170 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
171 this.fns = (LuanTable)cloner.get(fns); 171 this.fns = (LuanTable)cloner.get(fns);
172 } 172 }
173 173
174 public synchronized Object call(Luan callerLuan,String fnName,Object... args) throws LuanException { 174 public synchronized Object call(Luan callerLuan,String fnName,Object... args) throws LuanException {
180 if( f == null ) 180 if( f == null )
181 throw new LuanException("function '"+fnName+"' not found in global_callable"); 181 throw new LuanException("function '"+fnName+"' not found in global_callable");
182 if( !(f instanceof LuanFunction) ) 182 if( !(f instanceof LuanFunction) )
183 throw new LuanException("value of '"+fnName+"' not a function in global_callable"); 183 throw new LuanException("value of '"+fnName+"' not a function in global_callable");
184 LuanFunction fn = (LuanFunction)f; 184 LuanFunction fn = (LuanFunction)f;
185 Object rtn = fn.call(luan,args); 185 Object rtn = fn.call(args);
186 rtn = makeSafe(callerLuan,rtn); 186 rtn = makeSafe(callerLuan,rtn);
187 if( rtn instanceof Unsafe ) 187 if( rtn instanceof Unsafe )
188 throw new LuanException("can't return "+((Unsafe)rtn).reason+" from global_callable"); 188 throw new LuanException("can't return "+((Unsafe)rtn).reason+" from global_callable");
189 return rtn; 189 return rtn;
190 } 190 }
199 if( callable.expires < now ) 199 if( callable.expires < now )
200 iter.remove(); 200 iter.remove();
201 } 201 }
202 } 202 }
203 203
204 public static synchronized Callable globalCallable(Luan luan,String name,LuanTable fns,long timeout) { 204 public static synchronized Callable globalCallable(String name,LuanTable fns,long timeout) {
205 Callable callable = callableMap.get(name); 205 Callable callable = callableMap.get(name);
206 if( callable == null ) { 206 if( callable == null ) {
207 sweep(); 207 sweep();
208 callable = new Callable(luan,fns); 208 callable = new Callable(fns);
209 callableMap.put(name,callable); 209 callableMap.put(name,callable);
210 } 210 }
211 callable.expires = System.currentTimeMillis() + timeout; 211 callable.expires = System.currentTimeMillis() + timeout;
212 return callable; 212 return callable;
213 } 213 }