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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 org.eclipse.jgit.lib.ProgressMonitor} to
  49. * make it thread safe.
  50. *
  51. * Updates to the underlying ProgressMonitor are made only from the thread that
  52. * allocated this wrapper. Callers are responsible for ensuring the allocating
  53. * thread uses {@link #pollForUpdates()} or {@link #waitForCompletion()} to
  54. * update the underlying ProgressMonitor.
  55. *
  56. * Only {@link #update(int)}, {@link #isCancelled()}, and {@link #endWorker()}
  57. * may be invoked from a worker thread. All other methods of the ProgressMonitor
  58. * interface can only be called from the thread that allocates this wrapper.
  59. */
  60. public class ThreadSafeProgressMonitor implements ProgressMonitor {
  61. private final ProgressMonitor pm;
  62. private final ReentrantLock lock;
  63. private final Thread mainThread;
  64. private final AtomicInteger workers;
  65. private final AtomicInteger pendingUpdates;
  66. private final Semaphore process;
  67. /**
  68. * Wrap a ProgressMonitor to be thread safe.
  69. *
  70. * @param pm
  71. * the underlying monitor to receive events.
  72. */
  73. public ThreadSafeProgressMonitor(ProgressMonitor pm) {
  74. this.pm = pm;
  75. this.lock = new ReentrantLock();
  76. this.mainThread = Thread.currentThread();
  77. this.workers = new AtomicInteger(0);
  78. this.pendingUpdates = new AtomicInteger(0);
  79. this.process = new Semaphore(0);
  80. }
  81. /** {@inheritDoc} */
  82. @Override
  83. public void start(int totalTasks) {
  84. if (!isMainThread())
  85. throw new IllegalStateException();
  86. pm.start(totalTasks);
  87. }
  88. /** {@inheritDoc} */
  89. @Override
  90. public void beginTask(String title, int totalWork) {
  91. if (!isMainThread())
  92. throw new IllegalStateException();
  93. pm.beginTask(title, totalWork);
  94. }
  95. /**
  96. * Notify the monitor a worker is starting.
  97. */
  98. public void startWorker() {
  99. startWorkers(1);
  100. }
  101. /**
  102. * Notify the monitor of workers starting.
  103. *
  104. * @param count
  105. * the number of worker threads that are starting.
  106. */
  107. public void startWorkers(int count) {
  108. workers.addAndGet(count);
  109. }
  110. /**
  111. * Notify the monitor a worker is finished.
  112. */
  113. public void endWorker() {
  114. if (workers.decrementAndGet() == 0)
  115. process.release();
  116. }
  117. /**
  118. * Non-blocking poll for pending updates.
  119. *
  120. * This method can only be invoked by the same thread that allocated this
  121. * ThreadSafeProgressMonior.
  122. */
  123. public void pollForUpdates() {
  124. assert isMainThread();
  125. doUpdates();
  126. }
  127. /**
  128. * Process pending updates and wait for workers to finish.
  129. *
  130. * This method can only be invoked by the same thread that allocated this
  131. * ThreadSafeProgressMonior.
  132. *
  133. * @throws java.lang.InterruptedException
  134. * if the main thread is interrupted while waiting for
  135. * completion of workers.
  136. */
  137. public void waitForCompletion() throws InterruptedException {
  138. assert isMainThread();
  139. while (0 < workers.get()) {
  140. doUpdates();
  141. process.acquire();
  142. }
  143. doUpdates();
  144. }
  145. private void doUpdates() {
  146. int cnt = pendingUpdates.getAndSet(0);
  147. if (0 < cnt)
  148. pm.update(cnt);
  149. }
  150. /** {@inheritDoc} */
  151. @Override
  152. public void update(int completed) {
  153. if (0 == pendingUpdates.getAndAdd(completed))
  154. process.release();
  155. }
  156. /** {@inheritDoc} */
  157. @Override
  158. public boolean isCancelled() {
  159. lock.lock();
  160. try {
  161. return pm.isCancelled();
  162. } finally {
  163. lock.unlock();
  164. }
  165. }
  166. /** {@inheritDoc} */
  167. @Override
  168. public void endTask() {
  169. if (!isMainThread())
  170. throw new IllegalStateException();
  171. pm.endTask();
  172. }
  173. private boolean isMainThread() {
  174. return Thread.currentThread() == mainThread;
  175. }
  176. }