BReactor_glib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /**
  2. * @file BReactor_glib.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 <stdlib.h>
  30. #include <string.h>
  31. #include <misc/offset.h>
  32. #include <base/BLog.h>
  33. #include "BReactor_glib.h"
  34. #include <generated/blog_channel_BReactor.h>
  35. struct fd_source {
  36. GSource source;
  37. BFileDescriptor *bfd;
  38. };
  39. static void assert_timer (BSmallTimer *bt)
  40. {
  41. ASSERT(bt->is_small == 0 || bt->is_small == 1)
  42. ASSERT(bt->active == 0 || bt->active == 1)
  43. ASSERT(!bt->active || bt->reactor)
  44. ASSERT(!bt->active || bt->source)
  45. }
  46. static void dispatch_pending (BReactor *o)
  47. {
  48. while (!o->exiting && BPendingGroup_HasJobs(&o->pending_jobs)) {
  49. BPendingGroup_ExecuteJob(&o->pending_jobs);
  50. }
  51. }
  52. static void reset_limits (BReactor *o)
  53. {
  54. LinkedList1Node *list_node;
  55. while (list_node = LinkedList1_GetFirst(&o->active_limits_list)) {
  56. BReactorLimit *limit = UPPER_OBJECT(list_node, BReactorLimit, active_limits_list_node);
  57. ASSERT(limit->count > 0)
  58. limit->count = 0;
  59. LinkedList1_Remove(&o->active_limits_list, &limit->active_limits_list_node);
  60. }
  61. }
  62. static gushort get_glib_wait_events (int ev)
  63. {
  64. gushort gev = G_IO_ERR | G_IO_HUP;
  65. if (ev & BREACTOR_READ) {
  66. gev |= G_IO_IN;
  67. }
  68. if (ev & BREACTOR_WRITE) {
  69. gev |= G_IO_OUT;
  70. }
  71. return gev;
  72. }
  73. static int get_fd_dispatchable_events (BFileDescriptor *bfd)
  74. {
  75. ASSERT(bfd->active)
  76. int ev = 0;
  77. if ((bfd->waitEvents & BREACTOR_READ) && (bfd->pollfd.revents & G_IO_IN)) {
  78. ev |= BREACTOR_READ;
  79. }
  80. if ((bfd->waitEvents & BREACTOR_WRITE) && (bfd->pollfd.revents & G_IO_OUT)) {
  81. ev |= BREACTOR_WRITE;
  82. }
  83. if ((bfd->pollfd.revents & G_IO_ERR)) {
  84. ev |= BREACTOR_ERROR;
  85. }
  86. if ((bfd->pollfd.revents & G_IO_HUP)) {
  87. ev |= BREACTOR_HUP;
  88. }
  89. return ev;
  90. }
  91. static gboolean timer_source_handler (gpointer data)
  92. {
  93. BSmallTimer *bt = (void *)data;
  94. assert_timer(bt);
  95. ASSERT(bt->active)
  96. BReactor *reactor = bt->reactor;
  97. if (reactor->exiting) {
  98. return FALSE;
  99. }
  100. g_source_destroy(bt->source);
  101. g_source_unref(bt->source);
  102. bt->active = 0;
  103. DebugCounter_Decrement(&reactor->d_timers_ctr);
  104. if (bt->is_small) {
  105. bt->handler.smalll(bt);
  106. } else {
  107. BTimer *btimer = UPPER_OBJECT(bt, BTimer, base);
  108. bt->handler.heavy(btimer->user);
  109. }
  110. dispatch_pending(reactor);
  111. reset_limits(reactor);
  112. return FALSE;
  113. }
  114. static gboolean fd_source_func_prepare (GSource *source, gint *timeout)
  115. {
  116. BFileDescriptor *bfd = ((struct fd_source *)source)->bfd;
  117. ASSERT(bfd->active)
  118. ASSERT(bfd->source == source)
  119. *timeout = -1;
  120. return FALSE;
  121. }
  122. static gboolean fd_source_func_check (GSource *source)
  123. {
  124. BFileDescriptor *bfd = ((struct fd_source *)source)->bfd;
  125. ASSERT(bfd->active)
  126. ASSERT(bfd->source == source)
  127. return (get_fd_dispatchable_events(bfd) ? TRUE : FALSE);
  128. }
  129. static gboolean fd_source_func_dispatch (GSource *source, GSourceFunc callback, gpointer user_data)
  130. {
  131. BFileDescriptor *bfd = ((struct fd_source *)source)->bfd;
  132. BReactor *reactor = bfd->reactor;
  133. ASSERT(bfd->active)
  134. ASSERT(bfd->source == source)
  135. if (reactor->exiting) {
  136. return TRUE;
  137. }
  138. int events = get_fd_dispatchable_events(bfd);
  139. if (!events) {
  140. return TRUE;
  141. }
  142. bfd->handler(bfd->user, events);
  143. dispatch_pending(reactor);
  144. reset_limits(reactor);
  145. return TRUE;
  146. }
  147. void BSmallTimer_Init (BSmallTimer *bt, BSmallTimer_handler handler)
  148. {
  149. bt->handler.smalll = handler;
  150. bt->active = 0;
  151. bt->is_small = 1;
  152. }
  153. int BSmallTimer_IsRunning (BSmallTimer *bt)
  154. {
  155. assert_timer(bt);
  156. return bt->active;
  157. }
  158. void BTimer_Init (BTimer *bt, btime_t msTime, BTimer_handler handler, void *user)
  159. {
  160. bt->base.handler.heavy = handler;
  161. bt->base.active = 0;
  162. bt->base.is_small = 0;
  163. bt->user = user;
  164. bt->msTime = msTime;
  165. }
  166. int BTimer_IsRunning (BTimer *bt)
  167. {
  168. return BSmallTimer_IsRunning(&bt->base);
  169. }
  170. void BFileDescriptor_Init (BFileDescriptor *bs, int fd, BFileDescriptor_handler handler, void *user)
  171. {
  172. bs->fd = fd;
  173. bs->handler = handler;
  174. bs->user = user;
  175. bs->active = 0;
  176. }
  177. int BReactor_Init (BReactor *bsys)
  178. {
  179. return BReactor_InitFromExistingGMainLoop(bsys, g_main_loop_new(NULL, FALSE), 1);
  180. }
  181. void BReactor_Free (BReactor *bsys)
  182. {
  183. DebugObject_Free(&bsys->d_obj);
  184. DebugCounter_Free(&bsys->d_timers_ctr);
  185. DebugCounter_Free(&bsys->d_limits_ctr);
  186. DebugCounter_Free(&bsys->d_fds_counter);
  187. ASSERT(!BPendingGroup_HasJobs(&bsys->pending_jobs))
  188. ASSERT(LinkedList1_IsEmpty(&bsys->active_limits_list))
  189. // free job queue
  190. BPendingGroup_Free(&bsys->pending_jobs);
  191. // unref main loop if needed
  192. if (bsys->unref_gloop_on_free) {
  193. g_main_loop_unref(bsys->gloop);
  194. }
  195. }
  196. int BReactor_Exec (BReactor *bsys)
  197. {
  198. DebugObject_Access(&bsys->d_obj);
  199. // dispatch pending jobs (until exiting) and reset limits
  200. dispatch_pending(bsys);
  201. reset_limits(bsys);
  202. // if exiting, do not enter glib loop
  203. if (bsys->exiting) {
  204. return bsys->exit_code;
  205. }
  206. // enter glib loop
  207. g_main_loop_run(bsys->gloop);
  208. ASSERT(bsys->exiting)
  209. return bsys->exit_code;
  210. }
  211. void BReactor_Quit (BReactor *bsys, int code)
  212. {
  213. DebugObject_Access(&bsys->d_obj);
  214. // remember exiting
  215. bsys->exiting = 1;
  216. bsys->exit_code = code;
  217. // request termination of glib loop
  218. g_main_loop_quit(bsys->gloop);
  219. }
  220. void BReactor_SetSmallTimer (BReactor *bsys, BSmallTimer *bt, int mode, btime_t time)
  221. {
  222. DebugObject_Access(&bsys->d_obj);
  223. assert_timer(bt);
  224. // remove timer if it's already set
  225. BReactor_RemoveSmallTimer(bsys, bt);
  226. // if mode is absolute, subtract current time
  227. if (mode == BTIMER_SET_ABSOLUTE) {
  228. btime_t now = btime_gettime();
  229. time = (time < now ? 0 : time - now);
  230. }
  231. // set active and reactor
  232. bt->active = 1;
  233. bt->reactor = bsys;
  234. // init source
  235. bt->source = g_timeout_source_new(time);
  236. g_source_set_callback(bt->source, timer_source_handler, bt, NULL);
  237. g_source_attach(bt->source, g_main_loop_get_context(bsys->gloop));
  238. DebugCounter_Increment(&bsys->d_timers_ctr);
  239. }
  240. void BReactor_RemoveSmallTimer (BReactor *bsys, BSmallTimer *bt)
  241. {
  242. DebugObject_Access(&bsys->d_obj);
  243. assert_timer(bt);
  244. // do nothing if timer is not active
  245. if (!bt->active) {
  246. return;
  247. }
  248. // free source
  249. g_source_destroy(bt->source);
  250. g_source_unref(bt->source);
  251. // set not active
  252. bt->active = 0;
  253. DebugCounter_Decrement(&bsys->d_timers_ctr);
  254. }
  255. void BReactor_SetTimer (BReactor *bsys, BTimer *bt)
  256. {
  257. BReactor_SetSmallTimer(bsys, &bt->base, BTIMER_SET_RELATIVE, bt->msTime);
  258. }
  259. void BReactor_SetTimerAfter (BReactor *bsys, BTimer *bt, btime_t after)
  260. {
  261. BReactor_SetSmallTimer(bsys, &bt->base, BTIMER_SET_RELATIVE, after);
  262. }
  263. void BReactor_SetTimerAbsolute (BReactor *bsys, BTimer *bt, btime_t time)
  264. {
  265. BReactor_SetSmallTimer(bsys, &bt->base, BTIMER_SET_ABSOLUTE, time);
  266. }
  267. void BReactor_RemoveTimer (BReactor *bsys, BTimer *bt)
  268. {
  269. return BReactor_RemoveSmallTimer(bsys, &bt->base);
  270. }
  271. BPendingGroup * BReactor_PendingGroup (BReactor *bsys)
  272. {
  273. DebugObject_Access(&bsys->d_obj);
  274. return &bsys->pending_jobs;
  275. }
  276. int BReactor_Synchronize (BReactor *bsys, BSmallPending *ref)
  277. {
  278. DebugObject_Access(&bsys->d_obj);
  279. ASSERT(ref)
  280. while (!bsys->exiting) {
  281. ASSERT(BPendingGroup_HasJobs(&bsys->pending_jobs))
  282. if (BPendingGroup_PeekJob(&bsys->pending_jobs) == ref) {
  283. return 1;
  284. }
  285. BPendingGroup_ExecuteJob(&bsys->pending_jobs);
  286. }
  287. return 0;
  288. }
  289. int BReactor_AddFileDescriptor (BReactor *bsys, BFileDescriptor *bs)
  290. {
  291. DebugObject_Access(&bsys->d_obj);
  292. ASSERT(!bs->active)
  293. // set active, no wait events, and set reactor
  294. bs->active = 1;
  295. bs->waitEvents = 0;
  296. bs->reactor = bsys;
  297. // create source
  298. bs->source = g_source_new(&bsys->fd_source_funcs, sizeof(struct fd_source));
  299. ((struct fd_source *)bs->source)->bfd = bs;
  300. // init pollfd
  301. bs->pollfd.fd = bs->fd;
  302. bs->pollfd.events = get_glib_wait_events(bs->waitEvents);
  303. bs->pollfd.revents = 0;
  304. // start source
  305. g_source_add_poll(bs->source, &bs->pollfd);
  306. g_source_attach(bs->source, g_main_loop_get_context(bsys->gloop));
  307. DebugCounter_Increment(&bsys->d_fds_counter);
  308. return 1;
  309. }
  310. void BReactor_RemoveFileDescriptor (BReactor *bsys, BFileDescriptor *bs)
  311. {
  312. DebugObject_Access(&bsys->d_obj);
  313. DebugCounter_Decrement(&bsys->d_fds_counter);
  314. ASSERT(bs->active)
  315. // free source
  316. g_source_destroy(bs->source);
  317. g_source_unref(bs->source);
  318. // set not active
  319. bs->active = 0;
  320. }
  321. void BReactor_SetFileDescriptorEvents (BReactor *bsys, BFileDescriptor *bs, int events)
  322. {
  323. DebugObject_Access(&bsys->d_obj);
  324. ASSERT(bs->active)
  325. ASSERT(!(events&~(BREACTOR_READ|BREACTOR_WRITE)))
  326. // set new wait events
  327. bs->waitEvents = events;
  328. // update pollfd wait events
  329. bs->pollfd.events = get_glib_wait_events(bs->waitEvents);
  330. }
  331. int BReactor_InitFromExistingGMainLoop (BReactor *bsys, GMainLoop *gloop, int unref_gloop_on_free)
  332. {
  333. ASSERT(gloop)
  334. ASSERT(unref_gloop_on_free == !!unref_gloop_on_free)
  335. // set not exiting
  336. bsys->exiting = 0;
  337. // set gloop and unref on free flag
  338. bsys->gloop = gloop;
  339. bsys->unref_gloop_on_free = unref_gloop_on_free;
  340. // init fd source functions table
  341. memset(&bsys->fd_source_funcs, 0, sizeof(bsys->fd_source_funcs));
  342. bsys->fd_source_funcs.prepare = fd_source_func_prepare;
  343. bsys->fd_source_funcs.check = fd_source_func_check;
  344. bsys->fd_source_funcs.dispatch = fd_source_func_dispatch;
  345. bsys->fd_source_funcs.finalize = NULL;
  346. // init job queue
  347. BPendingGroup_Init(&bsys->pending_jobs);
  348. // init active limits list
  349. LinkedList1_Init(&bsys->active_limits_list);
  350. DebugCounter_Init(&bsys->d_fds_counter);
  351. DebugCounter_Init(&bsys->d_limits_ctr);
  352. DebugCounter_Init(&bsys->d_timers_ctr);
  353. DebugObject_Init(&bsys->d_obj);
  354. return 1;
  355. }
  356. GMainLoop * BReactor_GetGMainLoop (BReactor *bsys)
  357. {
  358. DebugObject_Access(&bsys->d_obj);
  359. return bsys->gloop;
  360. }
  361. int BReactor_SynchronizeAll (BReactor *bsys)
  362. {
  363. DebugObject_Access(&bsys->d_obj);
  364. dispatch_pending(bsys);
  365. return !bsys->exiting;
  366. }
  367. void BReactorLimit_Init (BReactorLimit *o, BReactor *reactor, int limit)
  368. {
  369. DebugObject_Access(&reactor->d_obj);
  370. ASSERT(limit > 0)
  371. // init arguments
  372. o->reactor = reactor;
  373. o->limit = limit;
  374. // set count zero
  375. o->count = 0;
  376. DebugCounter_Increment(&reactor->d_limits_ctr);
  377. DebugObject_Init(&o->d_obj);
  378. }
  379. void BReactorLimit_Free (BReactorLimit *o)
  380. {
  381. BReactor *reactor = o->reactor;
  382. DebugObject_Free(&o->d_obj);
  383. DebugCounter_Decrement(&reactor->d_limits_ctr);
  384. // remove from active limits list
  385. if (o->count > 0) {
  386. LinkedList1_Remove(&reactor->active_limits_list, &o->active_limits_list_node);
  387. }
  388. }
  389. int BReactorLimit_Increment (BReactorLimit *o)
  390. {
  391. BReactor *reactor = o->reactor;
  392. DebugObject_Access(&o->d_obj);
  393. // check count against limit
  394. if (o->count >= o->limit) {
  395. return 0;
  396. }
  397. // increment count
  398. o->count++;
  399. // if limit was zero, add to active limits list
  400. if (o->count == 1) {
  401. LinkedList1_Append(&reactor->active_limits_list, &o->active_limits_list_node);
  402. }
  403. return 1;
  404. }
  405. void BReactorLimit_SetLimit (BReactorLimit *o, int limit)
  406. {
  407. DebugObject_Access(&o->d_obj);
  408. ASSERT(limit > 0)
  409. // set limit
  410. o->limit = limit;
  411. }