summaryrefslogtreecommitdiffstats
path: root/src/python/openlmi/common/TimerManager.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/python/openlmi/common/TimerManager.py')
-rw-r--r--src/python/openlmi/common/TimerManager.py29
1 files changed, 25 insertions, 4 deletions
diff --git a/src/python/openlmi/common/TimerManager.py b/src/python/openlmi/common/TimerManager.py
index 5eb39ae..42d48cd 100644
--- a/src/python/openlmi/common/TimerManager.py
+++ b/src/python/openlmi/common/TimerManager.py
@@ -94,6 +94,7 @@ class Timer(object):
A class representing a timer. A timer has a timeout and after the timeout,
given callback is called and the timer is deleted.
"""
+
@cmpi_logging.trace_method
def __init__(self, timer_manager, name, callback=None, *args, **kwargs):
"""
@@ -193,6 +194,10 @@ class TimerManager(singletonmixin.Singleton):
you provider initialization.
"""
+ # Commands to the timer thread
+ COMMAND_STOP = 1
+ COMMAND_RESCHEDULE = 2
+
@cmpi_logging.trace_method
def __init__(self, env=None):
"""
@@ -215,7 +220,7 @@ class TimerManager(singletonmixin.Singleton):
self._timer_thread = threading.Thread(
target=self._timer_loop, args=(new_broker,))
- self._timer_thread.daemon = True
+ self._timer_thread.daemon = False
self._timer_thread.start()
def create_timer(self, name, callback=None, *args, **kwargs):
@@ -239,17 +244,22 @@ class TimerManager(singletonmixin.Singleton):
"""
if broker:
broker.AttachThread()
+ cmpi_logging.logger.info("Started Timer thread.")
while True:
self._handle_expired()
timeout = self._find_timeout()
if timeout != 0:
# Wait for the timeout or any change in timers.
try:
- self._queue.get(timeout=timeout)
+ command = self._queue.get(timeout=timeout)
self._queue.task_done()
+ if command == self.COMMAND_STOP:
+ break # stop the thread
+ # process COMMAND_RESCHEDULE in next loop
except Queue.Empty:
# Timeout has happened, ignore the exception.
pass
+ cmpi_logging.logger.info("Stopped Timer thread.")
@cmpi_logging.trace_method
def _handle_expired(self):
@@ -320,7 +330,7 @@ class TimerManager(singletonmixin.Singleton):
with self._lock:
self._timers.append(timer)
# Wake up the timer manager thread.
- self._queue.put("")
+ self._queue.put(self.COMMAND_RESCHEDULE)
cmpi_logging.logger.trace_info("Timer: Timer %s added" % (timer._name))
@cmpi_logging.trace_method
@@ -336,7 +346,7 @@ class TimerManager(singletonmixin.Singleton):
except ValueError:
pass
# Wake up the timer manager thread.
- self._queue.put("")
+ self._queue.put(self.COMMAND_RESCHEDULE)
cmpi_logging.logger.trace_info("Timer: Timer %s removed"
% (timer._name))
@@ -350,6 +360,15 @@ class TimerManager(singletonmixin.Singleton):
"""
return self._clock.now()
+ @cmpi_logging.trace_method
+ def shutdown(self):
+ """
+ Stop the thread. This method blocks until the thread is safely
+ destroyed.
+ """
+ self._queue.put(self.COMMAND_STOP)
+ self._timer_thread.join()
+
if __name__ == "__main__":
cmpi_logging.logger = cmpi_logging.CMPILogger("")
import time
@@ -398,3 +417,5 @@ if __name__ == "__main__":
t15.start(1.5)
time.sleep(4)
+
+ mgr.stop_thread()