X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=lib-sys%2Fevtloop.c;h=a28c9b872844b6fddb6b4a9bdc405b0763bad2c9;hb=852725f1307961ba31e4f1470d8b834e4b7a6754;hp=4b2f0d84e65ea9b56e767647f58c839797cd0128;hpb=f8d1899d44b259a0920a52bd7290f15e39fd0d47;p=apps%2Fmadmutt.git diff --git a/lib-sys/evtloop.c b/lib-sys/evtloop.c index 4b2f0d8..a28c9b8 100644 --- a/lib-sys/evtloop.c +++ b/lib-sys/evtloop.c @@ -17,6 +17,7 @@ * Copyright © 2006 Pierre Habouzit */ +#include #include #include #ifndef EPOLLRDHUP @@ -31,7 +32,10 @@ #include "mutt.h" #include "mutt_ssl.li" +DO_ARRAY_TYPE(job_t, job); + static int epollfd = -1; +static job_array jobs; static int el_job_setemode(job_t *w, el_mode emode) { @@ -78,6 +82,32 @@ 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) { w->state = EL_LLP_FINI; @@ -89,6 +119,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; } @@ -279,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); @@ -307,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(); @@ -315,10 +370,12 @@ void el_initialize(void) mutt_error("epoll_create"); mutt_exit(EXIT_FAILURE); } + job_array_init(&jobs); } void el_shutdown(void) { + job_array_wipe(&jobs); close(epollfd); gnutls_global_deinit(); }