EventBase.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "rust/cjdns_sys/Rffi.h"
  16. #include "util/events/libuv/UvWrapper.h"
  17. #include "memory/Allocator.h"
  18. #include "util/events/libuv/EventBase_pvt.h"
  19. #include "util/Assert.h"
  20. #include "util/Identity.h"
  21. #include "util/Js.h"
  22. Js({ require("../util/events/libuv/libuv.js")(builder, js); })
  23. #ifdef win32
  24. #include <sys/timeb.h>
  25. #include <time.h>
  26. #else
  27. #include <sys/time.h>
  28. #endif
  29. static int onFree2(struct EventBase_pvt* ctx)
  30. {
  31. if (!uv_is_closing((uv_handle_t*) &ctx->uvAwakener)) {
  32. uv_close((uv_handle_t*) &ctx->uvAwakener, NULL);
  33. }
  34. if (!uv_is_closing((uv_handle_t*) &ctx->blockTimer)) {
  35. uv_close((uv_handle_t*) &ctx->blockTimer, NULL);
  36. }
  37. uv_loop_delete(ctx->loop);
  38. Allocator_free(ctx->alloc);
  39. return 0;
  40. }
  41. static int onFree(struct Allocator_OnFreeJob* job)
  42. {
  43. struct EventBase_pvt* ctx = Identity_check((struct EventBase_pvt*) job->userData);
  44. ctx->userAlloc = NULL;
  45. Rffi_stopEventLoop(ctx->rffi_loop);
  46. if (!ctx->running) {
  47. if (Rffi_eventLoopRefCtr(ctx->rffi_loop)) {
  48. // We cycle the loop once in order to let Rffi tear down
  49. EventBase_beginLoop(&ctx->pub);
  50. } else {
  51. onFree2(ctx);
  52. }
  53. } else if (ctx->running == 2) {
  54. // Don't allow a request to quit when we are freeing
  55. ctx->running = 1;
  56. } else {
  57. // Running and not freeing, continue...
  58. }
  59. return 0;
  60. }
  61. static void calibrateTime(struct EventBase_pvt* base)
  62. {
  63. uint64_t seconds;
  64. uint64_t milliseconds;
  65. #ifdef win32
  66. struct _timeb tb;
  67. _ftime(&tb);
  68. seconds = tb.time;
  69. milliseconds = tb.millitm;
  70. #else // sane operating systems.
  71. struct timeval tv;
  72. gettimeofday(&tv, NULL);
  73. seconds = tv.tv_sec;
  74. milliseconds = tv.tv_usec / 1000;
  75. #endif
  76. base->baseTime = (seconds * 1000) + milliseconds - uv_now(base->loop);
  77. }
  78. static void uvAwakener(uv_async_t* handle, int status)
  79. {
  80. struct EventBase_pvt* base = Identity_containerOf(handle, struct EventBase_pvt, uvAwakener);
  81. if (base->running == 2) {
  82. uv_stop(base->loop);
  83. }
  84. // title says it all
  85. // printf("[%d] Do nothing\n", getpid());
  86. }
  87. static void blockTimer(uv_timer_t* timer, int status)
  88. {
  89. //printf("[%d] blockTimer\n", getpid());
  90. }
  91. struct EventBase* EventBase_new(struct Allocator* allocator)
  92. {
  93. struct Allocator* loopAlloc = Allocator_new(1<<24); // 4MB
  94. struct EventBase_pvt* base = Allocator_calloc(loopAlloc, sizeof(struct EventBase_pvt), 1);
  95. base->loop = uv_loop_new();
  96. base->rffi_loop = Rffi_mkEventLoop(loopAlloc, &base->pub);
  97. uv_async_init(base->loop, &base->uvAwakener, uvAwakener);
  98. uv_unref((uv_handle_t*) &base->uvAwakener);
  99. uv_timer_init(base->loop, &base->blockTimer);
  100. Assert_true(Rffi_eventLoopRefCtr(base->rffi_loop) == 0);
  101. base->alloc = loopAlloc;
  102. base->userAlloc = allocator;
  103. Identity_set(base);
  104. Allocator_onFree(allocator, onFree, base);
  105. calibrateTime(base);
  106. return &base->pub;
  107. }
  108. void EventBase_beginLoop(struct EventBase* eventBase)
  109. {
  110. struct EventBase_pvt* ctx = Identity_check((struct EventBase_pvt*) eventBase);
  111. Assert_true(!ctx->running); // double begin
  112. ctx->running = 1;
  113. do {
  114. uv_timer_start(&ctx->blockTimer, blockTimer, 1, 0);
  115. // start the loop.
  116. uv_run(ctx->loop, UV_RUN_DEFAULT);
  117. if (ctx->userAlloc == NULL) {
  118. // Freeing = only exit if no more events left (they are being terminated)
  119. if (Rffi_eventLoopRefCtr(ctx->rffi_loop) == 0) {
  120. break;
  121. }
  122. } else if (ctx->running == 2) {
  123. // Not freeing, request to exit
  124. break;
  125. } else if (Rffi_eventLoopRefCtr(ctx->rffi_loop) == 0) {
  126. // Not freeing, no request to exit, cycle until no events left in Rust
  127. break;
  128. }
  129. } while (1);
  130. ctx->running = 0;
  131. if (ctx->userAlloc == NULL) {
  132. Assert_true(Rffi_eventLoopRefCtr(ctx->rffi_loop) == 0);
  133. onFree2(ctx);
  134. }
  135. }
  136. void EventBase_endLoop(struct EventBase* eventBase)
  137. {
  138. struct EventBase_pvt* ctx = Identity_check((struct EventBase_pvt*) eventBase);
  139. // End loop is a no-op when freeing, the loop will end when all events are shutdown.
  140. if (ctx->running == 0 || ctx->userAlloc == NULL) { return; }
  141. ctx->running = 2;
  142. uv_async_send(&ctx->uvAwakener);
  143. }
  144. void EventBase_wakeup(void* eventBase)
  145. {
  146. struct EventBase_pvt* ctx = Identity_check((struct EventBase_pvt*) eventBase);
  147. uv_async_send(&ctx->uvAwakener);
  148. }
  149. static void countCallback(uv_handle_t* event, void* vEventCount)
  150. {
  151. int* eventCount = (int*) vEventCount;
  152. if (!uv_is_closing(event)) {
  153. *eventCount = *eventCount + 1;
  154. }
  155. }
  156. int EventBase_eventCount(struct EventBase* eventBase)
  157. {
  158. int eventCount = 0;
  159. struct EventBase_pvt* ctx = Identity_check((struct EventBase_pvt*) eventBase);
  160. uv_walk(ctx->loop, countCallback, &eventCount);
  161. return eventCount;
  162. }
  163. struct EventBase_pvt* EventBase_privatize(struct EventBase* base)
  164. {
  165. return Identity_check((struct EventBase_pvt*) base);
  166. }