You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

71 regels
1.8 KiB

  1. @cython.no_gc_clear
  2. cdef class UVIdle(UVHandle):
  3. cdef _init(self, Loop loop, Handle h):
  4. cdef int err
  5. self._start_init(loop)
  6. self._handle = <uv.uv_handle_t*>PyMem_RawMalloc(sizeof(uv.uv_idle_t))
  7. if self._handle is NULL:
  8. self._abort_init()
  9. raise MemoryError()
  10. err = uv.uv_idle_init(self._loop.uvloop, <uv.uv_idle_t*>self._handle)
  11. if err < 0:
  12. self._abort_init()
  13. raise convert_error(err)
  14. self._finish_init()
  15. self.h = h
  16. self.running = 0
  17. cdef inline stop(self):
  18. cdef int err
  19. if not self._is_alive():
  20. self.running = 0
  21. return
  22. if self.running == 1:
  23. err = uv.uv_idle_stop(<uv.uv_idle_t*>self._handle)
  24. self.running = 0
  25. if err < 0:
  26. exc = convert_error(err)
  27. self._fatal_error(exc, True)
  28. return
  29. cdef inline start(self):
  30. cdef int err
  31. self._ensure_alive()
  32. if self.running == 0:
  33. err = uv.uv_idle_start(<uv.uv_idle_t*>self._handle,
  34. cb_idle_callback)
  35. if err < 0:
  36. exc = convert_error(err)
  37. self._fatal_error(exc, True)
  38. return
  39. self.running = 1
  40. @staticmethod
  41. cdef UVIdle new(Loop loop, Handle h):
  42. cdef UVIdle handle
  43. handle = UVIdle.__new__(UVIdle)
  44. handle._init(loop, h)
  45. return handle
  46. cdef void cb_idle_callback(uv.uv_idle_t* handle) with gil:
  47. if __ensure_handle_data(<uv.uv_handle_t*>handle, "UVIdle callback") == 0:
  48. return
  49. cdef:
  50. UVIdle idle = <UVIdle> handle.data
  51. Handle h = idle.h
  52. try:
  53. h._run()
  54. except BaseException as ex:
  55. idle._error(ex, False)