Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: abda52d02775b381594d31d430b36907b7f58b44 (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
/*******************************************************************************
 * Copyright (c) 2007 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 com.windriver.tcf.api.util;

import java.io.IOException;
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 com.windriver.tcf.api.protocol.Protocol;

public abstract class TCFTask<V> implements Runnable, Future<V> {
    
    private V result;
    private Throwable error;
    private boolean canceled;
    
    public TCFTask() {
        Protocol.invokeLater(new Runnable() {
            public void run() {
                try {
                    TCFTask.this.run();
                }
                catch (Throwable x) {
                    if (result == null && error == null) error(x);
                }
            }
        });
    }
    
    public synchronized void done(V result) {
        if (canceled) return;
        assert Protocol.isDispatchThread();
        assert this.error == null;
        assert this.result == null;
        this.result = result;
        notifyAll();
    }
    
    public synchronized void error(Throwable error) {
        assert Protocol.isDispatchThread();
        if (canceled) return;
        assert this.error == null;
        assert this.result == null;
        this.error = error;
        //System.err.print("TCFTask exception: ");
        //error.printStackTrace();
        notifyAll();
    }

    public synchronized boolean cancel(boolean mayInterruptIfRunning) {
        if (isDone()) return false;
        canceled = true;
        error = new CancellationException();
        notifyAll();
        return true;
    }

    public V get() throws InterruptedException, ExecutionException {
        assert !Protocol.isDispatchThread();
        synchronized (this) {
            if (!isDone()) wait();
            if (error instanceof ExecutionException) throw (ExecutionException)error;
            if (error instanceof InterruptedException) throw (InterruptedException)error;
            if (error != null) throw new ExecutionException(error);
            return result;
        }
    }
    
    public V getE() {
        try {
            return get();
        }
        catch (Throwable e) {
            if (e instanceof Error) throw (Error)e;
            throw new Error(e);
        }
    }

    public V getIO() throws IOException {
        try {
            return get();
        }
        catch (Throwable e) {
            if (e instanceof IOException) throw (IOException)e;
            IOException y = new IOException();
            y.initCause(e);
            throw y;
        }
    }

    public synchronized V get(long timeout, TimeUnit unit)
            throws InterruptedException, ExecutionException, TimeoutException {
        unit.toNanos(timeout);
        // TODO Auto-generated method stub
        assert false;
        return null;
    }

    public synchronized boolean isCancelled() {
        return canceled;
    }

    public synchronized boolean isDone() {
        return canceled || error != null || result != null;
    }
}

Back to the top