[Piglit] [Patch v2 2/3] framework.core: Replace synchronized_self decorator

Dylan Baker baker.dylan.c at gmail.com
Fri Apr 25 13:38:03 PDT 2014


This removes the synchronized_self decorator and replaces it with a
context manager.

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/core.py | 85 ++++++++++++++++++++++++++++---------------------------
 1 file changed, 43 insertions(+), 42 deletions(-)

diff --git a/framework/core.py b/framework/core.py
index cd72956..d0e91e6 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -32,13 +32,13 @@ import sys
 from cStringIO import StringIO
 # TODO: ConfigParser is known as configparser in python3
 import ConfigParser
+from threading import RLock
 try:
     import simplejson as json
 except ImportError:
     import json
 
 import framework.status as status
-from .threads import synchronized_self
 
 __all__ = ['PIGLIT_CONFIG',
            'Environment',
@@ -111,6 +111,7 @@ class JSONWriter:
         self.__indent_level = 0
         self.__inhibit_next_indent = False
         self.__encoder = PiglitJSONEncoder(indent=self.INDENT)
+        self._lock = RLock()
 
         # self.__is_collection_empty
         #
@@ -127,63 +128,63 @@ class JSONWriter:
         #
         self.__is_collection_empty = []
 
-    @synchronized_self
     def __write_indent(self):
-        if self.__inhibit_next_indent:
-            self.__inhibit_next_indent = False
-            return
-        else:
-            i = ' ' * self.__indent_level * self.INDENT
-            self.file.write(i)
+        with self._lock:
+            if self.__inhibit_next_indent:
+                self.__inhibit_next_indent = False
+                return
+            else:
+                i = ' ' * self.__indent_level * self.INDENT
+                self.file.write(i)
 
-    @synchronized_self
     def __write(self, obj):
-        lines = list(self.__encoder.encode(obj).split('\n'))
-        n = len(lines)
-        for i in range(n):
-            self.__write_indent()
-            self.file.write(lines[i])
-            if i != n - 1:
-                self.file.write('\n')
+        with self._lock:
+            lines = list(self.__encoder.encode(obj).split('\n'))
+            n = len(lines)
+            for i in range(n):
+                self.__write_indent()
+                self.file.write(lines[i])
+                if i != n - 1:
+                    self.file.write('\n')
 
-    @synchronized_self
     def open_dict(self):
-        self.__write_indent()
-        self.file.write('{')
+        with self._lock:
+            self.__write_indent()
+            self.file.write('{')
 
-        self.__indent_level += 1
-        self.__is_collection_empty.append(True)
+            self.__indent_level += 1
+            self.__is_collection_empty.append(True)
 
-    @synchronized_self
     def close_dict(self, comma=True):
-        self.__indent_level -= 1
-        self.__is_collection_empty.pop()
+        with self._lock:
+            self.__indent_level -= 1
+            self.__is_collection_empty.pop()
 
-        self.file.write('\n')
-        self.__write_indent()
-        self.file.write('}')
+            self.file.write('\n')
+            self.__write_indent()
+            self.file.write('}')
 
-    @synchronized_self
     def write_dict_item(self, key, value):
-        # Write key.
-        self.write_dict_key(key)
+        with self._lock:
+            # Write key.
+            self.write_dict_key(key)
 
-        # Write value.
-        self.__write(value)
+            # Write value.
+            self.__write(value)
 
-    @synchronized_self
     def write_dict_key(self, key):
-        # Write comma if this is not the initial item in the dict.
-        if self.__is_collection_empty[-1]:
-            self.__is_collection_empty[-1] = False
-        else:
-            self.file.write(',')
+        with self._lock:
+            # Write comma if this is not the initial item in the dict.
+            if self.__is_collection_empty[-1]:
+                self.__is_collection_empty[-1] = False
+            else:
+                self.file.write(',')
 
-        self.file.write('\n')
-        self.__write(key)
-        self.file.write(': ')
+            self.file.write('\n')
+            self.__write(key)
+            self.file.write(': ')
 
-        self.__inhibit_next_indent = True
+            self.__inhibit_next_indent = True
 
 
 # Ensure the given directory exists
-- 
2.0.0.rc0



More information about the Piglit mailing list