2 * UCW Library -- Main Loop
4 * (c) 2004--2011 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"
30 #ifdef CONFIG_UCW_THREADS
32 #define THREAD_SIGMASK pthread_sigmask
34 #define THREAD_SIGMASK sigprocmask
37 #ifdef CONFIG_UCW_EPOLL
38 #include <sys/epoll.h>
41 #define MAIN_TIMER_LESS(x,y) ((x)->expires < (y)->expires)
42 #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))
44 #define EPOLL_BUF_SIZE 256
46 static void file_del_ctx(struct main_context *m, struct main_file *fi);
47 static void signal_del_ctx(struct main_context *m, struct main_signal *ms);
50 main_get_time_ctx(struct main_context *m)
53 gettimeofday(&tv, NULL);
54 m->now_seconds = tv.tv_sec;
55 m->now = (timestamp_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
58 static struct main_context *
59 main_current_nocheck(void)
61 return ucwlib_thread_context()->main_context;
67 struct main_context *m = main_current_nocheck();
73 main_is_current(struct main_context *m)
75 return (m == main_current_nocheck());
81 struct main_context *m = xmalloc_zero(sizeof(*m));
83 DBG("MAIN: New context");
84 clist_init(&m->file_list);
85 clist_init(&m->file_active_list);
86 clist_init(&m->hook_list);
87 clist_init(&m->hook_done_list);
88 clist_init(&m->process_list);
89 clist_init(&m->signal_list);
90 #ifdef CONFIG_UCW_EPOLL
91 m->epoll_fd = epoll_create(64);
93 die("epoll_create() failed: %m");
94 m->epoll_events = xmalloc(EPOLL_BUF_SIZE * sizeof(struct epoll_event *));
95 clist_init(&m->file_recalc_list);
97 m->poll_table_obsolete = 1;
100 sigemptyset(&m->want_signals);
101 m->sig_pipe_recv = m->sig_pipe_send = -1;
107 main_delete(struct main_context *m)
110 * If the context is current, deactivate it first. But beware,
111 * we must not call functions that depend on the current context.
113 if (main_is_current(m))
114 main_switch_context(NULL);
116 if (m->sigchld_handler)
118 signal_del_ctx(m, m->sigchld_handler);
119 xfree(m->sigchld_handler);
121 if (m->sig_pipe_file)
123 file_del_ctx(m, m->sig_pipe_file);
124 xfree(m->sig_pipe_file);
126 if (m->sig_pipe_recv >= 0)
128 close(m->sig_pipe_recv);
129 close(m->sig_pipe_send);
131 ASSERT(clist_empty(&m->file_list));
132 ASSERT(clist_empty(&m->file_active_list));
133 ASSERT(clist_empty(&m->hook_list));
134 ASSERT(clist_empty(&m->hook_done_list));
135 ASSERT(clist_empty(&m->process_list));
136 ASSERT(clist_empty(&m->signal_list));
137 GARY_FREE(m->timer_table);
138 #ifdef CONFIG_UCW_EPOLL
139 ASSERT(clist_empty(&m->file_recalc_list));
140 xfree(m->epoll_events);
143 GARY_FREE(m->poll_table);
144 GARY_FREE(m->poll_file_table);
149 struct main_context *
150 main_switch_context(struct main_context *m)
152 struct ucwlib_context *c = ucwlib_thread_context();
153 struct main_context *m0 = c->main_context;
156 * Not only we need to switch the signal sets of the two contexts,
157 * but it is also necessary to avoid invoking a signal handler
158 * in the middle of changing c->main_context.
160 if (m0 && !clist_empty(&m0->signal_list))
161 THREAD_SIGMASK(SIG_BLOCK, &m0->want_signals, NULL);
163 if (m && !clist_empty(&m->signal_list))
164 THREAD_SIGMASK(SIG_UNBLOCK, &m->want_signals, NULL);
172 struct main_context *m = main_switch_context(main_new());
179 main_delete(main_current());
185 main_get_time_ctx(main_current());
189 count_timers(struct main_context *m)
192 return GARY_SIZE(m->timer_table) - 1;
198 timer_add(struct main_timer *tm, timestamp_t expires)
200 struct main_context *m = main_current();
204 GARY_INIT(m->timer_table, 1);
205 m->timer_table[0] = NULL;
209 DBG("MAIN: Setting timer %p (expire at now+%lld)", tm, (long long)(expires - m->now));
211 DBG("MAIN: Clearing timer %p", tm);
212 uns num_timers = count_timers(m);
213 if (tm->expires < expires)
217 tm->expires = expires;
218 tm->index = num_timers + 1;
219 *GARY_PUSH(m->timer_table, 1) = tm;
220 HEAP_INSERT(struct main_timer *, m->timer_table, tm->index, MAIN_TIMER_LESS, MAIN_TIMER_SWAP);
224 tm->expires = expires;
225 HEAP_INCREASE(struct main_timer *, m->timer_table, num_timers, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
228 else if (tm->expires > expires)
232 ASSERT(tm->index && tm->index <= num_timers);
233 HEAP_DELETE(struct main_timer *, m->timer_table, num_timers, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
236 GARY_POP(m->timer_table, 1);
240 tm->expires = expires;
241 HEAP_DECREASE(struct main_timer *, m->timer_table, num_timers, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
247 timer_add_rel(struct main_timer *tm, timestamp_t expires_delta)
249 struct main_context *m = main_current();
250 return timer_add(tm, m->now + expires_delta);
254 timer_del(struct main_timer *tm)
260 file_want_events(struct main_file *fi)
263 if (fi->read_handler)
264 events |= POLLIN | POLLHUP | POLLERR;
265 if (fi->write_handler)
266 events |= POLLOUT | POLLERR;
271 file_add(struct main_file *fi)
273 struct main_context *m = main_current();
275 DBG("MAIN: Adding file %p (fd=%d)", fi, fi->fd);
276 ASSERT(!clist_is_linked(&fi->n));
277 clist_add_tail(&m->file_list, &fi->n);
279 #ifdef CONFIG_UCW_EPOLL
280 struct epoll_event evt = {
281 .events = file_want_events(fi),
284 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, fi->fd, &evt) < 0)
285 die("epoll_ctl() failed: %m");
286 fi->last_want_events = evt.events;
288 m->poll_table_obsolete = 1;
290 if (fcntl(fi->fd, F_SETFL, O_NONBLOCK) < 0)
291 msg(L_ERROR, "Error setting fd %d to non-blocking mode: %m. Keep fingers crossed.", fi->fd);
295 file_chg(struct main_file *fi)
297 #ifdef CONFIG_UCW_EPOLL
298 clist_remove(&fi->n);
299 clist_add_tail(&main_current()->file_recalc_list, &fi->n);
301 struct pollfd *p = fi->pollfd;
303 p->events = file_want_events(fi);
308 file_del_ctx(struct main_context *m, struct main_file *fi)
310 // XXX: Can be called on a non-current context
311 DBG("MAIN: Deleting file %p (fd=%d)", fi, fi->fd);
313 ASSERT(clist_is_linked(&fi->n));
314 clist_unlink(&fi->n);
316 #ifdef CONFIG_UCW_EPOLL
317 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, fi->fd, NULL) < 0)
318 die("epoll_ctl() failed: %m");
320 m->poll_table_obsolete = 1;
325 file_del(struct main_file *fi)
327 file_del_ctx(main_current(), fi);
333 struct main_context *m = main_current();
335 CLIST_FOR_EACH(struct main_file *, f, m->file_list)
340 hook_add(struct main_hook *ho)
342 struct main_context *m = main_current();
344 DBG("MAIN: Adding hook %p", ho);
345 ASSERT(!clist_is_linked(&ho->n));
346 clist_add_tail(&m->hook_list, &ho->n);
350 hook_del(struct main_hook *ho)
352 DBG("MAIN: Deleting hook %p", ho);
353 ASSERT(clist_is_linked(&ho->n));
354 clist_unlink(&ho->n);
358 sigchld_received(struct main_signal *sg UNUSED)
360 struct main_context *m = main_current();
364 while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
366 DBG("MAIN: Child %d exited with status %x", pid, stat);
367 CLIST_FOR_EACH(struct main_process *, pr, m->process_list)
372 format_exit_status(pr->status_msg, pr->status);
373 DBG("MAIN: Calling process exit handler");
381 process_add(struct main_process *mp)
383 struct main_context *m = main_current();
385 DBG("MAIN: Adding process %p (pid=%d)", mp, mp->pid);
386 ASSERT(!clist_is_linked(&mp->n));
388 clist_add_tail(&m->process_list, &mp->n);
389 if (!m->sigchld_handler)
391 struct main_signal *sg = xmalloc_zero(sizeof(*sg));
392 m->sigchld_handler = sg;
393 sg->signum = SIGCHLD;
394 sg->handler = sigchld_received;
400 process_del(struct main_process *mp)
402 DBG("MAIN: Deleting process %p (pid=%d)", mp, mp->pid);
403 ASSERT(clist_is_linked(&mp->n));
404 clist_unlink(&mp->n);
408 process_fork(struct main_process *mp)
413 DBG("MAIN: Fork failed");
415 format_exit_status(mp->status_msg, -1);
423 DBG("MAIN: Forked process %d", (int) pid);
431 pipe_read_handler(struct main_file *mf UNUSED)
433 struct main_context *m = main_current();
435 int n = read(m->sig_pipe_recv, &signum, sizeof(signum));
440 msg(L_ERROR, "Error reading signal pipe: %m");
443 ASSERT(n == sizeof(signum));
445 DBG("MAIN: Sigpipe: received signal %d", signum);
446 struct main_signal iter = { .signum = -1 };
447 struct main_signal *sg = clist_head(&m->signal_list);
450 if (sg->signum == signum)
452 DBG("MAIN: Sigpipe: invoking handler %p", sg);
453 clist_insert_after(&iter.n, &sg->n);
455 sg = clist_next(&m->signal_list, &iter.n);
456 clist_remove(&iter.n);
459 sg = clist_next(&m->signal_list, &sg->n);
466 pipe_configure(int fd)
469 if ((flags = fcntl(fd, F_GETFL)) < 0 || fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0)
470 die("Could not set file descriptor %d to non-blocking: %m", fd);
474 pipe_setup(struct main_context *m)
476 DBG("MAIN: Sigpipe: Setting up the pipe");
479 if (pipe(pipe_result) == -1)
480 die("Could not create signal pipe: %m");
481 pipe_configure(pipe_result[0]);
482 pipe_configure(pipe_result[1]);
483 m->sig_pipe_recv = pipe_result[0];
484 m->sig_pipe_send = pipe_result[1];
486 struct main_file *f = xmalloc_zero(sizeof(*f));
487 m->sig_pipe_file = f;
488 f->fd = m->sig_pipe_recv;
489 f->read_handler = pipe_read_handler;
494 signal_handler_pipe(int signum)
496 struct main_context *m = main_current();
498 msg(L_DEBUG | L_SIGHANDLER, "MAIN: Sigpipe: sending signal %d down the drain", signum);
500 write(m->sig_pipe_send, &signum, sizeof(signum));
504 signal_add(struct main_signal *ms)
506 struct main_context *m = main_current();
508 DBG("MAIN: Adding signal %p (sig=%d)", ms, ms->signum);
510 ASSERT(!clist_is_linked(&ms->n));
511 // Adding at the head of the list is better if we are in the middle of walking the list.
512 clist_add_head(&m->signal_list, &ms->n);
513 if (m->sig_pipe_recv < 0)
516 struct sigaction sa = {
517 .sa_handler = signal_handler_pipe,
518 .sa_flags = SA_NOCLDSTOP | SA_RESTART,
520 sigaction(ms->signum, &sa, NULL);
524 sigaddset(&ss, ms->signum);
525 THREAD_SIGMASK(SIG_UNBLOCK, &ss, NULL);
526 sigaddset(&m->want_signals, ms->signum);
530 signal_del_ctx(struct main_context *m, struct main_signal *ms)
532 // XXX: Can be called on a non-current context
533 DBG("MAIN: Deleting signal %p (sig=%d)", ms, ms->signum);
535 ASSERT(clist_is_linked(&ms->n));
536 clist_unlink(&ms->n);
539 CLIST_FOR_EACH(struct main_signal *, s, m->signal_list)
540 if (s->signum == ms->signum)
544 if (main_is_current(m))
548 sigaddset(&ss, ms->signum);
549 THREAD_SIGMASK(SIG_BLOCK, &ss, NULL);
551 sigdelset(&m->want_signals, ms->signum);
556 signal_del(struct main_signal *ms)
558 signal_del_ctx(main_current(), ms);
562 main_debug_context(struct main_context *m UNUSED)
565 msg(L_DEBUG, "### Main loop status on %lld", (long long) m->now);
566 msg(L_DEBUG, "\tActive timers:");
567 uns num_timers = count_timers(m);
568 for (uns i = 1; i <= num_timers; i++)
570 struct main_timer *tm = m->timer_table[i];
571 msg(L_DEBUG, "\t\t%p (expires %lld, data %p)", tm, (long long)(tm->expires ? tm->expires - m->now : 999999), tm->data);
573 msg(L_DEBUG, "\tActive files:");
574 CLIST_FOR_EACH(struct main_file *, fi, m->file_list)
575 msg(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, data %p)",
576 fi, fi->fd, fi->read_handler, fi->write_handler, fi->data);
577 CLIST_FOR_EACH(struct main_file *, fi, m->file_active_list)
578 msg(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, data %p) [pending events: %x]",
579 fi, fi->fd, fi->read_handler, fi->write_handler, fi->data, fi->events);
580 // FIXME: Can we display status of block_io requests somehow?
581 #ifdef CONFIG_UCW_EPOLL
582 CLIST_FOR_EACH(struct main_file *, fi, m->file_recalc_list)
583 msg(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, data %p) [pending recalculation]",
584 fi, fi->fd, fi->read_handler, fi->write_handler, fi->data);
586 msg(L_DEBUG, "\tActive hooks:");
587 CLIST_FOR_EACH(struct main_hook *, ho, m->hook_done_list)
588 msg(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
589 CLIST_FOR_EACH(struct main_hook *, ho, m->hook_list)
590 msg(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
591 msg(L_DEBUG, "\tActive processes:");
592 CLIST_FOR_EACH(struct main_process *, pr, m->process_list)
593 msg(L_DEBUG, "\t\t%p (pid %d, func %p, data %p)", pr, pr->pid, pr->handler, pr->data);
594 msg(L_DEBUG, "\tActive signal catchers:");
595 CLIST_FOR_EACH(struct main_signal *, sg, m->signal_list)
597 msg(L_DEBUG, "\t\t(placeholder)");
599 msg(L_DEBUG, "\t\t%p (sig %d, func %p, data %p)", sg, sg->signum, sg->handler, sg->data);
604 process_timers(struct main_context *m)
606 struct main_timer *tm;
607 while (count_timers(m) && (tm = m->timer_table[1])->expires <= m->now)
609 DBG("MAIN: Timer %p expired at now-%lld", tm, (long long)(m->now - tm->expires));
614 static enum main_hook_return
615 process_hooks(struct main_context *m)
617 int hook_min = HOOK_RETRY;
618 int hook_max = HOOK_SHUTDOWN;
619 struct main_hook *ho;
621 while (ho = clist_remove_head(&m->hook_list))
623 clist_add_tail(&m->hook_done_list, &ho->n);
624 DBG("MAIN: Hook %p", ho);
625 int ret = ho->handler(ho);
626 hook_min = MIN(hook_min, ret);
627 hook_max = MAX(hook_max, ret);
629 clist_move(&m->hook_list, &m->hook_done_list);
630 if (hook_min == HOOK_SHUTDOWN ||
631 hook_min == HOOK_DONE && hook_max == HOOK_DONE ||
634 DBG("MAIN: Shut down by %s", m->shutdown ? "main_shutdown" : "a hook");
635 return HOOK_SHUTDOWN;
637 if (hook_max == HOOK_RETRY)
643 #ifdef CONFIG_UCW_EPOLL
646 recalc_files(struct main_context *m)
648 struct main_file *fi;
650 while (fi = clist_remove_head(&m->file_recalc_list))
652 struct epoll_event evt = {
653 .events = file_want_events(fi),
656 if (evt.events != fi->last_want_events)
658 DBG("MAIN: Changing requested events for fd %d to %x", fi->fd, evt.events);
659 fi->last_want_events = evt.events;
660 if (epoll_ctl(main_current()->epoll_fd, EPOLL_CTL_MOD, fi->fd, &evt) < 0)
661 die("epoll_ctl() failed: %m");
663 clist_add_tail(&m->file_list, &fi->n);
670 rebuild_poll_table(struct main_context *m)
672 GARY_INIT_OR_RESIZE(m->poll_table, m->file_cnt);
673 GARY_INIT_OR_RESIZE(m->poll_file_table, m->file_cnt);
674 DBG("MAIN: Rebuilding poll table: %d entries", m->file_cnt);
676 struct pollfd *p = m->poll_table;
677 struct main_file **pf = m->poll_file_table;
678 CLIST_FOR_EACH(struct main_file *, fi, m->file_list)
681 p->events = file_want_events(fi);
685 m->poll_table_obsolete = 0;
693 DBG("MAIN: Entering main_loop");
694 struct main_context *m = main_current();
696 main_get_time_ctx(m);
699 timestamp_t wake = m->now + 1000000000;
701 switch (process_hooks(m))
711 wake = MIN(wake, m->timer_table[1]->expires);
712 main_get_time_ctx(m);
713 int timeout = ((wake > m->now) ? wake - m->now : 0);
715 #ifdef CONFIG_UCW_EPOLL
717 DBG("MAIN: Epoll for %d fds and timeout %d ms", m->file_cnt, timeout);
718 int n = epoll_wait(m->epoll_fd, m->epoll_events, EPOLL_BUF_SIZE, timeout);
720 if (m->poll_table_obsolete)
721 rebuild_poll_table(m);
722 DBG("MAIN: Poll for %d fds and timeout %d ms", m->file_cnt, timeout);
723 int n = poll(m->poll_table, m->file_cnt, timeout);
726 DBG("\t-> %d events", n);
727 if (n < 0 && errno != EAGAIN && errno != EINTR)
728 die("(e)poll failed: %m");
729 timestamp_t old_now = m->now;
730 main_get_time_ctx(m);
731 m->idle_time += m->now - old_now;
736 // Relink all files with a pending event to file_active_list
737 #ifdef CONFIG_UCW_EPOLL
738 for (int i=0; i<n; i++)
740 struct epoll_event *e = &m->epoll_events[i];
741 struct main_file *fi = e->data.ptr;
742 clist_remove(&fi->n);
743 clist_add_tail(&m->file_active_list, &fi->n);
744 fi->events = e->events;
747 struct pollfd *p = m->poll_table;
748 struct main_file **pf = m->poll_file_table;
749 for (uns i=0; i < m->file_cnt; i++)
752 struct main_file *fi = pf[i];
753 clist_remove(&fi->n);
754 clist_add_tail(&m->file_active_list, &fi->n);
755 fi->events = p[i].revents;
760 * Process the buffered file events. This is pretty tricky, since
761 * user callbacks can modify the file structure or even destroy it.
762 * In such cases, we detect that the structure was relinked and stop
763 * processing its events, leaving them for the next iteration of the
766 struct main_file *fi;
767 while (fi = clist_head(&m->file_active_list))
769 if (fi->events & (POLLIN | POLLHUP | POLLERR))
771 fi->events &= ~(POLLIN | POLLHUP | POLLERR);
773 DBG("MAIN: Read event on fd %d", fi->fd);
774 while (fi->read_handler && fi->read_handler(fi));
777 if (fi->events & (POLLOUT | POLLERR))
779 fi->events &= ~(POLLOUT | POLLERR);
781 DBG("MAIN: Write event on fd %d", fi->fd);
782 while (fi->write_handler && fi->write_handler(fi));
785 clist_remove(&fi->n);
786 clist_add_tail(&m->file_list, &fi->n);