[pypy-commit] pypy stacklet: Switch the RPython API to use real COpaquePtrs instead of just Signeds.

arigo noreply at buildbot.pypy.org
Sat Aug 6 12:31:10 CEST 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: stacklet
Changeset: r46306:32b3ac02d4ab
Date: 2011-08-05 19:54 +0200
http://bitbucket.org/pypy/pypy/changeset/32b3ac02d4ab/

Log:	Switch the RPython API to use real COpaquePtrs instead of just
	Signeds.

diff --git a/pypy/rlib/rstacklet.py b/pypy/rlib/rstacklet.py
--- a/pypy/rlib/rstacklet.py
+++ b/pypy/rlib/rstacklet.py
@@ -23,20 +23,22 @@
 
 # ----- types -----
 
-handle = lltype.Signed
-thread_handle = lltype.Signed
-run_fn = lltype.Ptr(lltype.FuncType([handle, lltype.Signed], handle))
+handle = rffi.COpaquePtr(typedef='stacklet_handle', compilation_info=eci)
+thread_handle = rffi.COpaquePtr(typedef='stacklet_thread_handle',
+                                compilation_info=eci)
+run_fn = lltype.Ptr(lltype.FuncType([handle, rffi.VOIDP], handle))
 
 # ----- constants -----
 
-EMPTY_STACK_HANDLE = -1
+def is_empty_handle(h):
+    return rffi.cast(lltype.Signed, h) == -1
 
 # ----- functions -----
 
 newthread = llexternal('stacklet_newthread', [], thread_handle)
 deletethread = llexternal('stacklet_deletethread',[thread_handle], lltype.Void)
 
