]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.scl/src/org/simantics/interop/scl/Threads.java
SCL threads support
[simantics/interop.git] / org.simantics.interop.scl / src / org / simantics / interop / scl / Threads.java
1 package org.simantics.interop.scl;
2
3 import java.util.concurrent.ScheduledThreadPoolExecutor;
4 import java.util.concurrent.ThreadFactory;
5 import java.util.concurrent.atomic.AtomicInteger;
6
7 import org.simantics.scl.runtime.SCLContext;
8 import org.simantics.scl.runtime.function.Function;
9 import org.simantics.scl.runtime.tuple.Tuple0;
10
11 public class Threads {
12         
13         private static final AtomicInteger threadCount = new AtomicInteger(0);
14         
15     private static final ThreadFactory threadFactory = r -> {
16                 Thread t = new Thread(r, "scl-interop-thread-" + threadCount.incrementAndGet());
17                 t.setDaemon(true);
18                 return t;
19             };
20
21
22         private static final ScheduledThreadPoolExecutor scheduledExecutor;
23            
24         static {
25                 scheduledExecutor = new ScheduledThreadPoolExecutor(2, threadFactory);
26                 scheduledExecutor.setMaximumPoolSize(2);
27         }
28         
29         public static void setMaximumPoolSize(int size) {
30                 if (size < 2)
31                         return;
32                 scheduledExecutor.setMaximumPoolSize(size);
33         }
34         
35     @SuppressWarnings({ "rawtypes", "unchecked" })
36     public static SCLThread runAsync(Function f) {
37         SCLContext context = SCLContext.createDerivedContext();
38         SCLThread t = new SCLThread(context,f);
39         scheduledExecutor.submit(t);
40         return t;
41     }
42         
43         public static class SCLThread implements Runnable {
44                 SCLContext context;
45                 Function f;
46                 boolean running = false;
47                 boolean completed = false;
48                 Object returnValue = null;
49                 Throwable error;
50                 
51                 public SCLThread(SCLContext context, Function f) {
52                         this.context = context;
53                         this.f = f;
54                 }
55                 
56                 @Override
57                 public void run() {
58                         SCLContext.push(context);
59                         running = true;
60                         try {
61                                 returnValue = f.apply(Tuple0.INSTANCE);
62                         } catch (Throwable t) {
63                                 error = t;
64                         } finally {
65                 SCLContext.pop();
66             }
67                         running = false;
68                         completed = true;
69                 }
70                 
71                 public boolean isRunning() {
72                         return running;
73                 }
74                 
75                 public boolean isCompleted() {
76                         return completed;
77                 }
78                 
79                 public boolean hasErrors() {
80                         return error != null;
81                 }
82                 
83                 public Object getReturnValue() {
84                         return returnValue;
85                 }
86                 
87                 public Throwable getError() {
88                         return error;
89                 }
90                 
91         }
92
93 }