Author: Armin Rigo <[email protected]>
Branch: stmgc-c7
Changeset: r75611:472276917c27
Date: 2015-01-31 17:51 +0100
http://bitbucket.org/pypy/pypy/changeset/472276917c27/

Log:    Make 'policy' and 'position_key' thread-local

diff --git a/rpython/annotator/annrpython.py b/rpython/annotator/annrpython.py
--- a/rpython/annotator/annrpython.py
+++ b/rpython/annotator/annrpython.py
@@ -49,9 +49,9 @@
         self.frozen = False
         if policy is None:
             from rpython.annotator.policy import AnnotatorPolicy
-            self.policy = AnnotatorPolicy()
+            self.default_policy = AnnotatorPolicy()
         else:
-            self.policy = policy
+            self.default_policy = policy
         if bookkeeper is None:
             bookkeeper = Bookkeeper(self)
         self.bookkeeper = bookkeeper
@@ -101,14 +101,13 @@
             result.append((graph, inputcells))
             return annmodel.s_ImpossibleValue
 
-        prevpolicy = self.policy
-        self.policy = policy
+        prevpolicy = self.bookkeeper.change_policy(policy)
         self.bookkeeper.enter(None)
         try:
             desc.pycall(schedule, args, annmodel.s_ImpossibleValue)
         finally:
             self.bookkeeper.leave()
-            self.policy = prevpolicy
+            self.bookkeeper.change_policy(prevpolicy)
         [(graph, inputcells)] = result
         return graph, inputcells
 
@@ -125,15 +124,16 @@
         return graph
 
     def complete_helpers(self, policy):
-        saved = self.policy, self.added_blocks
-        self.policy = policy
+        saved = self.added_blocks
+        prevpolicy = self.bookkeeper.change_policy(policy)
         try:
             self.added_blocks = {}
             self.complete()
             # invoke annotation simplifications for the new blocks
             self.simplify(block_subset=self.added_blocks)
         finally:
-            self.policy, self.added_blocks = saved
+            self.bookkeeper.change_policy(prevpolicy)
+            self.added_blocks = saved
 
     def build_graph_types(self, flowgraph, inputcells, complete_now=True):
         checkgraph(flowgraph)
@@ -200,7 +200,7 @@
         """Process pending blocks until none is left."""
         while True:
             self.complete_pending_blocks()
-            self.policy.no_more_blocks_to_annotate(self)
+            self.bookkeeper.get_policy().no_more_blocks_to_annotate(self)
             if not self.pendingblocks:
                 break   # finished
         # make sure that the return variables of all graphs is annotated
diff --git a/rpython/annotator/bookkeeper.py b/rpython/annotator/bookkeeper.py
--- a/rpython/annotator/bookkeeper.py
+++ b/rpython/annotator/bookkeeper.py
@@ -9,7 +9,7 @@
 from rpython.flowspace.model import Constant
 from rpython.annotator.model import (SomeOrderedDict,
     SomeString, SomeChar, SomeFloat, unionof, SomeInstance, SomeDict,
-    SomeBuiltin, SomePBC, SomeInteger, TLS, SomeUnicodeCodePoint,
+    SomeBuiltin, SomePBC, SomeInteger, TLS, TlsClass, SomeUnicodeCodePoint,
     s_None, s_ImpossibleValue, SomeBool, SomeTuple,
     SomeImpossibleValue, SomeUnicodeString, SomeList, HarmlesslyBlocked,
     SomeWeakRef, SomeByteArray, SomeConstantType)
@@ -53,7 +53,7 @@
 
     def __init__(self, annotator):
         self.annotator = annotator
-        self.policy = annotator.policy
+        self.bkTLS = TlsClass()
         self.descs = {}          # map Python objects to their XxxDesc wrappers
         self.methoddescs = {}    # map (funcdesc, classdef) to the MethodDesc
         self.classdefs = []      # list of all ClassDefs
@@ -75,17 +75,31 @@
 
         delayed_imports()
 
+    def get_policy(self):
+        return getattr(self.bkTLS, 'policy', self.annotator.default_policy)
+
+    def change_policy(self, new_policy):
+        if new_policy is None:
+            return None
+        old_policy = self.get_policy()
+        self.bkTLS.policy = new_policy
+        return old_policy
+
+    @property
+    def position_key(self):
+        return self.bkTLS.position_key
+
     def enter(self, position_key):
         """Start of an operation.
         The operation is uniquely identified by the given key."""
         assert not hasattr(self, 'position_key'), "don't call enter() nestedly"
-        self.position_key = position_key
+        self.bkTLS.position_key = position_key
         TLS.bookkeeper = self
 
     def leave(self):
         """End of an operation."""
         del TLS.bookkeeper
-        del self.position_key
+        del self.bkTLS.position_key
 
     def compute_at_fixpoint(self):
         # getbookkeeper() needs to work during this function, so provide
@@ -570,7 +584,7 @@
         return self.annotator.whereami(self.position_key)
 
     def event(self, what, x):
-        return self.annotator.policy.event(self, what, x)
+        return self.get_policy().event(self, what, x)
 
     def warning(self, msg):
         return self.annotator.warning(msg)
diff --git a/rpython/annotator/description.py b/rpython/annotator/description.py
--- a/rpython/annotator/description.py
+++ b/rpython/annotator/description.py
@@ -276,7 +276,7 @@
             # get the specializer based on the tag of the 'pyobj'
             # (if any), according to the current policy
             tag = getattr(self.pyobj, '_annspecialcase_', None)
-            policy = self.bookkeeper.annotator.policy
+            policy = self.bookkeeper.get_policy()
             self.specializer = policy.get_specializer(tag)
         enforceargs = getattr(self.pyobj, '_annenforceargs_', None)
         signature = getattr(self.pyobj, '_signature_', None)
diff --git a/rpython/annotator/model.py b/rpython/annotator/model.py
--- a/rpython/annotator/model.py
+++ b/rpython/annotator/model.py
@@ -46,12 +46,11 @@
 STATE = State()
 
 try:
-    import thread
-    TLS = thread._local()
+    from thread import _local as TlsClass
 except ImportError:
-    class Tls(object):
+    class TlsClass(object):
         pass
-    TLS = Tls()
+TLS = TlsClass()
 
 
 class SomeObject(object):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to