[pypy-commit] pypy cffi-static-callback-embedding: hg merge ec-keepalive

arigo pypy.commits at gmail.com
Mon Jan 4 11:33:34 EST 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-static-callback-embedding
Changeset: r81552:a7b258839a6c
Date: 2016-01-04 16:33 +0000
http://bitbucket.org/pypy/pypy/changeset/a7b258839a6c/

Log:	hg merge ec-keepalive

diff --git a/pypy/module/thread/threadlocals.py b/pypy/module/thread/threadlocals.py
--- a/pypy/module/thread/threadlocals.py
+++ b/pypy/module/thread/threadlocals.py
@@ -1,4 +1,5 @@
-from rpython.rlib import rthread, rweaklist
+import weakref
+from rpython.rlib import rthread, rshrinklist
 from rpython.rlib.objectmodel import we_are_translated
 from rpython.rlib.rarithmetic import r_ulonglong
 from pypy.module.thread.error import wrap_thread_error
@@ -14,8 +15,6 @@
     a thread finishes.  This works as long as the thread was started by
     os_thread.bootstrap()."""
 
-    _next_generation = r_ulonglong(0)
-
     def __init__(self, space):
         "NOT_RPYTHON"
         #
@@ -86,8 +85,7 @@
         # explicitly, so we return False.
         if self._weaklist is None:
             self._weaklist = ListECWrappers()
-            self._weaklist.initialize()
-        self._weaklist.add_handle(AutoFreeECWrapper(self, ec))
+        self._weaklist.append(weakref.ref(AutoFreeECWrapper(ec)))
         self._set_ec(ec, register_in_valuedict=False)
         return False
 
@@ -150,15 +148,11 @@
         # self._valuedict have priority because they are never
         # outdated.
         result = {}
-        generations = {}
-        for h in self._weaklist.get_all_handles():
+        for h in self._weaklist.items():
             wrapper = h()
-            if wrapper is not None:
-                key = wrapper.ident
-                prev = generations.get(key, r_ulonglong(0))
-                if wrapper.generation > prev:   # implies '.generation != 0'
-                    generations[key] = wrapper.generation
-                    result[key] = wrapper.ec
+            if wrapper is not None and not wrapper.deleted:
+                result[wrapper.ident] = wrapper.ec
+                # ^^ this possibly overwrites an older ec
         result.update(self._valuedict)
         return result
 
@@ -177,12 +171,11 @@
 
 
 class AutoFreeECWrapper(object):
+    deleted = False
 
-    def __init__(self, threadlocals, ec):
+    def __init__(self, ec):
         # this makes a loop between 'self' and 'ec'.  It should not prevent
         # the __del__ method here from being called.
-        threadlocals._next_generation += 1
-        self.generation = threadlocals._next_generation
         self.ec = ec
         ec._threadlocals_auto_free = self
         self.ident = rthread.get_ident()
@@ -193,8 +186,9 @@
         # referenced by 'self.ec' has finished at that point, and
         # we're just after the GC which finds no more references to
         # 'ec' (and thus to 'self').
-        self.generation = r_ulonglong(0)
+        self.deleted = True
         thread_is_stopping(self.ec)
 
-class ListECWrappers(rweaklist.RWeakListMixin):
-    pass
+class ListECWrappers(rshrinklist.AbstractShrinkList):
+    def must_keep(self, wref):
+        return wref() is not None


More information about the pypy-commit mailing list