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;
25 private final Semaphore requests;
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;
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;
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;
66 synchronized void dispose() {
68 // System.err.println("qt dispose");
72 // condition.signalAll();
77 } catch (InterruptedException e) {
78 LOGGER.error("dispose was interrupted", e);
84 // System.err.println("qt disposed");
88 boolean isDisposed() {
94 public Session getSession() {
100 private boolean pumpTask() {
101 return processor.scheduling.pumpTask(tasks);
104 ArrayList<SessionTask> newTasks(boolean doWait, ArrayList<SessionTask> tasks) {
110 // Return tasks if some were found
111 if(!tasks.isEmpty()) return tasks;
113 if(!doWait) return null;
115 synchronized (querySupportLock) {
122 // We are the last one awake
123 if(sleepers.incrementAndGet() == THREADS) {
125 // Do not indicate sleeping yet
126 sleepers.decrementAndGet();
127 // Ceased can create new own tasks
128 if(querySupport == null) System.err.println("null qs");
129 querySupport.ceased(index);
136 // OK, now we are going to sleep
137 sleepers.incrementAndGet();
143 // Nope nothing. Sleep & wait
144 // Whoever releases this calls sleepers.decrementAndGet()
148 threadStates[index] = ThreadState.DISPOSED;
154 threadStates[index] = ThreadState.SLEEP;
158 sleepers.decrementAndGet();
162 threadStates[index] = ThreadState.DISPOSED;
167 threadStates[index] = ThreadState.RUN;
173 } catch (InterruptedException e) {
175 LOGGER.error("Query handling (newTasks) was interrupted", e);
176 throw new RuntimeException("Querying was interrupted.", e);
182 public boolean runSynchronized() {
184 boolean didExecute = false;
186 // for(int performer=0;performer<THREADS;performer++) {
187 // if(!delayQueues[index * THREADS + performer].isEmpty()) {
188 // synchronized(executors[performer]) {
189 // threadLocks[performer].lock();
190 // queues[performer].addAll(delayQueues[index * THREADS + performer]);
191 // delayQueues[index * THREADS + performer].clear();
192 // executors[performer].notify();
193 // threadLocks[performer].unlock();
198 if(tasks.isEmpty()) {
199 ArrayList<SessionTask> finished = newTasks(false, tasks);
200 if(finished == null) return didExecute;
203 while(!tasks.isEmpty()) {
205 SessionTask task = tasks.remove(tasks.size() - 1);
207 // if(task.syncCaller == index) {
208 // ownSyncTasks[index].add(task);
211 // System.err.println("QT(s) " + index + " runs " + task);
224 QuerySupport support = this.querySupport;
230 boolean finished = newTasks(true, tasks) == null;
235 while(!tasks.isEmpty()) {
237 SessionTask task = tasks.remove(tasks.size()-1);
244 } catch (Throwable t) {
246 LOGGER.error("FATAL BUG: QueryThread task processing caused unexpected exception.", t);