You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ThreadSafeProgressMonitor.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.lib;
  44. import java.util.concurrent.Semaphore;
  45. import java.util.concurrent.atomic.AtomicInteger;
  46. import java.util.concurrent.locks.ReentrantLock;
  47. /**
  48. * Wrapper around the general {@link ProgressMonitor} to make it thread safe.
  49. *
  50. * Updates to the underlying ProgressMonitor are made only from the thread that
  51. * allocated this wrapper. Callers are responsible for ensuring the allocating
  52. * thread uses {@link #pollForUpdates()} or {@link #waitForCompletion()} to
  53. * update the underlying ProgressMonitor.
  54. *
  55. * Only {@link #update(int)}, {@link #isCancelled()}, and {@link #endWorker()}
  56. * may be invoked from a worker thread. All other methods of the ProgressMonitor
  57. * interface can only be called from the thread that allocates this wrapper.
  58. */
  59. public class ThreadSafeProgressMonitor implements ProgressMonitor {
  60. private final ProgressMonitor pm;
  61. private final ReentrantLock lock;
  62. private final Thread mainThread;
  63. private final AtomicInteger workers;
  64. private final AtomicInteger pendingUpdates;
  65. private final Semaphore process;
  66. /**
  67. * Wrap a ProgressMonitor to be thread safe.
  68. *
  69. * @param pm
  70. * the underlying monitor to receive events.
  71. */
  72. public ThreadSafeProgressMonitor(ProgressMonitor pm) {
  73. this.pm = pm;
  74. this.lock = new ReentrantLock();
  75. this.mainThread = Thread.currentThread();
  76. this.workers = new AtomicInteger(0);
  77. this.pendingUpdates = new AtomicInteger(0);
  78. this.process = new Semaphore(0);
  79. }
  80. public void start(int totalTasks) {
  81. if (!isMainThread())
  82. throw new IllegalStateException();
  83. pm.start(totalTasks);
  84. }
  85. public void beginTask(String title, int totalWork) {
  86. if (!isMainThread())
  87. throw new IllegalStateException();
  88. pm.beginTask(title, totalWork);
  89. }
  90. /** Notify the monitor a worker is starting. */
  91. public void startWorker() {
  92. startWorkers(1);
  93. }
  94. /**
  95. * Notify the monitor of workers starting.
  96. *
  97. * @param count
  98. * the number of worker threads that are starting.
  99. */
  100. public void startWorkers(int count) {
  101. workers.addAndGet(count);
  102. }
  103. /** Notify the monitor a worker is finished. */
  104. public void endWorker() {
  105. if (workers.decrementAndGet() == 0)
  106. process.release();
  107. }
  108. /**
  109. * Non-blocking poll for pending updates.
  110. *
  111. * This method can only be invoked by the same thread that allocated this
  112. * ThreadSafeProgressMonior.
  113. */
  114. public void pollForUpdates() {
  115. assert isMainThread();
  116. doUpdates();
  117. }
  118. /**
  119. * Process pending updates and wait for workers to finish.
  120. *
  121. * This method can only be invoked by the same thread that allocated this
  122. * ThreadSafeProgressMonior.
  123. *
  124. * @throws InterruptedException
  125. * if the main thread is interrupted while waiting for
  126. * completion of workers.
  127. */
  128. public void waitForCompletion() throws InterruptedException {
  129. assert isMainThread();
  130. while (0 < workers.get()) {
  131. doUpdates();
  132. process.acquire();
  133. }
  134. doUpdates();
  135. }
  136. private void doUpdates() {
  137. int cnt = pendingUpdates.getAndSet(0);
  138. if (0 < cnt)
  139. pm.update(cnt);
  140. }
  141. public void update(int completed) {
  142. int old = pendingUpdates.getAndAdd(completed);
  143. if (isMainThread())
  144. doUpdates();
  145. else if (old == 0)
  146. process.release();
  147. }
  148. public boolean isCancelled() {
  149. lock.lock();
  150. try {
  151. return pm.isCancelled();
  152. } finally {
  153. lock.unlock();
  154. }
  155. }
  156. public void endTask() {
  157. if (!isMainThread())
  158. throw new IllegalStateException();
  159. pm.endTask();
  160. }
  161. private boolean isMainThread() {
  162. return Thread.currentThread() == mainThread;
  163. }
  164. }