2 * UCW Library -- Main Loop
4 * (c) 2004--2012 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
14 #include <ucw/mainloop.h>
15 #include <ucw/threads.h>
17 #include <ucw/process.h>
32 #ifdef CONFIG_UCW_THREADS
34 #define THREAD_SIGMASK pthread_sigmask
36 #define THREAD_SIGMASK sigprocmask
39 #ifdef CONFIG_UCW_EPOLL
40 #include <sys/epoll.h>
43 #define MAIN_TIMER_LESS(x,y) ((x)->expires < (y)->expires)
44 #define MAIN_TIMER_SWAP(heap,a,b,t) (t=heap[a], heap[a]=heap[b], heap[b]=t, heap[a]->index=(a), heap[b]->index=(b))
46 #define EPOLL_BUF_SIZE 256
48 static void file_del_ctx(struct main_context *m, struct main_file *fi);
49 static void signal_del_ctx(struct main_context *m, struct main_signal *ms);
52 main_get_time_ctx(struct main_context *m)
54 m->now = get_timestamp();
57 static struct main_context *
58 main_current_nocheck(void)
60 return ucwlib_thread_context()->main_context;
66 struct main_context *m = main_current_nocheck();
72 main_is_current(struct main_context *m)
74 return (m == main_current_nocheck());
78 count_timers(struct main_context *m)
81 return GARY_SIZE(m->timer_table) - 1;
89 struct main_context *m = xmalloc_zero(sizeof(*m));
91 DBG("MAIN: New context");
92 clist_init(&m->file_list);
93 clist_init(&m->file_active_list);
94 clist_init(&m->hook_list);
95 clist_init(&m->hook_done_list);
96 clist_init(&m->process_list);
97 clist_init(&m->signal_list);
98 #ifdef CONFIG_UCW_EPOLL
99 m->epoll_fd = epoll_create(64);
101 die("epoll_create() failed: %m");
102 m->epoll_events = xmalloc(EPOLL_BUF_SIZE * sizeof(struct epoll_event));
103 clist_init(&m->file_recalc_list);
105 m->poll_table_obsolete = 1;
107 main_get_time_ctx(m);
108 sigemptyset(&m->want_signals);
109 m->sig_pipe_recv = m->sig_pipe_send = -1;
115 main_prepare_delete(struct main_context *m)
118 * If the context is current, deactivate it first. But beware,
119 * we must not call functions that depend on the current context.
121 if (main_is_current(m))
122 main_switch_context(NULL);
124 // Close epoll descriptor early enough, it might be shared after fork!
125 #ifdef CONFIG_UCW_EPOLL
126 xfree(m->epoll_events);
130 GARY_FREE(m->poll_table);
131 GARY_FREE(m->poll_file_table);
134 if (m->sigchld_handler)
136 signal_del_ctx(m, m->sigchld_handler);
137 xfree(m->sigchld_handler);
139 if (m->sig_pipe_file)
141 file_del_ctx(m, m->sig_pipe_file);
142 xfree(m->sig_pipe_file);
144 if (m->sig_pipe_recv >= 0)
146 close(m->sig_pipe_recv);
147 close(m->sig_pipe_send);
152 main_do_delete(struct main_context *m)
154 GARY_FREE(m->timer_table);
159 main_delete(struct main_context *m)
164 main_prepare_delete(m);
165 ASSERT(clist_empty(&m->file_list));
166 ASSERT(clist_empty(&m->file_active_list));
167 #ifdef CONFIG_UCW_EPOLL
168 ASSERT(clist_empty(&m->file_recalc_list));
170 ASSERT(clist_empty(&m->hook_list));
171 ASSERT(clist_empty(&m->hook_done_list));
172 ASSERT(clist_empty(&m->process_list));
173 ASSERT(clist_empty(&m->signal_list));
174 ASSERT(!count_timers(m));
179 main_destroy(struct main_context *m)
183 main_prepare_delete(m);
186 clist_insert_list_after(&m->file_active_list, m->file_list.head.prev);
187 #ifdef CONFIG_UCW_EPOLL
188 clist_insert_list_after(&m->file_recalc_list, m->file_list.head.prev);
190 CLIST_FOR_EACH(struct main_file *, f, m->file_list)
196 struct main_context *
197 main_switch_context(struct main_context *m)
199 struct ucwlib_context *c = ucwlib_thread_context();
200 struct main_context *m0 = c->main_context;
203 * Not only we need to switch the signal sets of the two contexts,
204 * but it is also necessary to avoid invoking a signal handler
205 * in the middle of changing c->main_context.
207 if (m0 && !clist_empty(&m0->signal_list))
208 THREAD_SIGMASK(SIG_BLOCK, &m0->want_signals, NULL);
210 if (m && !clist_empty(&m->signal_list))
211 THREAD_SIGMASK(SIG_UNBLOCK, &m->want_signals, NULL);
219 struct main_context *m = main_switch_context(main_new());
226 main_delete(main_current_nocheck());
232 main_destroy(main_current_nocheck());
238 main_get_time_ctx(main_current());
242 timer_add(struct main_timer *tm, timestamp_t expires)
244 struct main_context *m = main_current();
248 GARY_INIT(m->timer_table, 1);
249 m->timer_table[0] = NULL;
253 DBG("MAIN: Setting timer %p (expire at now+%lld)", tm, (long long)(expires - m->now));
255 DBG("MAIN: Clearing timer %p", tm);
256 uns num_timers = count_timers(m);
257 if (tm->expires < expires)
261 tm->expires = expires;
262 tm->index = num_timers + 1;
263 *GARY_PUSH(m->timer_table, 1) = tm;
264 HEAP_INSERT(struct main_timer *, m->timer_table, tm->index, MAIN_TIMER_LESS, MAIN_TIMER_SWAP);
268 tm->expires = expires;
269 HEAP_INCREASE(struct main_timer *, m->timer_table, num_timers, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
272 else if (tm->expires > expires)
276 ASSERT(tm->index && tm->index <= num_timers);
277 HEAP_DELETE(struct main_timer *, m->timer_table, num_timers, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
280 GARY_POP(m->timer_table, 1);
284 tm->expires = expires;
285 HEAP_DECREASE(struct main_timer *, m->timer_table, num_timers, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
291 timer_add_rel(struct main_timer *tm, timestamp_t expires_delta)
293 struct main_context *m = main_current();
294 return timer_add(tm, m->now + expires_delta);
298 timer_del(struct main_timer *tm)
304 file_want_events(struct main_file *fi)
307 if (fi->read_handler)
309 if (fi->write_handler)
315 file_add(struct main_file *fi)
317 struct main_context *m = main_current();
319 DBG("MAIN: Adding file %p (fd=%d)", fi, fi->fd);
320 ASSERT(!file_is_active(fi));
321 clist_add_tail(&m->file_list, &fi->n);
323 #ifdef CONFIG_UCW_EPOLL
324 struct epoll_event evt = {
325 .events = file_want_events(fi),
328 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, fi->fd, &evt) < 0)
329 die("epoll_ctl() failed: %m");
330 fi->last_want_events = evt.events;
332 m->poll_table_obsolete = 1;
334 if (fcntl(fi->fd, F_SETFL, O_NONBLOCK) < 0)
335 msg(L_ERROR, "Error setting fd %d to non-blocking mode: %m. Keep fingers crossed.", fi->fd);
339 file_chg(struct main_file *fi)
341 #ifdef CONFIG_UCW_EPOLL
342 clist_remove(&fi->n);
343 clist_add_tail(&main_current()->file_recalc_list, &fi->n);
345 struct pollfd *p = fi->pollfd;
347 p->events = file_want_events(fi);
352 file_del_ctx(struct main_context *m, struct main_file *fi)
354 // XXX: Can be called on a non-current context
355 DBG("MAIN: Deleting file %p (fd=%d)", fi, fi->fd);
357 if (!file_is_active(fi))
359 clist_unlink(&fi->n);
361 #ifdef CONFIG_UCW_EPOLL
362 if (m->epoll_fd >= 0 && epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, fi->fd, NULL) < 0)
363 die("epoll_ctl() failed: %m");
365 m->poll_table_obsolete = 1;
370 file_del(struct main_file *fi)
372 file_del_ctx(main_current(), fi);
376 hook_add(struct main_hook *ho)
378 struct main_context *m = main_current();
380 DBG("MAIN: Adding hook %p", ho);
381 if (hook_is_active(ho))
382 clist_unlink(&ho->n);
383 clist_add_tail(&m->hook_list, &ho->n);
387 hook_del(struct main_hook *ho)
389 DBG("MAIN: Deleting hook %p", ho);
390 if (hook_is_active(ho))
391 clist_unlink(&ho->n);
395 sigchld_received(struct main_signal *sg UNUSED)
397 struct main_context *m = main_current();
401 while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
403 DBG("MAIN: Child %d exited with status %x", pid, stat);
404 CLIST_FOR_EACH(struct main_process *, pr, m->process_list)
409 format_exit_status(pr->status_msg, pr->status);
410 DBG("MAIN: Calling process exit handler");
418 process_add(struct main_process *mp)
420 struct main_context *m = main_current();
422 DBG("MAIN: Adding process %p (pid=%d)", mp, mp->pid);
423 ASSERT(!process_is_active(mp));
425 clist_add_tail(&m->process_list, &mp->n);
426 if (!m->sigchld_handler)
428 struct main_signal *sg = xmalloc_zero(sizeof(*sg));
429 m->sigchld_handler = sg;
430 sg->signum = SIGCHLD;
431 sg->handler = sigchld_received;
437 process_del(struct main_process *mp)
439 DBG("MAIN: Deleting process %p (pid=%d)", mp, mp->pid);
440 if (process_is_active(mp))
441 clist_unlink(&mp->n);
445 process_fork(struct main_process *mp)
450 DBG("MAIN: Fork failed");
452 format_exit_status(mp->status_msg, -1);
460 DBG("MAIN: Forked process %d", (int) pid);
468 pipe_read_handler(struct main_file *mf UNUSED)
470 struct main_context *m = main_current();
472 int n = read(m->sig_pipe_recv, &signum, sizeof(signum));
477 msg(L_ERROR, "Error reading signal pipe: %m");
480 ASSERT(n == sizeof(signum));
482 DBG("MAIN: Sigpipe: received signal %d", signum);
483 struct main_signal iter = { .signum = -1 };
484 struct main_signal *sg = clist_head(&m->signal_list);
487 if (sg->signum == signum)
489 DBG("MAIN: Sigpipe: invoking handler %p", sg);
490 clist_insert_after(&iter.n, &sg->n);
492 sg = clist_next(&m->signal_list, &iter.n);
493 clist_remove(&iter.n);
496 sg = clist_next(&m->signal_list, &sg->n);
503 pipe_configure(int fd)
506 if ((flags = fcntl(fd, F_GETFL)) < 0 || fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0)
507 die("Could not set file descriptor %d to non-blocking: %m", fd);
511 pipe_setup(struct main_context *m)
513 DBG("MAIN: Sigpipe: Setting up the pipe");
516 if (pipe(pipe_result) == -1)
517 die("Could not create signal pipe: %m");
518 pipe_configure(pipe_result[0]);
519 pipe_configure(pipe_result[1]);
520 m->sig_pipe_recv = pipe_result[0];
521 m->sig_pipe_send = pipe_result[1];
523 struct main_file *f = xmalloc_zero(sizeof(*f));
524 m->sig_pipe_file = f;
525 f->fd = m->sig_pipe_recv;
526 f->read_handler = pipe_read_handler;
531 signal_handler_pipe(int signum)
533 struct main_context *m = main_current();
535 msg(L_DEBUG | L_SIGHANDLER, "MAIN: Sigpipe: sending signal %d down the drain", signum);
537 write(m->sig_pipe_send, &signum, sizeof(signum));
541 signal_add(struct main_signal *ms)
543 struct main_context *m = main_current();
545 DBG("MAIN: Adding signal %p (sig=%d)", ms, ms->signum);
547 ASSERT(!signal_is_active(ms));
548 // Adding at the head of the list is better if we are in the middle of walking the list.
549 clist_add_head(&m->signal_list, &ms->n);
550 if (m->sig_pipe_recv < 0)
553 struct sigaction sa = {
554 .sa_handler = signal_handler_pipe,
555 .sa_flags = SA_NOCLDSTOP | SA_RESTART,
557 sigaction(ms->signum, &sa, NULL);
561 sigaddset(&ss, ms->signum);
562 THREAD_SIGMASK(SIG_UNBLOCK, &ss, NULL);
563 sigaddset(&m->want_signals, ms->signum);
567 signal_del_ctx(struct main_context *m, struct main_signal *ms)
569 // XXX: Can be called on a non-current context
570 DBG("MAIN: Deleting signal %p (sig=%d)", ms, ms->signum);
572 if (!signal_is_active(ms))
574 clist_unlink(&ms->n);
577 CLIST_FOR_EACH(struct main_signal *, s, m->signal_list)
578 if (s->signum == ms->signum)
582 if (main_is_current(m))
586 sigaddset(&ss, ms->signum);
587 THREAD_SIGMASK(SIG_BLOCK, &ss, NULL);
589 sigdelset(&m->want_signals, ms->signum);
594 signal_del(struct main_signal *ms)
596 signal_del_ctx(main_current(), ms);
599 #ifdef CONFIG_UCW_DEBUG
602 file_debug(struct main_file *fi)
604 msg(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, data %p)",
605 fi, fi->fd, fi->read_handler, fi->write_handler, fi->data);
609 hook_debug(struct main_hook *ho)
611 msg(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
615 signal_debug(struct main_signal *sg)
618 msg(L_DEBUG, "\t\t(placeholder)");
620 msg(L_DEBUG, "\t\t%p (sig %d, func %p, data %p)", sg, sg->signum, sg->handler, sg->data);
624 timer_debug_ctx(struct main_context *m, struct main_timer *tm)
626 msg(L_DEBUG, "\t\t%p (expires %lld, data %p)", tm, (long long)(tm->expires - m->now), tm->data);
630 timer_debug(struct main_timer *tm)
632 timer_debug_ctx(main_current(), tm);
636 process_debug(struct main_process *pr)
638 msg(L_DEBUG, "\t\t%p (pid %d, func %p, data %p)", pr, pr->pid, pr->handler, pr->data);
642 main_debug_context(struct main_context *m UNUSED)
644 msg(L_DEBUG, "### Main loop status on %lld", (long long) m->now);
645 msg(L_DEBUG, "\tActive timers:");
646 uns num_timers = count_timers(m);
647 for (uns i = 1; i <= num_timers; i++)
648 timer_debug(m->timer_table[i]);
649 msg(L_DEBUG, "\tActive files:");
650 CLIST_FOR_EACH(struct main_file *, fi, m->file_list)
652 CLIST_FOR_EACH(struct main_file *, fi, m->file_active_list)
654 #ifdef CONFIG_UCW_EPOLL
655 CLIST_FOR_EACH(struct main_file *, fi, m->file_recalc_list)
658 msg(L_DEBUG, "\tActive hooks:");
659 CLIST_FOR_EACH(struct main_hook *, ho, m->hook_done_list)
661 CLIST_FOR_EACH(struct main_hook *, ho, m->hook_list)
663 msg(L_DEBUG, "\tActive processes:");
664 CLIST_FOR_EACH(struct main_process *, pr, m->process_list)
666 msg(L_DEBUG, "\tActive signal catchers:");
667 CLIST_FOR_EACH(struct main_signal *, sg, m->signal_list)
674 void file_debug(struct main_file *fi UNUSED) { }
675 void hook_debug(struct main_hook *ho UNUSED) { }
676 void signal_debug(struct main_signal *sg UNUSED) { }
677 void timer_debug(struct main_timer *tm UNUSED) { }
678 void process_debug(struct main_process *pr UNUSED) { }
679 void main_debug_context(struct main_context *m UNUSED) { }
684 process_timers(struct main_context *m)
686 struct main_timer *tm;
687 while (count_timers(m) && (tm = m->timer_table[1])->expires <= m->now)
689 DBG("MAIN: Timer %p expired at now-%lld", tm, (long long)(m->now - tm->expires));
694 static enum main_hook_return
695 process_hooks(struct main_context *m)
697 int hook_min = HOOK_RETRY;
698 int hook_max = HOOK_SHUTDOWN;
699 struct main_hook *ho;
701 while (ho = clist_remove_head(&m->hook_list))
703 clist_add_tail(&m->hook_done_list, &ho->n);
704 DBG("MAIN: Hook %p", ho);
705 int ret = ho->handler(ho);
706 hook_min = MIN(hook_min, ret);
707 hook_max = MAX(hook_max, ret);
709 clist_move(&m->hook_list, &m->hook_done_list);
710 if (hook_min == HOOK_SHUTDOWN ||
711 hook_min == HOOK_DONE && hook_max == HOOK_DONE ||
714 DBG("MAIN: Shut down by %s", m->shutdown ? "main_shut_down" : "a hook");
715 return HOOK_SHUTDOWN;
717 if (hook_max == HOOK_RETRY)
723 #ifdef CONFIG_UCW_EPOLL
726 recalc_files(struct main_context *m)
728 struct main_file *fi;
730 while (fi = clist_remove_head(&m->file_recalc_list))
732 struct epoll_event evt = {
733 .events = file_want_events(fi),
736 if (evt.events != fi->last_want_events)
738 DBG("MAIN: Changing requested events for fd %d to %x", fi->fd, evt.events);
739 fi->last_want_events = evt.events;
740 if (epoll_ctl(main_current()->epoll_fd, EPOLL_CTL_MOD, fi->fd, &evt) < 0)
741 die("epoll_ctl() failed: %m");
743 clist_add_tail(&m->file_list, &fi->n);
750 rebuild_poll_table(struct main_context *m)
752 GARY_INIT_OR_RESIZE(m->poll_table, m->file_cnt);
753 GARY_INIT_OR_RESIZE(m->poll_file_table, m->file_cnt);
754 DBG("MAIN: Rebuilding poll table: %d entries", m->file_cnt);
756 struct pollfd *p = m->poll_table;
757 struct main_file **pf = m->poll_file_table;
758 CLIST_FOR_EACH(struct main_file *, fi, m->file_list)
761 p->events = file_want_events(fi);
765 m->poll_table_obsolete = 0;
773 DBG("MAIN: Entering main_loop");
774 struct main_context *m = main_current();
776 main_get_time_ctx(m);
781 timestamp_t wake = m->now + 1000000000;
783 switch (process_hooks(m))
797 wake = MIN(wake, m->timer_table[1]->expires);
798 main_get_time_ctx(m);
799 timeout = ((wake > m->now) ? wake - m->now : 0);
802 #ifdef CONFIG_UCW_EPOLL
804 DBG("MAIN: Epoll for %d fds and timeout %d ms", m->file_cnt, timeout);
805 int n = epoll_wait(m->epoll_fd, m->epoll_events, EPOLL_BUF_SIZE, timeout);
807 if (m->poll_table_obsolete)
808 rebuild_poll_table(m);
809 DBG("MAIN: Poll for %d fds and timeout %d ms", m->file_cnt, timeout);
810 int n = poll(m->poll_table, m->file_cnt, timeout);
813 DBG("\t-> %d events", n);
814 if (n < 0 && errno != EAGAIN && errno != EINTR)
815 die("(e)poll failed: %m");
816 timestamp_t old_now = m->now;
817 main_get_time_ctx(m);
818 m->idle_time += m->now - old_now;
828 // Relink all files with a pending event to file_active_list
829 #ifdef CONFIG_UCW_EPOLL
830 for (int i=0; i<n; i++)
832 struct epoll_event *e = &m->epoll_events[i];
833 struct main_file *fi = e->data.ptr;
834 clist_remove(&fi->n);
835 clist_add_tail(&m->file_active_list, &fi->n);
836 fi->events = e->events;
839 struct pollfd *p = m->poll_table;
840 struct main_file **pf = m->poll_file_table;
841 for (uns i=0; i < m->file_cnt; i++)
844 struct main_file *fi = pf[i];
845 clist_remove(&fi->n);
846 clist_add_tail(&m->file_active_list, &fi->n);
847 fi->events = p[i].revents;
852 * Process the buffered file events. This is pretty tricky, since
853 * user callbacks can modify the file structure or even destroy it.
854 * In such cases, we detect that the structure was relinked and stop
855 * processing its events, leaving them for the next iteration of the
858 struct main_file *fi;
859 while (fi = clist_head(&m->file_active_list))
861 if (fi->read_handler && (fi->events & (POLLIN | POLLHUP)))
863 fi->events &= ~(POLLIN | POLLHUP);
865 DBG("MAIN: Read event on fd %d", fi->fd);
866 while (fi->read_handler && fi->read_handler(fi));
869 if (fi->write_handler && (fi->events & (POLLOUT | POLLHUP | POLLERR)))
871 fi->events &= ~(POLLOUT | POLLHUP | POLLERR);
873 DBG("MAIN: Write event on fd %d", fi->fd);
874 while (fi->write_handler && fi->write_handler(fi));
877 clist_remove(&fi->n);
878 clist_add_tail(&m->file_list, &fi->n);
886 struct main_context *m = main_current();