Mercurial Hosting > luan
comparison src/luan/modules/ThreadLuan.java @ 1563:8fbcc4747091
remove LuanFunction.luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 09 Nov 2020 01:37:57 -0700 |
parents | b89212fd04b5 |
children | c922446f53aa |
comparison
equal
deleted
inserted
replaced
1562:b89212fd04b5 | 1563:8fbcc4747091 |
---|---|
28 private static final Logger logger = LoggerFactory.getLogger(ThreadLuan.class); | 28 private static final Logger logger = LoggerFactory.getLogger(ThreadLuan.class); |
29 | 29 |
30 private static final Executor exec = Executors.newCachedThreadPool(); | 30 private static final Executor exec = Executors.newCachedThreadPool(); |
31 public static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | 31 public static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); |
32 | 32 |
33 private static Runnable runnable(final LuanFunction fn) { | 33 private static Runnable runnable(final Luan luan,final LuanFunction fn) { |
34 return new Runnable() { | 34 return new Runnable() { |
35 public synchronized void run() { | 35 public synchronized void run() { |
36 LuanLogger.startThreadLogging(fn.luan()); | 36 LuanLogger.startThreadLogging(luan); |
37 try { | 37 try { |
38 fn.call(); | 38 fn.call(luan); |
39 } catch(LuanException e) { | 39 } catch(LuanException e) { |
40 e.printStackTrace(); | 40 e.printStackTrace(); |
41 } finally { | 41 } finally { |
42 LuanLogger.endThreadLogging(); | 42 LuanLogger.endThreadLogging(); |
43 } | 43 } |
44 } | 44 } |
45 }; | 45 }; |
46 } | 46 } |
47 | 47 |
48 public static void fork(LuanFunction fn) { | 48 public static void fork(Luan luan,LuanFunction fn) { |
49 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); | 49 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); |
50 final LuanFunction newFn = (LuanFunction)cloner.get(fn); | 50 luan = (Luan)cloner.get(luan); |
51 exec.execute(runnable(newFn)); | 51 fn = (LuanFunction)cloner.get(fn); |
52 exec.execute(runnable(luan,fn)); | |
52 } | 53 } |
53 | 54 |
54 private static final Map<String,ScheduledFuture> scheduleds = new WeakCacheMap<String,ScheduledFuture>(); | 55 private static final Map<String,ScheduledFuture> scheduleds = new WeakCacheMap<String,ScheduledFuture>(); |
55 | 56 |
56 private static void cancel(ScheduledFuture sf,String src) { | 57 private static void cancel(ScheduledFuture sf,String src) { |
57 boolean b = sf.cancel(false); | 58 boolean b = sf.cancel(false); |
58 if( !sf.isCancelled() ) | 59 if( !sf.isCancelled() ) |
59 logger.error(src+" cancel="+b+" isCancelled="+sf.isCancelled()+" isDone="+sf.isDone()+" "+sf); | 60 logger.error(src+" cancel="+b+" isCancelled="+sf.isCancelled()+" isDone="+sf.isDone()+" "+sf); |
60 } | 61 } |
61 | 62 |
62 public static synchronized void schedule(LuanFunction fn,LuanTable options) | 63 public static synchronized void schedule(Luan luan,LuanFunction fn,LuanTable options) |
63 throws LuanException | 64 throws LuanException |
64 { | 65 { |
65 options = new LuanTable(options); | 66 options = new LuanTable(options); |
66 Number delay = Utils.removeNumber(options,"delay"); | 67 Number delay = Utils.removeNumber(options,"delay"); |
67 Number repeatingDelay = Utils.removeNumber(options,"repeating_delay"); | 68 Number repeatingDelay = Utils.removeNumber(options,"repeating_delay"); |
74 if( id != null ) { | 75 if( id != null ) { |
75 ScheduledFuture sf = scheduleds.remove(id); | 76 ScheduledFuture sf = scheduleds.remove(id); |
76 if( sf != null ) | 77 if( sf != null ) |
77 cancel(sf,"id "+id); | 78 cancel(sf,"id "+id); |
78 } | 79 } |
79 Luan luan = fn.luan(); | |
80 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); | 80 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); |
81 final Luan newLuan = (Luan)cloner.clone(luan); | 81 final Luan newLuan = (Luan)cloner.clone(luan); |
82 final LuanFunction newFn = (LuanFunction)cloner.get(fn); | 82 final LuanFunction newFn = (LuanFunction)cloner.get(fn); |
83 final Runnable r = runnable(newFn); | 83 final Runnable r = runnable(newLuan,newFn); |
84 final ScheduledFuture sf; | 84 final ScheduledFuture sf; |
85 if( repeatingDelay != null ) { | 85 if( repeatingDelay != null ) { |
86 if( delay==null ) | 86 if( delay==null ) |
87 delay = repeatingDelay; | 87 delay = repeatingDelay; |
88 sf = scheduler.scheduleWithFixedDelay(r,delay.longValue(),repeatingDelay.longValue(),TimeUnit.MILLISECONDS); | 88 sf = scheduler.scheduleWithFixedDelay(r,delay.longValue(),repeatingDelay.longValue(),TimeUnit.MILLISECONDS); |
208 if( f == null ) | 208 if( f == null ) |
209 throw new LuanException("function '"+fnName+"' not found in global_callable"); | 209 throw new LuanException("function '"+fnName+"' not found in global_callable"); |
210 if( !(f instanceof LuanFunction) ) | 210 if( !(f instanceof LuanFunction) ) |
211 throw new LuanException("value of '"+fnName+"' not a function in global_callable"); | 211 throw new LuanException("value of '"+fnName+"' not a function in global_callable"); |
212 LuanFunction fn = (LuanFunction)f; | 212 LuanFunction fn = (LuanFunction)f; |
213 Object rtn = fn.call(args); | 213 Object rtn = fn.call(luan,args); |
214 rtn = makeSafe(rtn); | 214 rtn = makeSafe(rtn); |
215 if( rtn instanceof Unsafe ) | 215 if( rtn instanceof Unsafe ) |
216 throw new LuanException("can't return "+((Unsafe)rtn).reason+" from global_callable"); | 216 throw new LuanException("can't return "+((Unsafe)rtn).reason+" from global_callable"); |
217 return rtn; | 217 return rtn; |
218 } | 218 } |
243 public static synchronized void removeGlobalCallable(String name) { | 243 public static synchronized void removeGlobalCallable(String name) { |
244 callableMap.remove(name); | 244 callableMap.remove(name); |
245 } | 245 } |
246 | 246 |
247 | 247 |
248 public static Object runInLock(Lock lock,long timeout,LuanFunction fn,Object... args) | 248 public static Object runInLock(Luan luan,Lock lock,long timeout,LuanFunction fn,Object... args) |
249 throws LuanException, InterruptedException | 249 throws LuanException, InterruptedException |
250 { | 250 { |
251 if( !lock.tryLock(timeout,TimeUnit.MILLISECONDS) ) | 251 if( !lock.tryLock(timeout,TimeUnit.MILLISECONDS) ) |
252 throw new LuanException("failed to acquire lock"); | 252 throw new LuanException("failed to acquire lock"); |
253 try { | 253 try { |
254 return fn.call(args); | 254 return fn.call(luan,args); |
255 } finally { | 255 } finally { |
256 lock.unlock(); | 256 lock.unlock(); |
257 } | 257 } |
258 } | 258 } |
259 | 259 |