選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

90 行
2.4 KiB

  1. @cython.no_gc_clear
  2. cdef class UVTimer(UVHandle):
  3. cdef _init(self, Loop loop, method_t callback, object ctx,
  4. uint64_t timeout):
  5. cdef int err
  6. self._start_init(loop)
  7. self._handle = <uv.uv_handle_t*> PyMem_RawMalloc(sizeof(uv.uv_timer_t))
  8. if self._handle is NULL:
  9. self._abort_init()
  10. raise MemoryError()
  11. err = uv.uv_timer_init(self._loop.uvloop, <uv.uv_timer_t*>self._handle)
  12. if err < 0:
  13. self._abort_init()
  14. raise convert_error(err)
  15. self._finish_init()
  16. self.callback = callback
  17. self.ctx = ctx
  18. self.running = 0
  19. self.timeout = timeout
  20. self.start_t = 0
  21. cdef stop(self):
  22. cdef int err
  23. if not self._is_alive():
  24. self.running = 0
  25. return
  26. if self.running == 1:
  27. err = uv.uv_timer_stop(<uv.uv_timer_t*>self._handle)
  28. self.running = 0
  29. if err < 0:
  30. exc = convert_error(err)
  31. self._fatal_error(exc, True)
  32. return
  33. cdef start(self):
  34. cdef int err
  35. self._ensure_alive()
  36. if self.running == 0:
  37. # Update libuv internal time.
  38. uv.uv_update_time(self._loop.uvloop) # void
  39. self.start_t = uv.uv_now(self._loop.uvloop)
  40. err = uv.uv_timer_start(<uv.uv_timer_t*>self._handle,
  41. __uvtimer_callback,
  42. self.timeout, 0)
  43. if err < 0:
  44. exc = convert_error(err)
  45. self._fatal_error(exc, True)
  46. return
  47. self.running = 1
  48. cdef get_when(self):
  49. return self.start_t + self.timeout
  50. @staticmethod
  51. cdef UVTimer new(Loop loop, method_t callback, object ctx,
  52. uint64_t timeout):
  53. cdef UVTimer handle
  54. handle = UVTimer.__new__(UVTimer)
  55. handle._init(loop, callback, ctx, timeout)
  56. return handle
  57. cdef void __uvtimer_callback(
  58. uv.uv_timer_t* handle,
  59. ) noexcept with gil:
  60. if __ensure_handle_data(<uv.uv_handle_t*>handle, "UVTimer callback") == 0:
  61. return
  62. cdef:
  63. UVTimer timer = <UVTimer> handle.data
  64. method_t cb = timer.callback
  65. timer.running = 0
  66. try:
  67. cb(timer.ctx)
  68. except BaseException as ex:
  69. timer._error(ex, False)