Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e680a480d61501be21212ec9e9891d7e6fdd16d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*******************************************************************************
 * Copyright (c) 2014 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.debug.test.util;

import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.eclipse.tcf.protocol.Protocol;


/**
 * Copied and adapted from org.eclipse.cdt.dsf.concurrent.
 * 
 * A convenience class that allows a client to retrieve data from services 
 * synchronously from a non-dispatch thread.  This class is different from
 * a Callable<V> in that it allows the implementation code to calculate
 * the result in several dispatches, rather than requiring it to return the 
 * data at end of Callable#call method.
 * <p>
 * Usage:<br/>
 * <pre>
 *     class DataQuery extends Query<Data> {
 *         protected void execute(DataCallback<Data> callback) {
 *             callback.setData(fSlowService.getData());
 *             callback.done();
 *         }
 *     }
 *     
 *     DsfExecutor executor = getExecutor();
 *     DataQuery query = new DataQuery();
 *     executor.submit(query);
 *     
 *     try {
 *         Data data = query.get();
 *     }
 *     
 * </pre>
 * <p> 
 * @see java.util.concurrent.Callable
 * 
 */
abstract public class Query<V> implements Future<V> 
{
    private class QueryCallback extends DataCallback<V> {

        boolean fExecuted = false;
        
        boolean fCompleted = false;
        
        private QueryCallback() { 
            super(null);
        }
        
        @Override
        public synchronized void handleCompleted() {
            fCompleted = true;
            notifyAll();
        }
    
        public synchronized boolean isCompleted() {
            return fCompleted;
        }
        
        public synchronized boolean setExecuted() {
            if (fExecuted || isCanceled()) {
                // already executed or canceled
                return false;
            } 
            fExecuted = true;
            return true;
        }
    };
    
    private final QueryCallback fCallback = new QueryCallback();
    
    /** 
     * The no-argument constructor 
     */
    public Query() {}

    public V get() throws InterruptedException, ExecutionException {
        invoke();
        Throwable error;
        V data;
        synchronized (fCallback) {
            while (!isDone()) {
                fCallback.wait();
            }
            error = fCallback.getError();
            data = fCallback.getData();
        }
        
        if (error instanceof CancellationException) {
            throw new CancellationException();
        } else if (error != null) {
            throw new ExecutionException(error);
        }
        return data;
    }

    public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        invoke();

        long timeLeft = unit.toMillis(timeout);
        long timeoutTime = System.currentTimeMillis() + unit.toMillis(timeout);

        Throwable error;
        V data;
        synchronized (fCallback) {
            while (!isDone()) {
                if (timeLeft <= 0) {
                    throw new TimeoutException();
                }
                fCallback.wait(timeLeft);
                timeLeft = timeoutTime - System.currentTimeMillis();
            }
            error = fCallback.getError();
            data = fCallback.getData();
        }
        
        if (error instanceof CancellationException) {
            throw new CancellationException();
        } else if (error != null) {
            throw new ExecutionException(error);
        }
        return data;
    }

    /**
     * Don't try to interrupt the DSF executor thread, just ignore the request 
     * if set.
     */
    public boolean cancel(boolean mayInterruptIfRunning) {
        boolean completed = false;
        synchronized (fCallback) {
            completed = fCallback.isCompleted();
            if (!completed) {
                fCallback.cancel();
                fCallback.notifyAll();
            }
        }
        return !completed; 
    }

    public boolean isCancelled() { return fCallback.isCanceled(); }

    public boolean isDone() {
        synchronized (fCallback) {
            // If future is canceled, return right away.
            return fCallback.isCompleted() || fCallback.isCanceled();
        }
    }

    abstract protected void execute(DataCallback<V> callback);
    
    public void invoke() {
        Protocol.invokeLater(new Runnable() {
            public void run() {
                if (fCallback.setExecuted()) {
                    execute(fCallback);
                }
            }
        });
    }
}

Back to the top