tmf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / TmfUiRefreshHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 * Patrick Tasse - Update queue handling
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui;
15
16 import java.util.LinkedHashMap;
17 import java.util.Map;
18
19 import org.eclipse.swt.widgets.Display;
20
21 /**
22 * This handler offers "coalescing" of UI updates.
23 *
24 * When displaying live experiments containing a high number of traces, every
25 * trace will want to regularly update views with their new available data. This
26 * can cause a high number of threads calling {@link Display#asyncExec}
27 * repeatedly, which can really impede UI responsiveness.
28 * <p>
29 * Instead of calling {@link Display#asyncExec} directly, threads that want to
30 * queue updates to the UI can instead call
31 * {@link TmfUiRefreshHandler#queueUpdate}. If the handler is not currently
32 * executing another update it will be scheduled immediately. Otherwise the
33 * update will be queued.
34 * <p>
35 * The handler will only execute one update at a time. While it is busy, new
36 * requests received from a source that is already in the queue will replace the
37 * previous one (as we assume the latest UI update request is the most
38 * up-to-date and interesting one), preserving the previous request order. New
39 * requests received from other sources will be added to the end of the queue
40 * (keeping only the latest request from each source).
41 * <p>
42 * Once the current update is completed, the oldest request in the queue will be
43 * sent to the UI thread via one single call to {@link Display#syncExec}.
44 *
45 * @author Alexandre Montplaisir
46 * @since 3.1
47 */
48 public class TmfUiRefreshHandler {
49
50 /** Singleton instance */
51 private static TmfUiRefreshHandler fInstance = null;
52
53 private final Map<Object, Runnable> fUpdates = new LinkedHashMap<>();
54 private Thread fCurrentTask;
55
56
57 /**
58 * Internal constructor
59 */
60 private TmfUiRefreshHandler() {
61 fCurrentTask = null;
62 }
63
64 /**
65 * Get the handler's instance
66 *
67 * @return The singleton instance
68 */
69 public static synchronized TmfUiRefreshHandler getInstance() {
70 if (fInstance == null) {
71 fInstance = new TmfUiRefreshHandler();
72 }
73 return fInstance;
74 }
75
76 /**
77 * Cancel all current requests and dispose the handler.
78 */
79 public synchronized void dispose() {
80 fUpdates.clear();
81 fCurrentTask = null;
82 }
83
84 /**
85 * Queue a UI update. Threads that want to benefit from "UI coalescing"
86 * should send their {@link Runnable} to this method, instead of
87 * {@link Display#asyncExec(Runnable)}.
88 *
89 * @param source
90 * The source sending the request. Typically callers should use
91 * "this". When multiple requests are queued before being
92 * executed, only the latest request per source is actually sent.
93 * @param task
94 * The {@link Runnable} to execute in the UI thread.
95 */
96 public synchronized void queueUpdate(Object source, Runnable task) {
97 fUpdates.put(source, task);
98 if (fCurrentTask == null) {
99 fCurrentTask = new RunAllUpdates();
100 fCurrentTask.start();
101 }
102 }
103
104 /**
105 * Task to empty the update queue, and send each task to the UI thread.
106 */
107 private class RunAllUpdates extends Thread {
108 @Override
109 public void run() {
110 while (true) {
111 Runnable nextTask = null;
112 synchronized (TmfUiRefreshHandler.this) {
113 if (!fUpdates.isEmpty()) {
114 Object firstKey = fUpdates.keySet().iterator().next();
115 nextTask = fUpdates.get(firstKey);
116 fUpdates.remove(firstKey);
117 }
118 if (nextTask == null) {
119 /*
120 * No updates remaining in the queue, put fCurrentTask
121 * back to null so that a new task can be scheduled.
122 */
123 fCurrentTask = null;
124 break;
125 }
126 }
127 Display.getDefault().syncExec(nextTask);
128 }
129 }
130 }
131 }
This page took 0.034257 seconds and 5 git commands to generate.