ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / TmfUiRefreshHandler.java
CommitLineData
9c91c317
AM
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
aae89862 11 * Patrick Tasse - Update queue handling
9c91c317
AM
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.tmf.ui;
15
aae89862 16import java.util.LinkedHashMap;
9c91c317 17import java.util.Map;
9c91c317 18
9c91c317
AM
19import org.eclipse.swt.widgets.Display;
20
9c91c317
AM
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
aae89862
PT
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}.
9c91c317
AM
44 *
45 * @author Alexandre Montplaisir
46 * @since 3.1
47 */
48public class TmfUiRefreshHandler {
49
9c91c317
AM
50 /** Singleton instance */
51 private static TmfUiRefreshHandler fInstance = null;
52
aae89862
PT
53 private final Map<Object, Runnable> fUpdates = new LinkedHashMap<>();
54 private Thread fCurrentTask;
9c91c317
AM
55
56
57 /**
58 * Internal constructor
59 */
60 private TmfUiRefreshHandler() {
9c91c317
AM
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() {
aae89862 80 fUpdates.clear();
9c91c317 81 fCurrentTask = null;
9c91c317
AM
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
aae89862
PT
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.
9c91c317
AM
93 * @param task
94 * The {@link Runnable} to execute in the UI thread.
95 */
aae89862 96 public synchronized void queueUpdate(Object source, Runnable task) {
9c91c317
AM
97 fUpdates.put(source, task);
98 if (fCurrentTask == null) {
99 fCurrentTask = new RunAllUpdates();
aae89862 100 fCurrentTask.start();
9c91c317
AM
101 }
102 }
103
104 /**
aae89862 105 * Task to empty the update queue, and send each task to the UI thread.
9c91c317 106 */
aae89862 107 private class RunAllUpdates extends Thread {
9c91c317
AM
108 @Override
109 public void run() {
aae89862
PT
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;
9c91c317
AM
125 }
126 }
aae89862 127 Display.getDefault().syncExec(nextTask);
9c91c317
AM
128 }
129 }
130 }
131}
This page took 0.046754 seconds and 5 git commands to generate.