Commits

Armin Rigo  committed 802adbb

Debugging code. Needs to be enabled explicitly for now.

  • Participants
  • Parent commits 962c280

Comments (0)

Files changed (1)

File pypy/rlib/rstacklet.py

 from pypy.rlib import _rffi_stacklet as _c
 from pypy.rpython.lltypesystem import lltype, llmemory
 
+DEBUG = False
+
 
 class StackletThread(object):
 
         if not self._thrd:
             raise MemoryError
         self._thrd_deleter = StackletThreadDeleter(self._thrd)
+        if DEBUG:
+            assert debug.sthread is None, "multithread debug support missing"
+            debug.sthread = self
 
     def new(self, callback, arg=llmemory.NULL):
-        return self._gcrootfinder.new(self, callback, arg)
+        if DEBUG:
+            callback = _debug_wrapper(callback)
+        h = self._gcrootfinder.new(self, callback, arg)
+        if DEBUG:
+            debug.add(h)
+        return h
     new._annspecialcase_ = 'specialize:arg(1)'
 
     def switch(self, stacklet):
-        return self._gcrootfinder.switch(self, stacklet)
+        if DEBUG:
+            debug.remove(stacklet)
+        h = self._gcrootfinder.switch(self, stacklet)
+        if DEBUG:
+            debug.add(h)
+        return h
 
     def destroy(self, stacklet):
+        if DEBUG:
+            debug.remove(stacklet)
         self._gcrootfinder.destroy(self, stacklet)
 
     def is_empty_handle(self, stacklet):
                         None, None, ['__doc__'])
     return module.gcrootfinder
 _getgcrootfinder._annspecialcase_ = 'specialize:memo'
+
+
+class StackletDebugError(Exception):
+    pass
+
+class Debug(object):
+    def __init__(self):
+        self.sthread = None
+        self.active = []
+    def _freeze_(self):
+        self.__init__()
+        return False
+    def add(self, h):
+        if not self.sthread.is_empty_handle(h):
+            self.active.append(h)
+    def remove(self, h):
+        try:
+            i = self.active.index(h)
+        except ValueError:
+            raise StackletDebugError
+        del self.active[i]
+debug = Debug()
+
+def _debug_wrapper(callback):
+    def wrapper(h, arg):
+        debug.add(h)
+        h = callback(h, arg)
+        debug.remove(h)
+        return h
+    return wrapper
+_debug_wrapper._annspecialcase_ = 'specialize:memo'