]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryThread.java
Multiple readers in db client
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryThread.java
1 package org.simantics.db.impl.query;
2
3 import java.util.ArrayList;
4 import java.util.concurrent.Semaphore;
5 import java.util.concurrent.atomic.AtomicInteger;
6
7 import org.simantics.db.Session;
8 import org.simantics.db.common.SessionThread;
9 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
10 import org.simantics.db.impl.query.QueryProcessor.ThreadState;
11 import org.slf4j.LoggerFactory;
12
13 class QueryThread extends Thread implements SessionThread {
14
15         private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(QueryThread.class);
16
17         boolean disposed = false;
18         private Semaphore exited = new Semaphore(0);
19
20         final int index;
21
22         private Session session;
23         private QuerySupport querySupport;
24         private final QueryProcessor processor;
25         private final Semaphore requests;
26
27         final private ArrayList<SessionTask> tasks = new ArrayList<SessionTask>();
28 //      final private ArrayList<SessionTask> own;
29 //      final private ArrayList<SessionTask> ownSync;
30 //      final private ArrayList<SessionTask> queue;
31 //      final private ReentrantLock lock;
32 //      final private Condition condition;
33         final private Object querySupportLock;
34         final private int THREADS;
35         final private AtomicInteger sleepers;
36         final private ThreadState[] threadStates;
37 //      final private ArrayList<SessionTask>[] delayQueues;
38 //      final private QueryThread[] executors;
39 //      final private ReentrantLock[] threadLocks;
40 //      final private ArrayList<SessionTask>[] queues;
41 //      final private ArrayList<SessionTask>[] ownSyncTasks;
42
43         public QueryThread(Session session, QueryProcessor processor, int index, String name) {
44                 super(QueryProcessor.QueryThreadGroup, null, name);
45                 this.session = session;
46                 this.processor = processor;
47                 this.index = index;
48 //              own = processor.ownTasks[index];
49 //              ownSync = processor.ownSyncTasks[index];
50 //              queue = processor.queues[index];
51 //              lock = processor.threadLocks[index];
52 //              condition = processor.threadConditions[index];
53                 querySupportLock = processor.querySupportLock;
54                 THREADS = processor.THREADS;
55                 sleepers = processor.sleepers;
56                 querySupport = processor.querySupport;
57                 threadStates = processor.threadStates;
58                 requests = processor.requests;
59 //              delayQueues = processor.delayQueues;
60 //              executors = processor.executors;
61 //              threadLocks = processor.threadLocks;
62 //              queues = processor.queues;
63 //              ownSyncTasks = processor.ownSyncTasks;
64         }
65
66         synchronized void dispose() {
67
68 //              System.err.println("qt dispose");
69
70                 disposed = true;
71 //              lock.lock();
72 //              condition.signalAll();
73 //              lock.unlock();
74                 
75                 try {
76                         exited.acquire();
77                 } catch (InterruptedException e) {
78                         LOGGER.error("dispose was interrupted", e);
79                 }
80
81                 session = null;
82                 querySupport = null;
83
84 //              System.err.println("qt disposed");
85
86         }
87
88         boolean isDisposed() { 
89
90                 return disposed;
91
92         }
93
94         public Session getSession() {
95
96                 return session;
97
98         }
99
100         private boolean pumpTask() {
101             // First finish existing executions
102         if(!processor.freeScheduling.isEmpty()) {
103             tasks.add(processor.freeScheduling.removeFirst());
104             return true;
105         }
106         // Check for new tasks
107         if(!processor.topLevelTasks.isEmpty()) {
108             tasks.add(processor.topLevelTasks.removeFirst());
109             return true;
110         }
111                 return false;
112         }
113         
114         ArrayList<SessionTask> newTasks(boolean doWait, ArrayList<SessionTask> tasks) {
115
116                 try {
117
118                         while(true) {
119
120                                 // Return tasks if some were found
121                                 if(!tasks.isEmpty()) return tasks;
122                                 
123                                 if(!doWait) return null;
124
125                                 synchronized (querySupportLock) {
126
127                                         if(pumpTask())
128                                                 return tasks;
129
130 //                                      lock.lock();
131
132                                         // We are the last one awake
133                                         if(sleepers.incrementAndGet() == THREADS) {
134
135                                                 // Do not indicate sleeping yet
136                                                 sleepers.decrementAndGet();
137                                                 // Ceased can create new own tasks
138                                                 if(querySupport == null) System.err.println("null qs");
139                                                 querySupport.ceased(index);
140
141                                                 if(pumpTask()) {
142 //                                                      lock.unlock();
143                                                         return tasks;
144                                                 }
145
146                                                 // OK, now we are going to sleep
147                                                 sleepers.incrementAndGet();
148
149                                         }
150
151                                 }
152
153                                 // Nope nothing. Sleep & wait
154                                 // Whoever releases this calls sleepers.decrementAndGet()
155
156                                 // We are done
157                                 if(isDisposed()) {
158                                         threadStates[index] = ThreadState.DISPOSED;
159 //                                      lock.unlock();
160                                         return null;
161                                 }
162
163                                 
164                                 threadStates[index] = ThreadState.SLEEP;
165                                 
166                                 requests.acquire();
167
168                                 sleepers.decrementAndGet();
169
170                                 // We are done
171                                 if(isDisposed()) {
172                                         threadStates[index] = ThreadState.DISPOSED;
173                                         //lock.unlock();
174                                         return null;
175                                 }
176
177                                 threadStates[index] = ThreadState.RUN;
178
179                                 //lock.unlock();
180
181                         }
182
183                 } catch (InterruptedException e) {
184
185                         LOGGER.error("Query handling (newTasks) was interrupted", e);
186                         throw new RuntimeException("Querying was interrupted.", e);
187
188                 }
189
190         }
191
192         public boolean runSynchronized() {
193
194                 boolean didExecute = false;
195
196 //              for(int performer=0;performer<THREADS;performer++) {
197 //                      if(!delayQueues[index * THREADS + performer].isEmpty()) {
198 //                              synchronized(executors[performer]) {
199 //                                      threadLocks[performer].lock();
200 //                                      queues[performer].addAll(delayQueues[index * THREADS + performer]);
201 //                                      delayQueues[index * THREADS + performer].clear();
202 //                                      executors[performer].notify();
203 //                                      threadLocks[performer].unlock();
204 //                              }
205 //                      }
206 //              }
207
208                 if(tasks.isEmpty()) {
209                         ArrayList<SessionTask> finished = newTasks(false, tasks);
210                         if(finished == null) return didExecute; 
211                 }
212
213                 while(!tasks.isEmpty()) {
214
215                         SessionTask task = tasks.remove(tasks.size() - 1);
216
217 //                      if(task.syncCaller == index) {
218 //                              ownSyncTasks[index].add(task);
219 //                      } else {
220                                 task.run(index);
221 //                              System.err.println("QT(s) " + index + " runs " + task);
222                                 didExecute = true;
223 //                      }
224
225                 }
226
227                 return didExecute;
228
229         }
230
231         @Override
232         public void run() {
233
234                 QuerySupport support = this.querySupport;
235
236                 try {
237
238                         while (true) {
239
240                                 boolean finished = newTasks(true, tasks) == null;
241                                 if(finished) {
242                                         return;
243                                 }
244                                 
245                                 while(!tasks.isEmpty()) {
246
247                                         SessionTask task = tasks.remove(tasks.size()-1);
248                                         task.run(0);
249
250                                 }
251
252                         }
253
254                 } catch (Throwable t) {
255
256                         LOGGER.error("FATAL BUG: QueryThread task processing caused unexpected exception.", t);
257                         support.exit(t);
258
259                 } finally {
260                         
261                         exited.release();
262
263                 }
264
265         }
266
267 }