Have a way to wait until a worker state changes.
[apps/madmutt.git] / lib-sys / evtloop.c
index 11546b2..8e1d361 100644 (file)
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
- *
- *  Copyright © 2006 Pierre Habouzit
+ */
+/*
+ *  Copyright © 2007 Pierre Habouzit
  */
 
+#include <pthread.h>
 #include <sys/epoll.h>
 #include <sys/socket.h>
 #ifndef EPOLLRDHUP
 #include "mutt.h"
 #include "mutt_ssl.li"
 
+DO_ARRAY_TYPE(job_t, job);
+
 static int epollfd = -1;
+static job_array jobs;
+static pthread_mutex_t el_mx;
+static pthread_cond_t el_cond;
+static pthread_t el_thread;
 
 static int el_job_setemode(job_t *w, el_mode emode)
 {
@@ -78,8 +86,38 @@ void job_wipe(job_t *w)
         gnutls_deinit(w->session);
 }
 
+static void job_arrau_dtor(job_t **j)
+{
+    if (*j)
+        IGNORE(el_job_release(*j, EL_KILLED));
+}
+
+DO_ARRAY_FUNCS(job_t, job, job_arrau_dtor);
+
+static void job_array_remove(job_array *arr, job_t *j)
+{
+    for (int i = 0; i < arr->len; i++) {
+        if (arr->arr[i] == j) {
+            job_array_take(arr, i);
+            break;
+        }
+    }
+}
+
+job_t *el_job_start(const machine_t *m, void *cfg)
+{
+    job_t *w = job_new();
+    w->m = m;
+    job_array_append(&jobs, w);
+    return m->setup(w, cfg) < 0 ? NULL : w;
+}
+
 int el_job_release(job_t *w, el_status reason)
 {
+    if (w->cond) {
+        pthread_cond_signal(&el_cond);
+        w->cond = false;
+    }
     w->state = EL_LLP_FINI;
     if (w->m && w->m->finalize) {
         w->m->finalize(w, reason);
@@ -89,6 +127,7 @@ int el_job_release(job_t *w, el_status reason)
             gnutls_bye(w->session, GNUTLS_SHUT_RDWR);
         close(w->fd);
     }
+    job_array_remove(&jobs, w);
     job_delete(&w);
     return -1;
 }
@@ -262,6 +301,16 @@ ssize_t el_job_write(job_t *w, buffer_t *buf)
     return nr;
 }
 
+void el_lock(void)
+{
+    pthread_mutex_lock(&el_mx);
+}
+
+void el_unlock(void)
+{
+    pthread_mutex_unlock(&el_mx);
+}
+
 int el_dispatch(int timeout)
 {
     struct epoll_event events[FD_SETSIZE];
@@ -274,11 +323,17 @@ int el_dispatch(int timeout)
         mutt_exit(EXIT_FAILURE);
     }
 
+    el_lock();
     while (--count >= 0) {
         job_t *w  = events[count].data.ptr;
         int event = events[count].events;
         int evt   = 0;
 
+        if (w->cond) {
+            pthread_cond_signal(&el_cond);
+            w->cond = false;
+        }
+        gettimeofday(&w->mru, NULL);
         switch (w->state) {
           case EL_LLP_INIT:
             w->llp(w);
@@ -303,6 +358,73 @@ int el_dispatch(int timeout)
             break;
         }
     }
+    el_unlock();
 
     return 0;
 }
+
+void el_wait(job_t *w)
+{
+    w->cond = true;
+    pthread_cond_wait(&el_cond, &el_mx);
+}
+
+static 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;
+
+        el_lock();
+        for (int i = jobs.len - 1; i >= 0; --i) {
+            job_t *w = jobs.arr[i];
+            if (timercmp(&now, &w->mru, >)) {
+                if (w->cond) {
+                    pthread_cond_signal(&el_cond);
+                    w->cond = false;
+                }
+                IGNORE(w->m->on_event(w, EL_EVT_WAKEUP));
+            }
+        }
+        el_unlock();
+    }
+}
+
+void el_initialize(void)
+{
+    pthread_mutexattr_t attr;
+
+    pthread_mutexattr_init(&attr);
+    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
+    pthread_mutex_init(&el_mx, &attr);
+    pthread_mutexattr_destroy(&attr);
+
+    gnutls_global_init();
+    epollfd = epoll_create(1024);
+    if (epollfd < 0) {
+        mutt_error("epoll_create");
+        mutt_exit(EXIT_FAILURE);
+    }
+    job_array_init(&jobs);
+    pthread_create(&el_thread, NULL, &el_loop, NULL);
+}
+
+void el_shutdown(void)
+{
+    pthread_cancel(el_thread);
+    pthread_join(el_thread, NULL);
+    job_array_wipe(&jobs);
+    close(epollfd);
+    gnutls_global_deinit();
+    pthread_mutex_destroy(&el_mx);
+}