| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- /**
- * @file BReactor_glib.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <stdlib.h>
- #include <string.h>
- #include <misc/offset.h>
- #include <base/BLog.h>
- #include "BReactor_glib.h"
- #include <generated/blog_channel_BReactor.h>
- struct fd_source {
- GSource source;
- BFileDescriptor *bfd;
- };
- static void dispatch_pending (BReactor *o)
- {
- while (!o->exiting && BPendingGroup_HasJobs(&o->pending_jobs)) {
- BPendingGroup_ExecuteJob(&o->pending_jobs);
- }
- }
- static void reset_limits (BReactor *o)
- {
- LinkedList1Node *list_node;
- while (list_node = LinkedList1_GetFirst(&o->active_limits_list)) {
- BReactorLimit *limit = UPPER_OBJECT(list_node, BReactorLimit, active_limits_list_node);
- ASSERT(limit->count > 0)
- limit->count = 0;
- LinkedList1_Remove(&o->active_limits_list, &limit->active_limits_list_node);
- }
- }
- static gushort get_glib_wait_events (int ev)
- {
- gushort gev = G_IO_ERR | G_IO_HUP;
-
- if (ev & BREACTOR_READ) {
- gev |= G_IO_IN;
- }
-
- if (ev & BREACTOR_WRITE) {
- gev |= G_IO_OUT;
- }
-
- return gev;
- }
- static int get_fd_dispatchable_events (BFileDescriptor *bfd)
- {
- ASSERT(bfd->active)
-
- int ev = 0;
-
- if ((bfd->waitEvents & BREACTOR_READ) && (bfd->pollfd.revents & G_IO_IN)) {
- ev |= BREACTOR_READ;
- }
-
- if ((bfd->waitEvents & BREACTOR_WRITE) && (bfd->pollfd.revents & G_IO_OUT)) {
- ev |= BREACTOR_WRITE;
- }
-
- if ((bfd->pollfd.revents & G_IO_ERR) || (bfd->pollfd.revents & G_IO_HUP)) {
- ev |= BREACTOR_ERROR;
- }
-
- return ev;
- }
- static gboolean timer_source_handler (gpointer data)
- {
- BTimer *bt = (void *)data;
- BReactor *reactor = bt->reactor;
- ASSERT(bt->active)
-
- if (reactor->exiting) {
- return FALSE;
- }
-
- g_source_destroy(bt->source);
- g_source_unref(bt->source);
- bt->active = 0;
- DebugCounter_Decrement(&reactor->d_timers_ctr);
-
- bt->handler(bt->handler_pointer);
- dispatch_pending(reactor);
- reset_limits(reactor);
-
- return FALSE;
- }
- static gboolean fd_source_func_prepare (GSource *source, gint *timeout)
- {
- BFileDescriptor *bfd = ((struct fd_source *)source)->bfd;
- ASSERT(bfd->active)
- ASSERT(bfd->source == source)
-
- *timeout = -1;
- return FALSE;
- }
- static gboolean fd_source_func_check (GSource *source)
- {
- BFileDescriptor *bfd = ((struct fd_source *)source)->bfd;
- ASSERT(bfd->active)
- ASSERT(bfd->source == source)
-
- return (get_fd_dispatchable_events(bfd) ? TRUE : FALSE);
- }
- static gboolean fd_source_func_dispatch (GSource *source, GSourceFunc callback, gpointer user_data)
- {
- BFileDescriptor *bfd = ((struct fd_source *)source)->bfd;
- BReactor *reactor = bfd->reactor;
- ASSERT(bfd->active)
- ASSERT(bfd->source == source)
-
- if (reactor->exiting) {
- return TRUE;
- }
-
- int events = get_fd_dispatchable_events(bfd);
- if (!events) {
- return TRUE;
- }
-
- bfd->handler(bfd->user, events);
- dispatch_pending(reactor);
- reset_limits(reactor);
-
- return TRUE;
- }
- void BTimer_Init (BTimer *bt, btime_t msTime, BTimer_handler handler, void *user)
- {
- bt->msTime = msTime;
- bt->handler = handler;
- bt->handler_pointer = user;
- bt->active = 0;
- }
- int BTimer_IsRunning (BTimer *bt)
- {
- ASSERT(bt->active == 0 || bt->active == 1)
-
- return bt->active;
- }
- void BFileDescriptor_Init (BFileDescriptor *bs, int fd, BFileDescriptor_handler handler, void *user)
- {
- bs->fd = fd;
- bs->handler = handler;
- bs->user = user;
- bs->active = 0;
- }
- int BReactor_Init (BReactor *bsys)
- {
- return BReactor_InitFromExistingGMainLoop(bsys, g_main_loop_new(NULL, FALSE), 1);
- }
- void BReactor_Free (BReactor *bsys)
- {
- DebugObject_Free(&bsys->d_obj);
- DebugCounter_Free(&bsys->d_timers_ctr);
- DebugCounter_Free(&bsys->d_limits_ctr);
- DebugCounter_Free(&bsys->d_fds_counter);
- ASSERT(!BPendingGroup_HasJobs(&bsys->pending_jobs))
- ASSERT(LinkedList1_IsEmpty(&bsys->active_limits_list))
-
- // free job queue
- BPendingGroup_Free(&bsys->pending_jobs);
-
- // unref main loop if needed
- if (bsys->unref_gloop_on_free) {
- g_main_loop_unref(bsys->gloop);
- }
- }
- int BReactor_Exec (BReactor *bsys)
- {
- DebugObject_Access(&bsys->d_obj);
-
- // dispatch pending jobs (until exiting) and reset limits
- dispatch_pending(bsys);
- reset_limits(bsys);
-
- // if exiting, do not enter glib loop
- if (bsys->exiting) {
- return bsys->exit_code;
- }
-
- // enter glib loop
- g_main_loop_run(bsys->gloop);
-
- ASSERT(bsys->exiting)
-
- return bsys->exit_code;
- }
- void BReactor_Quit (BReactor *bsys, int code)
- {
- DebugObject_Access(&bsys->d_obj);
-
- // remember exiting
- bsys->exiting = 1;
- bsys->exit_code = code;
-
- // request termination of glib loop
- g_main_loop_quit(bsys->gloop);
- }
- void BReactor_SetTimer (BReactor *bsys, BTimer *bt)
- {
- BReactor_SetTimerAfter(bsys, bt, bt->msTime);
- }
- void BReactor_SetTimerAfter (BReactor *bsys, BTimer *bt, btime_t after)
- {
- BReactor_SetTimerAbsolute(bsys, bt, btime_add(btime_gettime(), after));
- }
- void BReactor_SetTimerAbsolute (BReactor *bsys, BTimer *bt, btime_t time)
- {
- DebugObject_Access(&bsys->d_obj);
-
- // remove timer if it's already set
- BReactor_RemoveTimer(bsys, bt);
-
- // set active and reactor
- bt->active = 1;
- bt->reactor = bsys;
-
- // calculate relative time
- btime_t now = btime_gettime();
- btime_t relTime = (time < now ? 0 : time - now);
-
- // init source
- bt->source = g_timeout_source_new(relTime);
- g_source_set_callback(bt->source, timer_source_handler, bt, NULL);
- g_source_attach(bt->source, g_main_loop_get_context(bsys->gloop));
-
- DebugCounter_Increment(&bsys->d_timers_ctr);
- }
- void BReactor_RemoveTimer (BReactor *bsys, BTimer *bt)
- {
- DebugObject_Access(&bsys->d_obj);
-
- // do nothing if timer is not active
- if (!bt->active) {
- return;
- }
-
- // free source
- g_source_destroy(bt->source);
- g_source_unref(bt->source);
-
- // set not active
- bt->active = 0;
-
- DebugCounter_Decrement(&bsys->d_timers_ctr);
- }
- BPendingGroup * BReactor_PendingGroup (BReactor *bsys)
- {
- DebugObject_Access(&bsys->d_obj);
-
- return &bsys->pending_jobs;
- }
- int BReactor_Synchronize (BReactor *bsys, BSmallPending *ref)
- {
- DebugObject_Access(&bsys->d_obj);
- ASSERT(ref)
-
- while (!bsys->exiting) {
- ASSERT(BPendingGroup_HasJobs(&bsys->pending_jobs))
-
- if (BPendingGroup_PeekJob(&bsys->pending_jobs) == ref) {
- return 1;
- }
-
- BPendingGroup_ExecuteJob(&bsys->pending_jobs);
- }
-
- return 0;
- }
- int BReactor_AddFileDescriptor (BReactor *bsys, BFileDescriptor *bs)
- {
- DebugObject_Access(&bsys->d_obj);
- ASSERT(!bs->active)
-
- // set active, no wait events, and set reactor
- bs->active = 1;
- bs->waitEvents = 0;
- bs->reactor = bsys;
-
- // create source
- bs->source = g_source_new(&bsys->fd_source_funcs, sizeof(struct fd_source));
- ((struct fd_source *)bs->source)->bfd = bs;
-
- // init pollfd
- bs->pollfd.fd = bs->fd;
- bs->pollfd.events = get_glib_wait_events(bs->waitEvents);
- bs->pollfd.revents = 0;
-
- // start source
- g_source_add_poll(bs->source, &bs->pollfd);
- g_source_attach(bs->source, g_main_loop_get_context(bsys->gloop));
-
- DebugCounter_Increment(&bsys->d_fds_counter);
- return 1;
- }
- void BReactor_RemoveFileDescriptor (BReactor *bsys, BFileDescriptor *bs)
- {
- DebugObject_Access(&bsys->d_obj);
- DebugCounter_Decrement(&bsys->d_fds_counter);
- ASSERT(bs->active)
-
- // free source
- g_source_destroy(bs->source);
- g_source_unref(bs->source);
-
- // set not active
- bs->active = 0;
- }
- void BReactor_SetFileDescriptorEvents (BReactor *bsys, BFileDescriptor *bs, int events)
- {
- DebugObject_Access(&bsys->d_obj);
- ASSERT(bs->active)
- ASSERT(!(events&~(BREACTOR_READ|BREACTOR_WRITE)))
-
- // set new wait events
- bs->waitEvents = events;
-
- // update pollfd wait events
- bs->pollfd.events = get_glib_wait_events(bs->waitEvents);
- }
- int BReactor_InitFromExistingGMainLoop (BReactor *bsys, GMainLoop *gloop, int unref_gloop_on_free)
- {
- ASSERT(gloop)
- ASSERT(unref_gloop_on_free == !!unref_gloop_on_free)
-
- // set not exiting
- bsys->exiting = 0;
-
- // set gloop and unref on free flag
- bsys->gloop = gloop;
- bsys->unref_gloop_on_free = unref_gloop_on_free;
-
- // init fd source functions table
- memset(&bsys->fd_source_funcs, 0, sizeof(bsys->fd_source_funcs));
- bsys->fd_source_funcs.prepare = fd_source_func_prepare;
- bsys->fd_source_funcs.check = fd_source_func_check;
- bsys->fd_source_funcs.dispatch = fd_source_func_dispatch;
- bsys->fd_source_funcs.finalize = NULL;
-
- // init job queue
- BPendingGroup_Init(&bsys->pending_jobs);
-
- // init active limits list
- LinkedList1_Init(&bsys->active_limits_list);
-
- DebugCounter_Init(&bsys->d_fds_counter);
- DebugCounter_Init(&bsys->d_limits_ctr);
- DebugCounter_Init(&bsys->d_timers_ctr);
- DebugObject_Init(&bsys->d_obj);
- return 1;
- }
- GMainLoop * BReactor_GetGMainLoop (BReactor *bsys)
- {
- DebugObject_Access(&bsys->d_obj);
-
- return bsys->gloop;
- }
- int BReactor_SynchronizeAll (BReactor *bsys)
- {
- DebugObject_Access(&bsys->d_obj);
-
- dispatch_pending(bsys);
-
- return !bsys->exiting;
- }
- void BReactorLimit_Init (BReactorLimit *o, BReactor *reactor, int limit)
- {
- DebugObject_Access(&reactor->d_obj);
- ASSERT(limit > 0)
-
- // init arguments
- o->reactor = reactor;
- o->limit = limit;
-
- // set count zero
- o->count = 0;
-
- DebugCounter_Increment(&reactor->d_limits_ctr);
- DebugObject_Init(&o->d_obj);
- }
- void BReactorLimit_Free (BReactorLimit *o)
- {
- BReactor *reactor = o->reactor;
- DebugObject_Free(&o->d_obj);
- DebugCounter_Decrement(&reactor->d_limits_ctr);
-
- // remove from active limits list
- if (o->count > 0) {
- LinkedList1_Remove(&reactor->active_limits_list, &o->active_limits_list_node);
- }
- }
- int BReactorLimit_Increment (BReactorLimit *o)
- {
- BReactor *reactor = o->reactor;
- DebugObject_Access(&o->d_obj);
-
- // check count against limit
- if (o->count >= o->limit) {
- return 0;
- }
-
- // increment count
- o->count++;
-
- // if limit was zero, add to active limits list
- if (o->count == 1) {
- LinkedList1_Append(&reactor->active_limits_list, &o->active_limits_list_node);
- }
-
- return 1;
- }
- void BReactorLimit_SetLimit (BReactorLimit *o, int limit)
- {
- DebugObject_Access(&o->d_obj);
- ASSERT(limit > 0)
-
- // set limit
- o->limit = limit;
- }
|