Hi Raster,

This patch enables g_main_loop based glib integration without epoll.
Tested with elementary_test (normal, glib main non-epoll and glib main + epoll 
cases)

thanks,

Mike
>From ad10940e99e7114603cbe1e8c3badfa23e5e435f Mon Sep 17 00:00:00 2001
From: Mike McCormack <[email protected]>
Date: Wed, 6 Oct 2010 20:47:01 +0900
Subject: [PATCH] Make gmain integration work without epoll

---
 trunk/ecore/src/lib/ecore/ecore_main.c |  148 +++++++++++++++++++++++---------
 1 files changed, 107 insertions(+), 41 deletions(-)

diff --git a/trunk/ecore/src/lib/ecore/ecore_main.c b/trunk/ecore/src/lib/ecore/ecore_main.c
index de507da..c8597d8 100644
--- a/trunk/ecore/src/lib/ecore/ecore_main.c
+++ b/trunk/ecore/src/lib/ecore/ecore_main.c
@@ -74,6 +74,9 @@ struct _Ecore_Fd_Handler
    Eina_Bool                write_active : 1;
    Eina_Bool                error_active : 1;
    Eina_Bool                delete_me : 1;
+#if defined(USE_G_MAIN_LOOP) && !defined(HAVE_EPOLL)
+   GPollFD        gfd;
+#endif
 };
 
 #ifdef _WIN32
@@ -130,12 +133,12 @@ static double            t2 = 0.0;
 
 #ifdef HAVE_EPOLL
 static int epoll_fd = -1;
+static GPollFD ecore_epoll_fd;
 #endif
 
 #ifdef USE_G_MAIN_LOOP
-static GSource *ecore_epoll_source;
-static GPollFD ecore_epoll_fd;
-static guint ecore_epoll_id;
+static GSource *ecore_glib_source;
+static guint ecore_glib_source_id;
 static GMainLoop* ecore_main_loop;
 static gboolean ecore_idling;
 static gboolean ecore_fds_ready;
@@ -157,10 +160,21 @@ static inline int _ecore_poll_events_from_fdh(Ecore_Fd_Handler *fdh __UNUSED__)
 }
 #endif
 
-#ifdef HAVE_EPOLL
-static inline int _ecore_main_fdh_epoll_add(Ecore_Fd_Handler *fdh)
+#ifdef USE_G_MAIN_LOOP
+static inline int _gfd_events_from_fdh(Ecore_Fd_Handler *fdh)
+{
+   int events = 0;
+   if (fdh->flags & ECORE_FD_READ)  events |= G_IO_IN;
+   if (fdh->flags & ECORE_FD_WRITE) events |= G_IO_OUT;
+   if (fdh->flags & ECORE_FD_ERROR) events |= G_IO_ERR;
+   return events;
+}
+#endif
+
+static inline int _ecore_main_fdh_poll_add(Ecore_Fd_Handler *fdh)
 {
    int r = 0;
+#ifdef HAVE_EPOLL
    struct epoll_event ev;
 
    memset(&ev, 0, sizeof (ev));
@@ -168,18 +182,25 @@ static inline int _ecore_main_fdh_epoll_add(Ecore_Fd_Handler *fdh)
    ev.data.ptr = fdh;
    INF("adding poll on %d %08x", fdh->fd, ev.events);
    r = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fdh->fd, &ev);
-   return r;
-}
+#elif USE_G_MAIN_LOOP
+   fdh->gfd.fd = fdh->fd;
+   fdh->gfd.events = _gfd_events_from_fdh(fdh);
+   fdh->gfd.revents = 0;
+   INF("adding gpoll on %d %08x", fdh->fd, fdh->gfd.events);
+   g_source_add_poll(ecore_glib_source, &fdh->gfd);
 #else
-static inline int _ecore_main_fdh_epoll_add(Ecore_Fd_Handler *fdh __UNUSED__)
-{
-   return 0;
-}
+   if (!ECORE_MAGIC_CHECK(fdh, ECORE_MAGIC_FD_HANDLER))
+     {
+        ECORE_MAGIC_FAIL(fdh, ECORE_MAGIC_FD_HANDLER,
+                         "_ecore_main_fdh_poll_add");
+     }
 #endif
+   return r;
+}
 
