From: Pierre Habouzit Date: Sun, 13 Jan 2008 11:09:59 +0000 (+0100) Subject: Move the threading stuff in the evtloop module. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=5d91980585aec03b90e77d650f6853aaf9d2dfc5;p=apps%2Fmadmutt.git Move the threading stuff in the evtloop module. Signed-off-by: Pierre Habouzit --- diff --git a/lib-sys/evtloop.c b/lib-sys/evtloop.c index a28c9b8..1d7e252 100644 --- a/lib-sys/evtloop.c +++ b/lib-sys/evtloop.c @@ -13,8 +13,9 @@ * 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 @@ -36,6 +37,8 @@ DO_ARRAY_TYPE(job_t, job); static int epollfd = -1; static job_array jobs; +static pthread_mutex_t mx; +static pthread_t el_thread; static int el_job_setemode(job_t *w, el_mode emode) { @@ -293,6 +296,16 @@ ssize_t el_job_write(job_t *w, buffer_t *buf) return nr; } +void el_lock(void) +{ + pthread_mutex_lock(&mx); +} + +void el_unlock(void) +{ + pthread_mutex_unlock(&mx); +} + int el_dispatch(int timeout) { struct epoll_event events[FD_SETSIZE]; @@ -305,6 +318,7 @@ 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; @@ -335,11 +349,12 @@ int el_dispatch(int timeout) break; } } + el_unlock(); return 0; } -void *el_loop(void *data) +static void *el_loop(void *data) { time_t sec = time(NULL); @@ -354,16 +369,27 @@ void *el_loop(void *data) 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 (timercmp(&now, &w->mru, >)) { 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(&mx, &attr); + pthread_mutexattr_destroy(&attr); + gnutls_global_init(); epollfd = epoll_create(1024); if (epollfd < 0) { @@ -371,11 +397,15 @@ void el_initialize(void) 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(&mx); } diff --git a/lib-sys/evtloop.h b/lib-sys/evtloop.h index 0865039..961390a 100644 --- a/lib-sys/evtloop.h +++ b/lib-sys/evtloop.h @@ -15,7 +15,7 @@ * MA 02110-1301, USA. */ /* - * Copyright © 2006 Pierre Habouzit + * Copyright © 2007 Pierre Habouzit */ #ifndef MUTT_LIB_SYS_EVTLOOP_H @@ -95,9 +95,10 @@ __must_check__ int el_job_starttls(job_t *w); __must_check__ ssize_t el_job_read(job_t *w, buffer_t *buf); __must_check__ ssize_t el_job_write(job_t *w, buffer_t *buf); +void el_lock(void); +void el_unlock(void); int el_dispatch(int timeout); -void *el_loop(void *); void el_initialize(void); void el_shutdown(void); diff --git a/main.c b/main.c index af2141e..cc432d9 100644 --- a/main.c +++ b/main.c @@ -18,7 +18,6 @@ #include #include -#include #include #include @@ -190,7 +189,6 @@ int main (int argc, char **argv) int version = 0; int i; int explicit_folder = 0; - pthread_t pt; /* initialize random number for tmp file creation */ srand48((unsigned int) time (NULL)); @@ -309,7 +307,6 @@ int main (int argc, char **argv) el_initialize(); mutt_init (flags & M_NOSYSRC, commands); string_list_wipe(&commands); - pthread_create(&pt, NULL, &el_loop, NULL); if (!option(OPTNOCURSES)) { ui_layout_init(); @@ -504,8 +501,6 @@ int main (int argc, char **argv) mutt_endwin (Errorbuf); } - pthread_cancel(pt); - pthread_join(pt, NULL); luaM_shutdown(); mutt_sasl_shutdown(); el_shutdown();