BReactor_glib.c 12 KB

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