-#ifdef HAVE_EPOLL
-static inline void _ecore_main_fdh_epoll_del(Ecore_Fd_Handler *fdh)
+static inline void _ecore_main_fdh_poll_del(Ecore_Fd_Handler *fdh)
 {
+#ifdef HAVE_EPOLL
    struct epoll_event ev;
 
    memset(&ev, 0, sizeof (ev));
@@ -190,17 +211,25 @@ static inline void _ecore_main_fdh_epoll_del(Ecore_Fd_Handler *fdh)
      {
         ERR("Failed to delete epoll fd %d! (errno=%d)", fdh->fd, errno);
      }
-}
+#elif USE_G_MAIN_LOOP
+   fdh->gfd.fd = fdh->fd;
+   fdh->gfd.events = _gfd_events_from_fdh(fdh);
+   fdh->gfd.revents = 0;
+   INF("adding gpoll on %d %08x", fdh->fd, fdh->gfd.events);
+   g_source_add_poll(ecore_glib_source, &fdh->gfd);
 #else
-static inline void _ecore_main_fdh_epoll_del(Ecore_Fd_Handler *fdh __UNUSED__)
-{
-}
+   if (!ECORE_MAGIC_CHECK(fdh, ECORE_MAGIC_FD_HANDLER))
+     {
+        ECORE_MAGIC_FAIL(fdh, ECORE_MAGIC_FD_HANDLER,
+                              "_ecore_main_fdh_poll_del");
+     }
 #endif
+}
 
-#ifdef HAVE_EPOLL
-static inline int _ecore_main_fdh_epoll_modify(Ecore_Fd_Handler *fdh)
+static inline int _ecore_main_fdh_poll_modify(Ecore_Fd_Handler *fdh)
 {
    int r = 0;
+#ifdef HAVE_EPOLL
    struct epoll_event ev;
 
    memset(&ev, 0, sizeof (ev));
@@ -208,17 +237,23 @@ static inline int _ecore_main_fdh_epoll_modify(Ecore_Fd_Handler *fdh)
    ev.data.ptr = fdh;
    INF("modifing epoll on %d to %08x", fdh->fd, ev.events);
    r = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fdh->fd, &ev);
-   return r;
-}
+#elif USE_G_MAIN_LOOP
+   fdh->gfd.fd = fdh->fd;
+   fdh->gfd.events = _gfd_events_from_fdh(fdh);
+   fdh->gfd.revents = 0;
+   INF("modifing gpoll on %d to %08x", fdh->fd, fdh->gfd.events);
 #else
-static inline int _ecore_main_fdh_epoll_modify(Ecore_Fd_Handler *fdh __UNUSED__)
-{
-   return 0;
-}
+   if (!ECORE_MAGIC_CHECK(fdh, ECORE_MAGIC_FD_HANDLER))
+     {
+        ECORE_MAGIC_FAIL(fdh, ECORE_MAGIC_FD_HANDLER,
+                              "_ecore_main_fdh_poll_modify");
+     }
 #endif
+   return r;
+}
 
 #ifdef HAVE_EPOLL
-static inline int _ecore_main_fdh_epoll_mark_active(void)
+static inline int _ecore_main_fdh_poll_mark_active(void)
 {
    struct epoll_event ev[32];
    int i, ret;
@@ -240,7 +275,7 @@ static inline int _ecore_main_fdh_epoll_mark_active(void)
         if (!ECORE_MAGIC_CHECK(fdh, ECORE_MAGIC_FD_HANDLER))
           {
              ECORE_MAGIC_FAIL(fdh, ECORE_MAGIC_FD_HANDLER,
-                              "_ecore_main_fdh_epoll_mark_active");
+                              "_ecore_main_fdh_poll_mark_active");
              continue;
           }
         if (fdh->delete_me)
@@ -258,6 +293,35 @@ static inline int _ecore_main_fdh_epoll_mark_active(void)
 
    return ret;
 }
+
+#elif USE_G_MAIN_LOOP
+
+static inline int _ecore_main_fdh_poll_mark_active(void)
+{
+   Ecore_Fd_Handler *fdh;
+   int ret = 0;
+
+   /* call the prepare callback for all handlers */
+   EINA_INLIST_FOREACH(fd_handlers, fdh)
+     {
+        if (fdh->delete_me)
+          continue;
+
+        if (fdh->gfd.revents & G_IO_IN)
+          fdh->read_active = 1;
+        if (fdh->gfd.revents & G_IO_OUT)
+          fdh->write_active = 1;
+        if (fdh->gfd.revents & G_IO_ERR)
+          fdh->error_active = 1;
+        if (fdh->gfd.revents & (G_IO_IN|G_IO_OUT|G_IO_ERR))
+          ret++;
+     }
+
+   INF("found %d active fds", ret);
+
+   return ret;
+}
+
 #endif
 
 #ifdef USE_G_MAIN_LOOP
