PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/model/queue/Latch.java

http://github.com/hudson/hudson
Java | 95 lines | 48 code | 11 blank | 36 comment | 6 complexity | 89b2244a1e5265bfdff233acd046fddd MD5 | raw file
 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 */
24package hudson.model.queue;
25
26import hudson.AbortException;
27
28/**
29 * A concurrency primitive that waits for N number of threads to synchronize.
30 * If any of the threads are interrupted while waiting for the completion of the condition,
31 * then all the involved threads get interrupted.
32 *
33 * @author Kohsuke Kawaguchi
34 */
35class Latch {
36    private final int n;
37    private int i=0;
38    /**
39     * If the synchronization on the latch is aborted/interrupted,
40     * point to the stack trace where that happened. If null,
41     * no interruption happened.
42     */
43    private Exception interrupted;
44
45    public Latch(int n) {
46        this.n = n;
47    }
48
49    public synchronized void abort(Throwable cause) {
50        interrupted = new AbortException();
51        if (cause!=null)
52            interrupted.initCause(cause);
53        notifyAll();
54    }
55
56
57    public synchronized void synchronize() throws InterruptedException {
58        check(n);
59
60        try {
61            onCriteriaMet();
62        } catch (Error e) {
63            abort(e);
64            throw e;
65        } catch (RuntimeException e) {
66            abort(e);
67            throw e;
68        }
69
70        check(n*2);
71    }
72
73    private void check(int threshold) throws InterruptedException {
74        i++;
75        if (i==threshold) {
76            notifyAll();
77        } else {
78            while (i<threshold && interrupted==null) {
79                try {
80                    wait();
81                } catch (InterruptedException e) {
82                    interrupted = e;
83                    notifyAll();
84                    throw e;
85                }
86            }
87        }
88
89        // all of us either leave normally or get interrupted
90        if (interrupted!=null)
91            throw (InterruptedException)new InterruptedException().initCause(interrupted);
92    }
93
94    protected void onCriteriaMet() throws InterruptedException {}
95}