1 package org.simantics.db.impl.query;
3 import java.util.ArrayList;
4 import java.util.concurrent.Semaphore;
5 import java.util.concurrent.atomic.AtomicInteger;
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;
13 class QueryThread extends Thread implements SessionThread {
15 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(QueryThread.class);
17 boolean disposed = false;
18 private Semaphore exited = new Semaphore(0);
22 private Session session;
23 private QuerySupport querySupport;
24 private final QueryProcessor processor;
26 final private ArrayList<SessionTask> tasks = new ArrayList<SessionTask>();
27 // final private ArrayList<SessionTask> own;
28 // final private ArrayList<SessionTask> ownSync;
29 // final private ArrayList<SessionTask> queue;
30 // final private ReentrantLock lock;
31 // final private Condition condition;
32 final private Object querySupportLock;
33 final private int THREADS;
34 final private AtomicInteger sleepers;
35 final private ThreadState[] threadStates;
36 // final private ArrayList<SessionTask>[] delayQueues;
37 // final private QueryThread[] executors;
38 // final private ReentrantLock[] threadLocks;
39 // final private ArrayList<SessionTask>[] queues;
40 // final private ArrayList<SessionTask>[] ownSyncTasks;
42 public QueryThread(Session session, QueryProcessor processor, int index, String name) {
43 super(QueryProcessor.QueryThreadGroup, null, name);
44 this.session = session;
45 this.processor = processor;
47 // own = processor.ownTasks[index];
48 // ownSync = processor.ownSyncTasks[index];
49 // queue = processor.queues[index];
50 // lock = processor.threadLocks[index];
51 // condition = processor.threadConditions[index];
52 querySupportLock = processor.querySupportLock;
53 THREADS = processor.THREADS;
54 sleepers = processor.sleepers;
55 querySupport = processor.querySupport;
56 threadStates = processor.threadStates;
57 // delayQueues = processor.delayQueues;
58 // executors = processor.executors;
59 // threadLocks = processor.threadLocks;
60 // queues = processor.queues;
61 // ownSyncTasks = processor.ownSyncTasks;
64 synchronized void dispose() {
66 // System.err.println("qt dispose");
70 // condition.signalAll();
75 } catch (InterruptedException e) {
76 LOGGER.error("dispose was interrupted", e);
82 // System.err.println("qt disposed");
86 boolean isDisposed() {
92 public Session getSession() {
98 private boolean pumpTask() {
99 if(!processor.freeScheduling.isEmpty()) {
100 tasks.add(processor.freeScheduling.removeFirst());
106 ArrayList<SessionTask> newTasks(boolean doWait, ArrayList<SessionTask> tasks) {
112 // Return tasks if some were found
113 if(!tasks.isEmpty()) return tasks;
115 if(!doWait) return null;
117 synchronized (querySupportLock) {
124 // We are the last one awake
125 if(sleepers.incrementAndGet() == THREADS) {
127 // Do not indicate sleeping yet
128 sleepers.decrementAndGet();
129 // Ceased can create new own tasks
130 if(querySupport == null) System.err.println("null qs");
131 querySupport.ceased(index);
138 // OK, now we are going to sleep
139 sleepers.incrementAndGet();
145 // Nope nothing. Sleep & wait
146 // Whoever releases this calls sleepers.decrementAndGet()
150 threadStates[index] = ThreadState.DISPOSED;
156 threadStates[index] = ThreadState.SLEEP;
158 synchronized (querySupportLock) {
159 querySupportLock.wait(100);
163 // boolean woken = condition.await(10, TimeUnit.MILLISECONDS);
165 // synchronized (querySupportLock) {
166 // if(!processor.freeScheduling.isEmpty())
167 // System.err.println("some tasks are available!");
171 sleepers.decrementAndGet();
175 threadStates[index] = ThreadState.DISPOSED;
180 threadStates[index] = ThreadState.RUN;
186 } catch (InterruptedException e) {
188 LOGGER.error("Query handling (newTasks) was interrupted", e);
189 throw new RuntimeException("Querying was interrupted.", e);
195 public boolean runSynchronized() {
197 boolean didExecute = false;
199 // for(int performer=0;performer<THREADS;performer++) {
200 // if(!delayQueues[index * THREADS + performer].isEmpty()) {
201 // synchronized(executors[performer]) {
202 // threadLocks[performer].lock();
203 // queues[performer].addAll(delayQueues[index * THREADS + performer]);
204 // delayQueues[index * THREADS + performer].clear();
205 // executors[performer].notify();
206 // threadLocks[performer].unlock();
211 if(tasks.isEmpty()) {
212 ArrayList<SessionTask> finished = newTasks(false, tasks);
213 if(finished == null) return didExecute;
216 while(!tasks.isEmpty()) {
218 SessionTask task = tasks.remove(tasks.size() - 1);
220 // if(task.syncCaller == index) {
221 // ownSyncTasks[index].add(task);
224 // System.err.println("QT(s) " + index + " runs " + task);
237 processor.thread.set(index);
239 QuerySupport support = this.querySupport;
245 boolean finished = newTasks(true, tasks) == null;
250 while(!tasks.isEmpty()) {
252 SessionTask task = tasks.remove(tasks.size()-1);
253 // System.err.println("QT " + index + " runs " + task);
258 // for(int performer=0;performer<THREADS;performer++) {
259 // if(!delayQueues[index * THREADS + performer].isEmpty()) {
260 // synchronized(executors[performer]) {
261 // threadLocks[performer].lock();
262 // queues[performer].addAll(delayQueues[index * THREADS + performer]);
263 // delayQueues[index * THREADS + performer].clear();
264 // executors[performer].notify();
265 // threadLocks[performer].unlock();
272 } catch (Throwable t) {
274 LOGGER.error("FATAL BUG: QueryThread task processing caused unexpected exception.", t);