+
+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, >)) {
+ 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) {
+ 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(&mx);
+}