1 /*******************************************************************************
2 * Copyright (c) 2000, 2014 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.widgets;
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.graphics.*;
20 * Instances of this class provide synchronization support
21 * for displays. A default instance is created automatically
22 * for each display, and this instance is sufficient for almost
25 * <b>IMPORTANT:</b> Typical application code <em>never</em>
26 * needs to deal with this class. It is provided only to
27 * allow applications which require non-standard
28 * synchronization behavior to plug in the support they
29 * require. <em>Subclasses which override the methods in
30 * this class must ensure that the superclass methods are
31 * invoked in their implementations</em>
34 * @see Display#setSynchronizer
35 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
37 public class Synchronizer {
40 RunnableLock [] messages;
41 Object messageLock = new Object ();
43 static final int GROW_SIZE = 4;
44 static final int MESSAGE_LIMIT = 64;
47 static final boolean IS_COCOA = "cocoa".equals (SWT.getPlatform ());
48 static final boolean IS_GTK = "gtk".equals (SWT.getPlatform ());
51 * Constructs a new instance of this class.
53 * @param display the display to create the synchronizer on
55 public Synchronizer (Display display) {
56 this.display = display;
60 * Removes all pending events from the receiver and inserts them into the beginning of the given
61 * synchronizer's queue
63 * @param toReceiveTheEvents the synchronizer that will receive the events
65 void moveAllEventsTo (Synchronizer toReceiveTheEvents) {
66 RunnableLock[] oldMessages;
68 synchronized (messageLock) {
69 oldMessages = messages;
71 oldMessageCount = messageCount;
74 toReceiveTheEvents.addFirst(oldMessages, oldMessageCount);
78 * Adds the given events to the beginning of the message queue, to
79 * be processed in order.
81 * @param toAdd events to add. Permits null if and only if numToAdd is 0.
82 * @param numToAdd number of events to add from the beginning of the given array.
84 void addFirst (RunnableLock[] toAdd, int numToAdd) {
89 synchronized (messageLock) {
90 int nextSize = messageCount + Math.max(numToAdd, GROW_SIZE);
92 messages = new RunnableLock[nextSize];
93 if (messages.length < messageCount + numToAdd) {
94 RunnableLock[] newMessages = new RunnableLock[nextSize];
95 System.arraycopy(messages, 0, newMessages, numToAdd, messageCount);
96 messages = newMessages;
98 System.arraycopy(messages, 0, messages, numToAdd, messageCount);
100 System.arraycopy(toAdd, 0, messages, 0, numToAdd);
101 wake = (messageCount == 0);
102 messageCount += numToAdd;
105 display.wakeThread();
108 void addLast (RunnableLock lock) {
109 boolean wake = false;
110 synchronized (messageLock) {
111 if (messages == null) messages = new RunnableLock [GROW_SIZE];
112 if (messageCount == messages.length) {
113 RunnableLock[] newMessages = new RunnableLock [messageCount + GROW_SIZE];
114 System.arraycopy (messages, 0, newMessages, 0, messageCount);
115 messages = newMessages;
117 messages [messageCount++] = lock;
118 wake = messageCount == 1;
120 if (wake) display.wakeThread ();
124 * Causes the <code>run()</code> method of the runnable to
125 * be invoked by the user-interface thread at the next
126 * reasonable opportunity. The caller of this method continues
127 * to run in parallel, and is not notified when the
128 * runnable has completed.
130 * @param runnable code to run on the user-interface thread.
134 protected void asyncExec (Runnable runnable) {
135 if (runnable == null) {
137 if (!(IS_GTK || IS_COCOA)) {
142 addLast (new RunnableLock (runnable));
145 int getMessageCount () {
146 synchronized (messageLock) {
151 void releaseSynchronizer () {
158 RunnableLock removeFirst () {
159 synchronized (messageLock) {
160 if (messageCount == 0) return null;
161 RunnableLock lock = messages [0];
162 System.arraycopy (messages, 1, messages, 0, --messageCount);
163 messages [messageCount] = null;
164 if (messageCount == 0) {
165 if (messages.length > MESSAGE_LIMIT) messages = null;
171 boolean runAsyncMessages () {
172 return runAsyncMessages (false);
175 boolean runAsyncMessages (boolean all) {
178 RunnableLock lock = removeFirst ();
179 if (lock == null) return run;
181 synchronized (lock) {
182 syncThread = lock.thread;
183 display.sendPreEvent(SWT.None);
186 } catch (Throwable t) {
188 SWT.error (SWT.ERROR_FAILED_EXEC, t);
190 if (display != null && !display.isDisposed()) {
191 display.sendPostEvent(SWT.None);
202 * Causes the <code>run()</code> method of the runnable to
203 * be invoked by the user-interface thread at the next
204 * reasonable opportunity. The thread which calls this method
205 * is suspended until the runnable completes.
207 * @param runnable code to run on the user-interface thread.
209 * @exception SWTException <ul>
210 * <li>ERROR_FAILED_EXEC - if an exception occurred when executing the runnable</li>
215 protected void syncExec (Runnable runnable) {
216 RunnableLock lock = null;
217 synchronized (Device.class) {
218 if (display == null || display.isDisposed ()) SWT.error (SWT.ERROR_DEVICE_DISPOSED);
219 if (!display.isValidThread ()) {
220 if (runnable == null) {
224 lock = new RunnableLock (runnable);
226 * Only remember the syncThread for syncExec.
228 lock.thread = Thread.currentThread();
233 if (runnable != null) {
234 display.sendPreEvent(SWT.None);
237 } catch (RuntimeException exception) {
238 display.getRuntimeExceptionHandler ().accept (exception);
239 } catch (Error error) {
240 display.getErrorHandler ().accept (error);
242 if (display != null && !display.isDisposed()) {
243 display.sendPostEvent(SWT.None);
249 synchronized (lock) {
250 boolean interrupted = false;
251 while (!lock.done ()) {
254 } catch (InterruptedException e) {
259 Thread.currentThread().interrupt();
261 if (lock.throwable != null) {
262 SWT.error (SWT.ERROR_FAILED_EXEC, lock.throwable);