File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\main\java\org\jvnet\hudson\test\SequenceLock.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package org.jvnet.hudson.test;
25
26 /**
27 * Lock mechanism to let multiple threads execute phases sequentially.
28 *
29 * @author Kohsuke Kawaguchi
30 */
31 public class SequenceLock {
32 /**
33 * Currently executing phase N.
34 */
35 private int n;
36
37 /**
38 * This thread is executing the phase
39 */
40 private Thread t;
41
42 private boolean aborted;
43
44 /**
45 * Blocks until all the previous phases are completed, and returns when the specified phase <i>i</i> is started.
46 * If the calling thread was executing an earlier phase, that phase is marked as completed.
47 *
48 * @throws IllegalStateException
49 * if the sequential lock protocol is aborted, or the thread that owns the current phase has quit.
50 */
51 public synchronized void phase(int i) throws InterruptedException {
52 done(); // mark the previous phase done
53 while (i!=n) {
54 if (aborted)
55 throw new IllegalStateException("SequenceLock aborted");
56 if (t!=null && !t.isAlive())
57 throw new IllegalStateException("Owner thread of the current phase has quit"+t);
58 if (i<n)
59 throw new IllegalStateException("Phase "+i+" is already completed");
60 wait();
61 }
62
63 t = Thread.currentThread();
64 }
65
66 /**
67 * Marks the current phase completed that the calling thread was executing.
68 *
69 * <p>
70 * This is only necessary when the thread exits the last phase, as {@link #phase(int)} call implies the
71 * {@link #done()} call.
72 */
73 public synchronized void done() {
74 if (t==Thread.currentThread()) {
75 // phase N done
76 n++;
77 t = null;
78 notifyAll();
79 }
80 }
81
82 /**
83 * Tell all the threads that this sequencing was aborted.
84 * Everyone waiting for future phases will receive an error.
85 *
86 * <p>
87 * Calling this method from the finally block prevents a dead lock if one of the participating thread
88 * aborts with an exception, as without the explicit abort operation, other threads will block forever
89 * for a phase that'll never come.
90 */
91 public synchronized void abort() {
92 aborted = true;
93 notifyAll();
94 }
95 }
96