diff options
author | Karel Kočí <cynerd@email.cz> | 2015-08-26 21:23:58 +0200 |
---|---|---|
committer | Karel Kočí <cynerd@email.cz> | 2015-08-26 21:32:58 +0200 |
commit | 8c06f01c22bfe34fadeae955c57f03e0ec9ac7bb (patch) | |
tree | dd9b4b5c84ab40f27518d237bd9e03f0e8f4ba72 /scripts/loop.py | |
parent | 17e178eae3b0a39ef5eda09b0c2ed4811c405a85 (diff) | |
download | linux-conf-perf-8c06f01c22bfe34fadeae955c57f03e0ec9ac7bb.tar.gz linux-conf-perf-8c06f01c22bfe34fadeae955c57f03e0ec9ac7bb.tar.bz2 linux-conf-perf-8c06f01c22bfe34fadeae955c57f03e0ec9ac7bb.zip |
Use event for thread synchronization in multithread mode
Diffstat (limited to 'scripts/loop.py')
-rwxr-xr-x | scripts/loop.py | 43 |
1 files 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: |