Esempio n. 1
0
    def __init__(self, cfunc, callback):
        self.cfunc = cfunc
        self.callback = callback
        pointer = rffi.cast(rffi.VOIDP, clibffi.closureHeap.alloc())
        if cfunc.notready:
            cfunc.prepare_cif()
            cfunc.notready = False
        Mem.__init__(self, cfunc, pointer)

        closure_ptr = rffi.cast(clibffi.FFI_CLOSUREP, self.pointer)
        # hide the object
        gcref = rgc.cast_instance_to_gcref(self)
        raw = rgc.hide_nonmovable_gcref(gcref)
        unique_id = rffi.cast(rffi.VOIDP, raw)

        res = clibffi.c_ffi_prep_closure(closure_ptr, cfunc.cif.cif,
            invoke_callback, unique_id)

        if rffi.cast(lltype.Signed, res) != clibffi.FFI_OK:
            raise unwind(LError(u"libffi failed to build this callback"))
        if closure_ptr.c_user_data != unique_id:
            raise unwind(LError(u"ffi_prep_closure(): bad user_data"))

        # The function might be called in separate thread,
        # so allocate GIL here, just in case.
        rgil.allocate()
Esempio n. 2
0
def ll_start_new_thread(func):
    from rpython.rlib import rgil
    _check_thread_enabled()
    rgil.allocate()
    # ^^^ convenience: any RPython program which uses explicitly
    # rthread.start_new_thread() will initialize the GIL at that
    # point.
    ident = c_thread_start(func)
    if ident == -1:
        raise error("can't start new thread")
    return ident
Esempio n. 3
0
def ll_start_new_thread(func):
    from rpython.rlib import rgil
    _check_thread_enabled()
    rgil.allocate()
    # ^^^ convenience: any RPython program which uses explicitly
    # rthread.start_new_thread() will initialize the GIL at that
    # point.
    ident = c_thread_start(func)
    if ident == -1:
        raise error("can't start new thread")
    return ident
Esempio n. 4
0
 def setup_threads(self, space):
     """Enable threads in the object space, if they haven't already been."""
     if not self.gil_ready:
         # Note: this is a quasi-immutable read by module/pypyjit/interp_jit
         # It must be changed (to True) only if it was really False before
         rgil.allocate()
         self.gil_ready = True
         result = True
     else:
         result = False      # already set up
     return result
Esempio n. 5
0
def work(func, args):
    if not core.g.work_pool:
        # The function will be called in separate thread,
        # so allocate GIL here
        rgil.allocate()
        core.g.work_pool = WorkPool()

    req = lltype.malloc(uv.work_ptr.TO, flavor='raw', zero=True)
    work = Work(func, args)
    core.g.work_pool.push(req, work)
    try:
        response = uv_callback.after_work(req)
        response.wait(uv.queue_work(response.ec.uv_loop, req,
            work_cb, uv_callback.after_work.cb))
        if work.unwinder:
            raise work.unwinder
        return work.retval
    finally:
        core.g.work_pool.pop(req)
        lltype.free(req, flavor='raw')
Esempio n. 6
0
 def init(self):
     if not self._is_inited:
         self._lock = rthread.allocate_lock()
         self._is_inited = True
         rgil.allocate()
Esempio n. 7
0
 def init(self):
     if not self._is_inited:
         self._lock = rthread.allocate_lock()
         self._is_inited = True
         rgil.allocate()