From 2f4e1516de7ec1c4ec301fdad9d4b332d117abc2 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Sat, 17 Jul 2010 22:58:09 +0200
Subject: [PATCH] workerpool: Additional check in BaseWorker.ShouldTerminate

Document that it should only be called from within RunTask and
add an assertion for this. This means we can no longer use a
method on the pool and hence remove WorkerPool.ShouldWorkerTerminate.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/workerpool.py | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/lib/workerpool.py b/lib/workerpool.py
index 16e713f05..e09f47329 100644
--- a/lib/workerpool.py
+++ b/lib/workerpool.py
@@ -52,10 +52,17 @@ class BaseWorker(threading.Thread, object):
     self._current_task = None
 
   def ShouldTerminate(self):
-    """Returns whether a worker should terminate.
+    """Returns whether this worker should terminate.
+
+    Should only be called from within L{RunTask}.
 
     """
-    return self.pool.ShouldWorkerTerminate(self)
+    self.pool._lock.acquire()
+    try:
+      assert self._HasRunningTaskUnlocked()
+      return self.pool._ShouldWorkerTerminateUnlocked(self)
+    finally:
+      self.pool._lock.release()
 
   def _HasRunningTaskUnlocked(self):
     """Returns whether this worker is currently running a task.
@@ -249,16 +256,6 @@ class WorkerPool(object):
     """
     return (worker in self._termworkers)
 
-  def ShouldWorkerTerminate(self, worker):
-    """Returns whether a worker should terminate.
-
-    """
-    self._lock.acquire()
-    try:
-      return self._ShouldWorkerTerminateUnlocked(worker)
-    finally:
-      self._lock.release()
-
   def _HasRunningTasksUnlocked(self):
     """Checks whether there's a task running in a worker.
 
-- 
GitLab