[pypy-commit] pypy cpyext-ext: create a wrapper class to call os_thread.start_new_thread from PyThread_start_new_thread

mattip pypy.commits at gmail.com
Mon Mar 21 15:28:20 EDT 2016


Author: mattip <matti.picus at gmail.com>
Branch: cpyext-ext
Changeset: r83231:ce2053a9cdeb
Date: 2016-03-21 20:21 +0200
http://bitbucket.org/pypy/pypy/changeset/ce2053a9cdeb/

Log:	create a wrapper class to call os_thread.start_new_thread from
	PyThread_start_new_thread

diff --git a/pypy/module/cpyext/pystate.py b/pypy/module/cpyext/pystate.py
--- a/pypy/module/cpyext/pystate.py
+++ b/pypy/module/cpyext/pystate.py
@@ -55,12 +55,12 @@
     return 1
 
 thread_func = lltype.Ptr(lltype.FuncType([rffi.VOIDP], lltype.Void))
- at cpython_api([thread_func, rffi.VOIDP], rffi.INT_real, error=-1)
+ at cpython_api([thread_func, rffi.VOIDP], rffi.INT_real, error=-1, gil='release')
 def PyThread_start_new_thread(space, func, arg):
     from pypy.module.thread import os_thread
-    w_args = space.newtuple([arg])
-    XXX # How to wrap func as a space.callable ?
-    os_thread.start_new_thread(space, func, w_args)
+    w_args = space.newtuple([space.wrap(rffi.cast(lltype.Signed, arg)),])
+    w_func = os_thread.W_WrapThreadFunc(func)
+    os_thread.start_new_thread(space, w_func, w_args)
     return 0
 
 # XXX: might be generally useful
diff --git a/pypy/module/thread/os_thread.py b/pypy/module/thread/os_thread.py
--- a/pypy/module/thread/os_thread.py
+++ b/pypy/module/thread/os_thread.py
@@ -6,7 +6,9 @@
 from rpython.rlib import rthread
 from pypy.module.thread.error import wrap_thread_error
 from pypy.interpreter.error import OperationError, oefmt
-from pypy.interpreter.gateway import unwrap_spec, Arguments
+from pypy.interpreter.gateway import unwrap_spec, Arguments, interp2app
+from pypy.interpreter.baseobjspace import W_Root
+from pypy.interpreter.typedef import TypeDef
 
 # Here are the steps performed to start a new thread:
 #
@@ -161,6 +163,24 @@
     if w_threading is not None:
         space.call_method(w_threading, "_after_fork")
 
+class W_WrapThreadFunc(W_Root):
+    ''' Wrap a cpyext.pystate.thread_func, which
+        has the signature void func(void *)
+    '''
+    def __init__(self, func):
+        self.func = func
+
+    def descr_call(self, space, w_arg):
+        from rpython.rtyper.lltypesystem import rffi
+        try:
+            arg = rffi.cast(rffi.VOIDP, space.int_w(w_arg))
+            self.func(arg)
+        except Exception as e:
+            import pdb;pdb.set_trace()
+
+W_WrapThreadFunc.typedef = TypeDef("hiddenclass",
+    __call__ = interp2app(W_WrapThreadFunc.descr_call),
+)
 
 def start_new_thread(space, w_callable, w_args, w_kwargs=None):
     """Start a new thread and return its identifier.  The thread will call the


More information about the pypy-commit mailing list