-new = llexternal('stacklet_new', [thread_handle, run_fn, lltype.Signed],
+new = llexternal('stacklet_new', [thread_handle, run_fn, rffi.VOIDP],
                  handle)
 switch = llexternal('stacklet_switch', [thread_handle, handle], handle)
 destroy = llexternal('stacklet_destroy', [thread_handle, handle], lltype.Void)
diff --git a/pypy/rlib/test/test_rstacklet.py b/pypy/rlib/test/test_rstacklet.py
--- a/pypy/rlib/test/test_rstacklet.py
+++ b/pypy/rlib/test/test_rstacklet.py
@@ -1,5 +1,6 @@
 from pypy.rlib import rstacklet, rrandom
 from pypy.rlib.rarithmetic import intmask
+from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.translator.c.test.test_standalone import StandaloneTests
 
 
@@ -23,9 +24,10 @@
     @here_is_a_test
     def test_new(self):
         print 'start'
-        h = rstacklet.new(self.thrd, empty_callback, 123)
+        h = rstacklet.new(self.thrd, empty_callback,
+                          rffi.cast(rffi.VOIDP, 123))
         print 'end', h
-        assert h == -1
+        assert rstacklet.is_empty_handle(h)
 
     def nextstatus(self, nextvalue):
         print 'expected nextvalue to be %d, got %d' % (nextvalue,
@@ -36,13 +38,14 @@
     @here_is_a_test
     def test_simple_switch(self):
         self.status = 0
-        h = rstacklet.new(self.thrd, switchbackonce_callback, 321)
-        assert h != rstacklet.EMPTY_STACK_HANDLE
+        h = rstacklet.new(self.thrd, switchbackonce_callback,
+                          rffi.cast(rffi.VOIDP, 321))
+        assert not rstacklet.is_empty_handle(h)
         self.nextstatus(2)
         h = rstacklet.switch(runner.thrd, h)
         self.nextstatus(4)
         print 'end', h
-        assert h == rstacklet.EMPTY_STACK_HANDLE
+        assert rstacklet.is_empty_handle(h)
 
     @here_is_a_test
     def test_various_depths(self):
@@ -55,7 +58,7 @@
 
     def any_alive(self):
         for task in self.tasks:
-            if task.h != 0:
+            if task.h:
                 return True
         return False
 
@@ -63,7 +66,7 @@
 class Task:
     def __init__(self, n):
         self.n = n
-        self.h = 0
+        self.h = lltype.nullptr(rstacklet.handle.TO)
 
     def withdepth(self, d):
         if d > 0:
@@ -72,26 +75,27 @@
             res = 0
             n = intmask(runner.random.genrand32() % 10)
             if n == self.n or (runner.status >= STATUSMAX and
-                               runner.tasks[n].h == 0):
+                               not runner.tasks[n].h):
                 return 1
 
             print "status == %d, self.n = %d" % (runner.status, self.n)
-            assert self.h == 0
+            assert not self.h
             assert runner.nextstep == -1
             runner.status += 1
             runner.nextstep = runner.status
             runner.comefrom = self.n
             runner.gointo = n
             task = runner.tasks[n]
-            if task.h == 0:
+            if not task.h:
                 # start a new stacklet
                 print "NEW", n
-                h = rstacklet.new(runner.thrd, variousstackdepths_callback, n)
+                h = rstacklet.new(runner.thrd, variousstackdepths_callback,
+                                  rffi.cast(rffi.VOIDP, n))
             else:
                 # switch to this stacklet
                 print "switch to", n
                 h = task.h
-                task.h = 0
+                task.h = lltype.nullptr(rstacklet.handle.TO)
                 h = rstacklet.switch(runner.thrd, h)
 
             print "back in self.n = %d, coming from %d" % (self.n,
@@ -100,14 +104,14 @@
             runner.nextstep = -1
             assert runner.gointo == self.n
             assert runner.comefrom != self.n
-            assert self.h == 0
+            assert not self.h
             if runner.comefrom != -42:
                 assert 0 <= runner.comefrom < 10
                 task = runner.tasks[runner.comefrom]
-                assert task.h == 0
+                assert not task.h
                 task.h = h
             else:
-                assert h == rstacklet.EMPTY_STACK_HANDLE
+                assert rstacklet.is_empty_handle(h)
             runner.comefrom = -1
             runner.gointo = -1
         assert (res & (res-1)) == 0   # to prevent a tail-call to withdepth()
@@ -119,30 +123,31 @@
 
 def empty_callback(h, arg):
     print 'in empty_callback:', h, arg
-    assert arg == 123
+    assert rffi.cast(lltype.Signed, arg) == 123
     return h
 
 def switchbackonce_callback(h, arg):
     print 'in switchbackonce_callback:', h, arg
-    assert arg == 321
+    assert rffi.cast(lltype.Signed, arg) == 321
     runner.nextstatus(1)
-    assert h != rstacklet.EMPTY_STACK_HANDLE
+    assert not rstacklet.is_empty_handle(h)
     h = rstacklet.switch(runner.thrd, h)
     runner.nextstatus(3)
-    assert h != rstacklet.EMPTY_STACK_HANDLE
+    assert not rstacklet.is_empty_handle(h)
     return h
 
 def variousstackdepths_callback(h, arg):
     assert runner.nextstep == runner.status
     runner.nextstep = -1
+    arg = rffi.cast(lltype.Signed, arg)
     assert arg == runner.gointo
     self = runner.tasks[arg]
     assert self.n == runner.gointo
-    assert self.h == 0
+    assert not self.h
     assert 0 <= runner.comefrom < 10
     task = runner.tasks[runner.comefrom]
-    assert task.h == 0
-    assert h != 0 and h != rstacklet.EMPTY_STACK_HANDLE
+    assert not task.h
+    assert bool(h) and not rstacklet.is_empty_handle(h)
     task.h = h
     runner.comefrom = -1
     runner.gointo = -1
@@ -150,15 +155,15 @@
     while self.withdepth(runner.random.genrand32() % 20) == 0:
         pass
 
-    assert self.h == 0
+    assert not self.h
     while 1:
         n = intmask(runner.random.genrand32() % 10)
         h = runner.tasks[n].h
-        if h != 0:
+        if h:
             break
 
-    assert h != rstacklet.EMPTY_STACK_HANDLE
-    runner.tasks[n].h = 0
+    assert not rstacklet.is_empty_handle(h)
+    runner.tasks[n].h = lltype.nullptr(rstacklet.handle.TO)
     runner.comefrom = -42
     runner.gointo = n
     assert runner.nextstep == -1


More information about the pypy-commit mailing list