Move the event loop to evtloop.c, and wake up sleeping jobs every 10 seconds.
[apps/madmutt.git] / lib-sys / evtloop.c
index 3ecfb57..a28c9b8 100644 (file)
@@ -17,6 +17,7 @@
  *  Copyright © 2006 Pierre Habouzit
  */
 
+#include <pthread.h>
 #include <sys/epoll.h>
 #include <sys/socket.h>
 #ifndef EPOLLRDHUP
@@ -309,6 +310,7 @@ int el_dispatch(int timeout)
         int event = events[count].events;
         int evt   = 0;
 
+        gettimeofday(&w->mru, NULL);
         switch (w->state) {
           case EL_LLP_INIT:
             w->llp(w);
@@ -337,6 +339,29 @@ int el_dispatch(int timeout)
     return 0;
 }
 
+void *el_loop(void *data)
+{
+    time_t sec = time(NULL);
+
+    for (;;) {
+        struct timeval now;
+
+        el_dispatch(100);
+        pthread_testcancel();
+
+        gettimeofday(&now, NULL);
+        if (sec >= now.tv_sec)
+            continue;
+        sec = now.tv_sec;
+        now.tv_sec -= 10;
+        for (int i = jobs.len - 1; i >= 0; --i) {
+            job_t *w = jobs.arr[i];
+            if (timercmp(&now, &w->mru, >))
+                IGNORE(w->m->on_event(w, EL_EVT_WAKEUP));
+        }
+    }
+}
+
 void el_initialize(void)
 {
     gnutls_global_init();