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