[pypy-svn] r18737 - pypy/extradoc/sprintinfo/paris

arigo at codespeak.net arigo at codespeak.net
Tue Oct 18 14:30:47 CEST 2005

Author: arigo
Date: Tue Oct 18 14:30:47 2005
New Revision: 18737

tentative RPython interface to stackless frame chains.

Modified: pypy/extradoc/sprintinfo/paris/stackless-discussion.txt
--- pypy/extradoc/sprintinfo/paris/stackless-discussion.txt	(original)
+++ pypy/extradoc/sprintinfo/paris/stackless-discussion.txt	Tue Oct 18 14:30:47 2005
@@ -83,3 +83,63 @@
 This approach appears to give us full continuations for free.
+Exposing an interface to RPython
+Here is a tentative design of a minimal interface that might be good
+enough to implement, say, app-level greenlets or tasklets as a mixed
+We introduce an RPython type ``continuation`` and a built-in function
+``yield_continuation()`` that work as follows (see example below):
+* The built-in function ``yield_continuation()`` causes the current
+  function's state to be captured in a new ``continuation`` object that
+  is returned to the parent.  Only one frame, the current one, is
+  captured this way.  The current frame is suspended and the caller
+  continues to run.
+* A ``continuation`` object can be jumped to by calling its ``switch()``
+  method with no argument.
+* ``yield_continuation()`` and ``switch()`` themselves return a new
+  ``continuation`` object: the freshly captured state of the caller of
+  the source ``switch()`` that was just executed, or None in the case
+  described below.
+* ``yield_continuation()`` only works when called from a function whose
+  return type is precisely ``continuation``.  In other words this
+  function must also contain a regular return statement that returns a
+  ``continuation``.  When the captured continuation is later resumed and
+  the function eventually returns via this regular return statement,
+  then the returned ``continuation`` object designates the place where
+  execution should switch to (i.e. execution does not return to the
+  original caller).  In such an implicit switch, a None is returned to
+  the ``switch()`` or ``yield_continuation()`` at place that we switch
+  to.
+* every continuation must be resumed once and only once.  Not resuming
+  it at all causes a leak.  Resuming it several times causes a crash.
+The following example would print the numbers from 1 to 7 in order::
+    def g():
+        print 2
+        cont_before_5 = yield_continuation()
+        print 4
+        cont_before_7 = cont_before_5.switch()
+        print 6
+        return cont_before_7
+    def f():
+        print 1
+        cont_before_4 = g()
+        print 3
+        cont_before_6 = cont_before_4.switch()
+        print 5
+        cont_after_return = cont_before_6.switch()
+        print 7
+        assert cont_after_return is None
+    f()

More information about the Pypy-commit mailing list