@@ -315,7 +379,7 @@ _ecore_main_gsource_check(GSource *source)
    INF("enter");
    in_main_loop++;
 
-   ecore_fds_ready = (_ecore_main_fdh_epoll_mark_active() > 0);
+   ecore_fds_ready = (_ecore_main_fdh_poll_mark_active() > 0);
    _ecore_main_fd_handlers_cleanup();
 
    _ecore_time_loop_time = ecore_time_get();
@@ -418,17 +482,19 @@ _ecore_main_loop_init(void)
 #endif
 
 #ifdef USE_G_MAIN_LOOP
-   ecore_epoll_source = g_source_new(&ecore_gsource_funcs, sizeof (GSource));
-   if (!ecore_epoll_source)
+   ecore_glib_source = g_source_new(&ecore_gsource_funcs, sizeof (GSource));
+   if (!ecore_glib_source)
       CRIT("Failed to create glib source for epoll!");
    else
      {
+#ifdef HAVE_EPOLL
         ecore_epoll_fd.fd = epoll_fd;
         ecore_epoll_fd.events = G_IO_IN;
         ecore_epoll_fd.revents = 0;
-        g_source_add_poll(ecore_epoll_source, &ecore_epoll_fd);
-        ecore_epoll_id = g_source_attach(ecore_epoll_source, NULL);
-        if (ecore_epoll_id <= 0)
+        g_source_add_poll(ecore_glib_source, &ecore_epoll_fd);
+#endif
+        ecore_glib_source_id = g_source_attach(ecore_glib_source, NULL);
+        if (ecore_glib_source_id <= 0)
            CRIT("Failed to attach glib source to default context");
      }
 #endif
@@ -439,10 +505,10 @@ void
 _ecore_main_loop_shutdown(void)
 {
 #ifdef USE_G_MAIN_LOOP
-   if (ecore_epoll_source)
+   if (ecore_glib_source)
      {
-        g_source_destroy(ecore_epoll_source);
-        ecore_epoll_source = NULL;
+        g_source_destroy(ecore_glib_source);
+        ecore_glib_source = NULL;
      }
 #endif
 
@@ -611,9 +677,9 @@ ecore_main_fd_handler_add(int fd, Ecore_Fd_Handler_Flags flags, Ecore_Fd_Cb func
    ECORE_MAGIC_SET(fdh, ECORE_MAGIC_FD_HANDLER);
    fdh->fd = fd;
    fdh->flags = flags;
-   if (0 > _ecore_main_fdh_epoll_add(fdh))
+   if (0 > _ecore_main_fdh_poll_add(fdh))
      {
-        ERR("Failed to add epoll fd %d (errno = %d)!", fd, errno);
+        ERR("Failed to add poll on fd %d (errno = %d)!", fd, errno);
         free(fdh);
         return NULL;
      }
@@ -683,7 +749,7 @@ ecore_main_fd_handler_del(Ecore_Fd_Handler *fd_handler)
      }
    fd_handler->delete_me = 1;
    fd_handlers_delete_me = 1;
-   _ecore_main_fdh_epoll_del(fd_handler);
+   _ecore_main_fdh_poll_del(fd_handler);
    return fd_handler->data;
 }
 
@@ -782,7 +848,7 @@ ecore_main_fd_handler_active_set(Ecore_Fd_Handler *fd_handler, Ecore_Fd_Handler_
         return;
      }
    fd_handler->flags = flags;
-   if (0 > _ecore_main_fdh_epoll_modify(fd_handler))
+   if (0 > _ecore_main_fdh_poll_modify(fd_handler))
      {
        ERR("Failed to mod epoll fd %d!", fd_handler->fd);
      }
@@ -927,7 +993,7 @@ _ecore_main_select(double timeout)
    if (ret > 0)
      {
 #ifdef HAVE_EPOLL
-        _ecore_main_fdh_epoll_mark_active();
+        _ecore_main_fdh_poll_mark_active();
 #else /* HAVE_EPOLL */
         Ecore_Fd_Handler *fdh;
 
-- 
1.7.0.4

------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today.
http://p.sf.net/sfu/beautyoftheweb
_______________________________________________
enlightenment-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel

Reply via email to