aboutsummaryrefslogtreecommitdiff
path: root/piglit-run.py
diff options
context:
space:
mode:
authorU. Artie Eoff <ullysses.a.eoff@intel.com>2011-02-07 18:55:00 -0800
committerChad Versace <chad.versace@intel.com>2011-02-10 13:38:24 -0800
commit4d87ef44973e30b073f62e63ebfdd31197aaf18f (patch)
treebf509a6b7f05d5bd2a8327129353991dec2baec9 /piglit-run.py
parentccdef3127a815fa5cc7cb6e5686a9f0f4a7e4d9c (diff)
downloadpiglit-4d87ef44973e30b073f62e63ebfdd31197aaf18f.tar.gz
Add SyncFileWriter for write/close synchronization on results file.
Add SyncFileWriter class to synchronize writes to the 'main' results file from multiple threads. This helps to ensure that writes to this file are not intermingled.
Diffstat (limited to 'piglit-run.py')
-rwxr-xr-xpiglit-run.py40
1 files changed, 35 insertions, 5 deletions
diff --git a/piglit-run.py b/piglit-run.py
index 9f0eca67..1e6d5158 100755
--- a/piglit-run.py
+++ b/piglit-run.py
@@ -24,11 +24,41 @@
from getopt import getopt, GetoptError
import re
-import sys
+import sys, os
import framework.core as core
-
-
+from framework.threads import synchronized_self
+
+class SyncFileWriter:
+ '''
+ Using the 'print' syntax to write to an instance of this class
+ may have unexpected results in a multithreaded program. For example:
+ print >> file, "a", "b", "c"
+ will call write() to write "a", then call write() to write "b", and so on...
+ This type of execution allows for another thread to call write() before
+ the original statement completes its execution.
+ To avoid this behavior, call file.write() explicitly. For example:
+ file.write("a", "b", "c", "\n")
+ will ensure that "a b c" gets written to the file before any other thread
+ is given write access.
+ '''
+ def __init__(self, filename):
+ self.file = open(filename, 'w')
+
+ @synchronized_self
+ def write(self, *args):
+ [self.file.write(str(a)) for a in args]
+ self.file.flush()
+ os.fsync(self.file.fileno())
+
+ @synchronized_self
+ def writeLine(self, *args):
+ self.write(*args)
+ self.write('\n')
+
+ @synchronized_self
+ def close(self):
+ self.file.close()
#############################################################################
##### Main program
@@ -91,8 +121,8 @@ def main():
core.checkDir(resultsDir, False)
profile = core.loadTestProfile(profileFilename)
- env.file = open(resultsDir + '/main', "w")
- print >>env.file, "name: %(name)s" % { 'name': core.encode(OptionName) }
+ env.file = SyncFileWriter(resultsDir + '/main')
+ env.file.writeLine("name: %(name)s" % { 'name': core.encode(OptionName) })
env.collectData()
profile.run(env)
env.file.close()