Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 97c234f45ff6ae29fe794e653f01c4466ca87f9e (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
/*******************************************************************************
 * Copyright (c) 2007, 2013 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;

import java.util.LinkedList;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.IJobChangeListener;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.tcf.protocol.IEventQueue;
import org.eclipse.tcf.protocol.Protocol;


/**
 * Implementation of Target Communication Framework event queue.
 * This implementation is intended for Eclipse environment.
 */
class EventQueue implements IEventQueue, Runnable {

    private final LinkedList<Runnable> queue = new LinkedList<Runnable>();
    private final Thread thread;
    private boolean waiting;
    private boolean shutdown;
    private int job_cnt;

    EventQueue() {
        thread = new Thread(this);
        thread.setDaemon(true);
        thread.setName("TCF Event Dispatcher"); //$NON-NLS-1$
        // Need to monitor jobs to detect congestion
        Job.getJobManager().addJobChangeListener(new IJobChangeListener() {

            public void aboutToRun(IJobChangeEvent event) {
            }

            public void awake(IJobChangeEvent event) {
            }

            public void done(IJobChangeEvent event) {
                job_cnt--;
            }

            public void running(IJobChangeEvent event) {
            }

            public void scheduled(IJobChangeEvent event) {
                job_cnt++;
            }

            public void sleeping(IJobChangeEvent event) {
            }
        });
    }

    void start() {
        thread.start();
    }

    void shutdown() {
        try {
            synchronized (this) {
                shutdown = true;
                if (waiting) {
                    waiting = false;
                    notifyAll();
                }
            }
            thread.join();
        }
        catch (Throwable e) {
            Protocol.log("Failed to shutdown TCF event dispatch thread", e); //$NON-NLS-1$
        }
    }

    public void run() {
        for (;;) {
            try {
                Runnable r = null;
                synchronized (this) {
                    while (queue.size() == 0) {
                        if (shutdown) return;
                        waiting = true;
                        wait();
                    }
                    r = queue.removeFirst();
                }
                r.run();
            }
            catch (Throwable x) {
                Protocol.log("Unhandled exception in TCF event dispatch", x); //$NON-NLS-1$
            }
        }
    }

    public synchronized void invokeLater(final Runnable r) {
        assert r != null;
        if (shutdown) {
            // If not in debug mode, silently ignore this case to avoid
            // confusion and bad impression to the user.
            if (Platform.inDebugMode()) throw new IllegalStateException("TCF event dispatcher has shut down"); //$NON-NLS-1$
            return;
        }
        queue.add(r);
        if (waiting) {
            waiting = false;
            notifyAll();
        }
    }

    public boolean isDispatchThread() {
        return Thread.currentThread() == thread;
    }

    public synchronized int getCongestion() {
        if (Job.getJobManager().isIdle()) job_cnt = 0;
        int l0 = job_cnt / 10 - 100;
        int l1 = queue.size() / 10 - 100;
        if (l1 > l0) l0 = l1;
        if (l0 > 100) l0 = 100;
        return l0;
    }
}

Back to the top