File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\queue\Latch.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, InfraDNA, 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 hudson.model.queue;
25
26 import hudson.AbortException;
27
28 /**
29 * A concurrency primitive that waits for N number of threads to synchronize,
30 * then have them all execute a certain task, then synchronize for the completion of all N tasks.
31 *
32 * If any of the threads are interrupted while waiting for the completion of the condition or
33 * fails to complete the task, then all the involved threads get interrupted and receive a failure.
34 *
35 * @author Kohsuke Kawaguchi
36 */
37 class Latch {
38 private final int n;
39 private int i=0;
40 /**
41 * If the synchronization on the latch is aborted/interrupted,
42 * point to the stack trace where that happened. If null,
43 * no interruption happened.
44 */
45 private Exception interrupted;
46
47 /**
48 * @param n
49 * Number of threads that need to enter the {@link #synchronize()} method
50 * before all of them get released.
51 */
52 public Latch(int n) {
53 this.n = n;
54 }
55
56 public synchronized void abort(Throwable cause) {
57 interrupted = new AbortException();
58 if (cause!=null)
59 interrupted.initCause(cause);
60 notifyAll();
61 }
62
63 /**
64 * Waits for N threads to enter the {@link #synchronize()} method, then
65 * returns.
66 *
67 * @return
68 * returns normally if N threads successfully synchronized.
69 * @throws InterruptedException
70 * if any of the threads that were synchronizing get interrupted,
71 * or if the {@link #abort(Throwable)} is called.
72 */
73 public synchronized void synchronize() throws InterruptedException {
74 check(n);
75
76 try {
77 onCriteriaMet();
78 } catch (Error e) {
79 abort(e);
80 throw e;
81 } catch (RuntimeException e) {
82 abort(e);
83 throw e;
84 }
85
86 check(n*2);
87 }
88
89 private void check(int threshold) throws InterruptedException {
90 i++;
91 if (i==threshold) {
92 notifyAll();
93 } else {
94 while (i<threshold && interrupted==null) {
95 try {
96 wait();
97 } catch (InterruptedException e) {
98 interrupted = e;
99 notifyAll();
100 throw e;
101 }
102 }
103 }
104
105 // all of us either leave normally or get interrupted
106 if (interrupted!=null)
107 throw (InterruptedException)new InterruptedException().initCause(interrupted);
108 }
109
110 /**
111 * Once N threads gather, this method is called to execute to carry out some task.
112 */
113 protected void onCriteriaMet() throws InterruptedException {}
114 }
115