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

Dylan Baker baker.dylan.c at gmail.com
Tue Apr 1 10:12:55 PDT 2014


This removes the synchronized_self decorator and replaces it with the
standard python idiom

with lock:
    <stuff>

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 5f5cf86..cc0dc82 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -37,6 +37,7 @@ import multiprocessing.dummy
 import importlib
 # TODO: ConfigParser is known as configparser in python3
 import ConfigParser
+from threading import RLock
 try:
     import simplejson as json
 except ImportError:
@@ -44,7 +45,6 @@ except ImportError:
 
 import framework.status as status
 from .dmesg import get_dmesg
-from .threads import synchronized_self
 from .log import Log
 
 __all__ = ['PIGLIT_CONFIG',
@@ -120,6 +120,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
         #
@@ -136,63 +137,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
-- 
1.9.1



More information about the Piglit mailing list