EventBase.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "util/events/libuv/UvWrapper.h"
  16. #include "memory/Allocator.h"
  17. #include "util/events/libuv/EventBase_pvt.h"
  18. #include "util/Assert.h"
  19. #include "util/Identity.h"
  20. #include "util/Js.h"
  21. Js({ require("../util/events/libuv/libuv.js")(builder, js); })
  22. #ifdef win32
  23. #include <sys/timeb.h>
  24. #include <time.h>
  25. #else
  26. #include <sys/time.h>
  27. #endif
  28. static int onFree(struct Allocator_OnFreeJob* job)
  29. {
  30. struct EventBase_pvt* ctx = Identity_check((struct EventBase_pvt*) job->userData);
  31. if (ctx->running) {
  32. // The job will be completed in EventLoop_beginLoop()
  33. ctx->onFree = job;
  34. EventBase_endLoop((struct EventBase*) ctx);
  35. return Allocator_ONFREE_ASYNC;
  36. } else {
  37. uv_loop_delete(ctx->loop);
  38. return 0;
  39. }
  40. }
  41. static void calibrateTime(struct EventBase_pvt* base)
  42. {
  43. uint64_t seconds;
  44. uint64_t milliseconds;
  45. #ifdef win32
  46. struct _timeb tb;
  47. _ftime(&tb);
  48. seconds = tb.time;
  49. milliseconds = tb.millitm;
  50. #else // sane operating systems.
  51. struct timeval tv;
  52. gettimeofday(&tv, NULL);
  53. seconds = tv.tv_sec;
  54. milliseconds = tv.tv_usec / 1000;
  55. #endif
  56. base->baseTime = (seconds * 1000) + milliseconds - uv_now(base->loop);
  57. }
  58. struct EventBase* EventBase_new(struct Allocator* allocator)
  59. {
  60. struct Allocator* alloc = Allocator_child(allocator);
  61. struct EventBase_pvt* base = Allocator_calloc(alloc, sizeof(struct EventBase_pvt), 1);
  62. base->loop = uv_loop_new();
  63. base->alloc = alloc;
  64. Identity_set(base);
  65. Allocator_onFree(alloc, onFree, base);
  66. calibrateTime(base);
  67. return &base->pub;
  68. }
  69. void EventBase_beginLoop(struct EventBase* eventBase)
  70. {
  71. struct EventBase_pvt* ctx = Identity_check((struct EventBase_pvt*) eventBase);
  72. Assert_true(!ctx->running); // double begin
  73. ctx->running = 1;
  74. // start the loop.
  75. uv_run(ctx->loop, UV_RUN_DEFAULT);
  76. ctx->running = 0;
  77. if (ctx->onFree) {
  78. uv_loop_delete(ctx->loop);
  79. Allocator_onFreeComplete(ctx->onFree);
  80. return;
  81. }
  82. }
  83. void EventBase_endLoop(struct EventBase* eventBase)
  84. {
  85. struct EventBase_pvt* ctx = Identity_check((struct EventBase_pvt*) eventBase);
  86. uv_stop(ctx->loop);
  87. }
  88. static void countCallback(uv_handle_t* event, void* vEventCount)
  89. {
  90. int* eventCount = (int*) vEventCount;
  91. if (!uv_is_closing(event)) {
  92. *eventCount = *eventCount + 1;
  93. }
  94. }
  95. int EventBase_eventCount(struct EventBase* eventBase)
  96. {
  97. int eventCount = 0;
  98. struct EventBase_pvt* ctx = Identity_check((struct EventBase_pvt*) eventBase);
  99. uv_walk(ctx->loop, countCallback, &eventCount);
  100. return eventCount;
  101. }
  102. struct EventBase_pvt* EventBase_privatize(struct EventBase* base)
  103. {
  104. return Identity_check((struct EventBase_pvt*) base);
  105. }