summaryrefslogtreecommitdiff
path: root/venv/lib/python3.11/site-packages/uvloop/handles/async_.pyx
blob: 5c740cfeb9c97339f2e2c0de840bec2cac2dd9c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@cython.no_gc_clear
cdef class UVAsync(UVHandle):
    cdef _init(self, Loop loop, method_t callback, object ctx):
        cdef int err

        self._start_init(loop)

        self._handle = <uv.uv_handle_t*>PyMem_RawMalloc(sizeof(uv.uv_async_t))
        if self._handle is NULL:
            self._abort_init()
            raise MemoryError()

        err = uv.uv_async_init(self._loop.uvloop,
                               <uv.uv_async_t*>self._handle,
                               __uvasync_callback)
        if err < 0:
            self._abort_init()
            raise convert_error(err)

        self._finish_init()

        self.callback = callback
        self.ctx = ctx

    cdef send(self):
        cdef int err

        self._ensure_alive()

        err = uv.uv_async_send(<uv.uv_async_t*>self._handle)
        if err < 0:
            exc = convert_error(err)
            self._fatal_error(exc, True)
            return

    @staticmethod
    cdef UVAsync new(Loop loop, method_t callback, object ctx):
        cdef UVAsync handle
        handle = UVAsync.__new__(UVAsync)
        handle._init(loop, callback, ctx)
        return handle


cdef void __uvasync_callback(
    uv.uv_async_t* handle,
) noexcept with gil:
    if __ensure_handle_data(<uv.uv_handle_t*>handle, "UVAsync callback") == 0:
        return

    cdef:
        UVAsync async_ = <UVAsync> handle.data
        method_t cb = async_.callback
    try:
        cb(async_.ctx)
    except BaseException as ex:
        async_._error(ex, False)