Skip to main content
summaryrefslogtreecommitdiffstats
blob: 271c51ceaf455964ae2669292704f94b86ad7406 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*******************************************************************************
 * Copyright (c) 2008, 2010 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.utility.internal.synchronizers;

import java.util.Vector;
import org.eclipse.jpt.utility.Command;
import org.eclipse.jpt.utility.internal.CompositeException;
import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.jpt.utility.internal.SynchronizedObject;
import org.eclipse.jpt.utility.synchronizers.Synchronizer;

/**
 * This synchronizer will synchronize immediately and not return until the
 * synchronization and any nested (recursive) synchronizations are complete.
 * In some situations this implementation should be used sparingly, and for as
 * short a time as possible, as it increases the probability of deadlocks. A
 * deadlock can occur when {@link Synchronizer#synchronize()} is called from multiple
 * threads and multiple resources are locked by the synchronization in varying
 * orders.
 * <p>
 * As defined in the {@link Synchronizer} interface, {@link Synchronizer#start()}
 * and {@link Synchronizer#stop()}
 * should be called in the same thread, but it is not required.
 * {@link Synchronizer#synchronize()} should
 * always be called in the same thread (i.e. only recursively, beyond the
 * initial call); although, this too is not required.
 * This thread need not be the same thread that executes
 * {@link Synchronizer#start()} and {@link Synchronizer#stop()}.
 */
public class SynchronousSynchronizer
	implements Synchronizer
{
	/**
	 * The client-supplied command that performs the synchronization. It may
	 * trigger further calls to {@link #synchronize()} (i.e. the
	 * synchronization may recurse).
	 */
	private final Command command;

	/**
	 * The synchronizer's current state.
	 */
	final SynchronizedObject<State> state;

	/**
	 * The synchronizer's initial state is {@link #STOPPED}.
	 */
	enum State {
		STOPPED,
		READY,
		EXECUTING,
		REPEAT,
		STOPPING
	}

	/**
	 * A list of the uncaught exceptions thrown by the command.
	 */
	final Vector<Throwable> exceptions = new Vector<Throwable>();


	// ********** construction **********

	/**
	 * Construct a synchronous synchronizer that uses the specified command to
	 * perform the synchronization.
	 */
	public SynchronousSynchronizer(Command command) {
		super();
		if (command == null) {
			throw new NullPointerException();
		}
		this.command = command;
		// use the synchronizer as the mutex so it is freed up by the wait in #stop()
		this.state = new SynchronizedObject<State>(State.STOPPED, this);
	}


	// ********** Synchronizer implementation **********

	/**
	 * Set the synchronizer's {@link #state} to {@link State#READY READY}
	 * and execute the first synchronization. Throw an exception if the
	 * synchronizer is not {@link State#STOPPED STOPPED}.
	 */
	public synchronized void start() {
		switch (this.state.getValue()) {
			case STOPPED:
				this.state.setValue(State.READY);
				this.synchronize();
				break;
			case READY:
			case EXECUTING:
			case REPEAT:
			case STOPPING:
			default:
				throw this.buildIllegalStateException();
		}
	}

	/**
	 * It's possible to come back here if the synchronization command recurses
	 * and triggers another synchronization.
	 */
	public void synchronize() {
		if (this.beginSynchronization()) {
			this.synchronize_();
		}
	}

	/**
	 * A client has requested a synchronization.
	 * Return whether we can begin a new synchronization.
	 * If a synchronization is already under way, return <code>false</code>;
	 * but set the {@link #state} to {@link State#REPEAT REPEAT}
	 * so another synchronization will occur once the current
	 * synchronization is complete.
	 */
	private synchronized boolean beginSynchronization() {
		switch (this.state.getValue()) {
			case STOPPED:
				// synchronization is not allowed
				return false;
			case READY:
				// begin a new synchronization
				this.state.setValue(State.EXECUTING);
				return true;
			case EXECUTING:
				// set flag so a new synchronization will occur once the current one is finished
				this.state.setValue(State.REPEAT);
				return false;
			case REPEAT:
				// the "repeat" flag is already set
				return false;
			case STOPPING:
				// no further synchronizations are allowed
				return false;
			default:
				throw this.buildIllegalStateException();
		}
	}

	/**
	 * This method should be called only once per set of "recursing"
	 * synchronizations. Any recursive call to {@link #synchronize()} will
	 * simply set the {@link #state} to {@link State#REPEAT REPEAT},
	 * causing the command to execute again.
	 */
	private void synchronize_() {
		do {
			this.execute();
		} while (this.endSynchronization());
	}

	/**
	 * Execute the client-supplied command. Do not allow any unhandled
	 * exceptions to kill the thread. Store them up for later pain.
	 */
	private void execute() {
		try {
			this.execute_();
		} catch (Throwable ex) {
			this.exceptions.add(ex);
		}
	}

	/**
	 * By default, just execute the command.
	 */
	void execute_() {
		this.command.execute();
	}

	/**
	 * The current synchronization has finished.
	 * Return whether we should begin another synchronization.
	 */
	private synchronized boolean endSynchronization() {
		switch (this.state.getValue()) {
			case STOPPED:
			case READY:
				throw this.buildIllegalStateException();
			case EXECUTING:
				// synchronization has finished and there are no outstanding requests for another; return to "ready"
				this.state.setValue(State.READY);
				return false;
			case REPEAT:
				// the "repeat" flag was set; clear it and start another synchronization
				this.state.setValue(State.EXECUTING);
				return true;
			case STOPPING:
				// a client has initiated a "stop"; mark the "stop" complete and perform no more synchronizations
				this.state.setValue(State.STOPPED);
				return false;
			default:
				throw this.buildIllegalStateException();
		}
	}

	/**
	 * Set the flags so that no further synchronizations occur. If any uncaught
	 * exceptions were thrown while the synchronization was executing,
	 * wrap them in a composite exception and throw the composite exception.
	 */
	public synchronized void stop() {
		switch (this.state.getValue()) {
			case STOPPED:
				throw this.buildIllegalStateException();
			case READY:
				// simply return to "stopped" state
				this.state.setValue(State.STOPPED);
				break;
			case EXECUTING:
			case REPEAT:
				// set the "stopping" flag and wait until the synchronization has finished
				this.state.setValue(State.STOPPING);
				this.waitUntilStopped();
				break;
			case STOPPING:
				throw this.buildIllegalStateException();
			default:
				throw this.buildIllegalStateException();
		}

		if (this.exceptions.size() > 0) {
			Throwable[] temp = this.exceptions.toArray(new Throwable[this.exceptions.size()]);
			this.exceptions.clear();
			throw new CompositeException(temp);
		}
	}

	/**
	 * This wait will free up the synchronizer's synchronized methods
	 * (since the synchronizer is the state's mutex).
	 */
	private void waitUntilStopped() {
		try {
			this.state.waitUntilValueIs(State.STOPPED);
		} catch (InterruptedException ex) {
			// the thread that called #stop() was interrupted while waiting
			// for the synchronization to finish - ignore;
			// 'state' is still set to 'STOPPING', so the #synchronize_() loop
			// will still stop - we just won't wait around for it...
		}
	}

	private IllegalStateException buildIllegalStateException() {
		return new IllegalStateException("state: " + this.state); //$NON-NLS-1$
	}

	@Override
	public String toString() {
		return StringTools.buildToStringFor(this, this.state);
	}

}

Back to the top