BReactor_emscripten.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @file BReactor_emscripten.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include <inttypes.h>
  31. #include <emscripten/emscripten.h>
  32. #include "BReactor_emscripten.h"
  33. #include <misc/debug.h>
  34. #include <generated/blog_channel_BReactor.h>
  35. static void assert_timer (BTimer *bt, BReactor *reactor)
  36. {
  37. ASSERT(bt->active == 0 || bt->active == 1);
  38. ASSERT(bt->active == 0 || bt->reactor);
  39. ASSERT(bt->active == 0 || (!reactor || bt->reactor == reactor));
  40. }
  41. static void dispatch_pending (BReactor *o)
  42. {
  43. while (BPendingGroup_HasJobs(&o->pending_jobs)) {
  44. BPendingGroup_ExecuteJob(&o->pending_jobs);
  45. }
  46. }
  47. __attribute__((used))
  48. void breactor_timer_cb (BReactor *reactor, BTimer *bt)
  49. {
  50. assert_timer(bt, reactor);
  51. ASSERT(bt->active);
  52. ASSERT(!BPendingGroup_HasJobs(&reactor->pending_jobs));
  53. bt->active = 0;
  54. bt->handler(bt->handler_pointer);
  55. dispatch_pending(reactor);
  56. }
  57. static void small_timer_handler (void *vbt)
  58. {
  59. BSmallTimer *bt = vbt;
  60. bt->handler(bt);
  61. }
  62. void BTimer_Init (BTimer *bt, btime_t msTime, BTimer_handler handler, void *user)
  63. {
  64. bt->msTime = msTime;
  65. bt->handler = handler;
  66. bt->handler_pointer = user;
  67. bt->active = 0;
  68. }
  69. int BTimer_IsRunning (BTimer *bt)
  70. {
  71. assert_timer(bt, NULL);
  72. return bt->active;
  73. }
  74. void BSmallTimer_Init (BSmallTimer *bt, BSmallTimer_handler handler)
  75. {
  76. BTimer_Init(&bt->timer, 0, small_timer_handler, bt);
  77. bt->handler = handler;
  78. }
  79. int BSmallTimer_IsRunning (BSmallTimer *bt)
  80. {
  81. return BTimer_IsRunning(&bt->timer);
  82. }
  83. void BReactor_EmscriptenInit (BReactor *bsys)
  84. {
  85. BPendingGroup_Init(&bsys->pending_jobs);
  86. DebugObject_Init(&bsys->d_obj);
  87. }
  88. void BReactor_EmscriptenFree (BReactor *bsys)
  89. {
  90. DebugObject_Free(&bsys->d_obj);
  91. ASSERT(!BPendingGroup_HasJobs(&bsys->pending_jobs));
  92. }
  93. void BReactor_EmscriptenSync (BReactor *bsys)
  94. {
  95. DebugObject_Access(&bsys->d_obj);
  96. dispatch_pending(bsys);
  97. }
  98. BPendingGroup * BReactor_PendingGroup (BReactor *bsys)
  99. {
  100. DebugObject_Access(&bsys->d_obj);
  101. return &bsys->pending_jobs;
  102. }
  103. void BReactor_SetTimer (BReactor *bsys, BTimer *bt)
  104. {
  105. BReactor_SetTimerAfter(bsys, bt, bt->msTime);
  106. }
  107. void BReactor_SetTimerAfter (BReactor *bsys, BTimer *bt, btime_t after)
  108. {
  109. DebugObject_Access(&bsys->d_obj);
  110. assert_timer(bt, bsys);
  111. if (bt->active) {
  112. BReactor_RemoveTimer(bsys, bt);
  113. }
  114. char cmd[120];
  115. sprintf(cmd, "setTimeout(function(){Module.ccall('breactor_timer_cb','null',['number','number'],[%d,%d]);},%"PRIi64")", (int)bsys, (int)bt, after);
  116. bt->active = 1;
  117. bt->timerid = emscripten_run_script_int(cmd);
  118. bt->reactor = bsys;
  119. }
  120. void BReactor_RemoveTimer (BReactor *bsys, BTimer *bt)
  121. {
  122. DebugObject_Access(&bsys->d_obj);
  123. assert_timer(bt, bsys);
  124. if (!bt->active) {
  125. return;
  126. }
  127. char cmd[30];
  128. sprintf(cmd, "clearTimeout(%d)", bt->timerid);
  129. emscripten_run_script(cmd);
  130. bt->active = 0;
  131. }
  132. void BReactor_SetSmallTimer (BReactor *bsys, BSmallTimer *bt, int mode, btime_t time)
  133. {
  134. ASSERT(mode == BTIMER_SET_RELATIVE)
  135. BReactor_SetTimerAfter(bsys, &bt->timer, time);
  136. }
  137. void BReactor_RemoveSmallTimer (BReactor *bsys, BSmallTimer *bt)
  138. {
  139. BReactor_RemoveTimer(bsys, &bt->timer);
  140. }