From 8c06f01c22bfe34fadeae955c57f03e0ec9ac7bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Wed, 26 Aug 2015 21:23:58 +0200 Subject: Use event for thread synchronization in multithread mode --- scripts/loop.py | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/scripts/loop.py b/scripts/loop.py index 9161165..29be5aa 100755 --- a/scripts/loop.py +++ b/scripts/loop.py @@ -5,6 +5,7 @@ import subprocess import signal from threading import Thread from threading import Lock +from threading import Event from conf import conf from conf import sf @@ -51,6 +52,8 @@ def measure(kernelimg, con): # Multithread # __conflist__ = [] __listlock__ = Lock() +__preparethreadEvent__ = Event() +__measurethreadEvent__ = Event() class prepareThread(Thread): global __preparethread__ @@ -59,19 +62,22 @@ class prepareThread(Thread): Thread.__init__(self, name=name) def run(self): print('Prepare thread start') - __listlock__.acquire() - while not __terminate__ and len(__conflist__) <= conf.multithread_buffer: - __listlock__.release() + while not __terminate__: try: img, config = prepare() except exceptions.NoApplicableConfiguration: return __listlock__.acquire() __conflist__.append((img, config)) - if not __measurethread__.is_alive(): - __measurethread__ = measureThread() - __measurethread__.start() - __listlock__.release() + __preparethreadEvent__.set() + if len(__conflist__) > conf.multithread_buffer: + __listlock__.release() + print('Prepare thread suspended') + __measurethreadEvent__.wait() + print('Prepare thread waken') + else: + __listlock__.release() + __measurethreadEvent__.clear() print('Prepare thread stop') class measureThread(Thread): @@ -81,32 +87,39 @@ class measureThread(Thread): Thread.__init__(self, name=name) def run(self): print('Measure thread start') - __listlock__.acquire() - while not __terminate__ and len(__conflist__) > 0: + while not __terminate__: + __listlock__.acquire() + if len(__conflist__) <= 0: + __listlock__.release() + print('Measure thread suspended') + __preparethreadEvent__.wait() + print('Measure thread waken') + __listlock__.acquire() + __preparethreadEvent__.clear() img, config = __conflist__.pop() __listlock__.release() - if not __preparethread__.is_alive(): - __preparethread__ = prepareThread() - __preparethread__.start() + __measurethreadEvent__.set() measure(img, config) - __listlock__.acquire() - __listlock__.release() print('Measure thread stop') __preparethread__ = prepareThread() __measurethread__ = measureThread() # Start and sigterm handler # -__terminate__ = False def sigterm_handler(_signo, _stack_frame): global __terminate__ __terminate__ = True + if conf.multithread: + __measurethreadEvent__.set() + __preparethreadEvent__.set() # Main loop and single thread # def loop(): utils.dirtycheck() initialize.all() if conf.multithread: + global __terminate__ + __terminate__ = False __preparethread__.start() __measurethread__.start() else: -- cgit v1.2.3