2 * UCW Library -- Main Loop
4 * (c) 2004--2010 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
47 do_main_get_time(struct main_context *m)
50 gettimeofday(&tv, NULL);
51 m->now_seconds = tv.tv_sec;
52 m->now = (timestamp_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
58 struct main_context *m = xmalloc_zero(sizeof(*m));
60 DBG("MAIN: New context");
61 clist_init(&m->file_list);
62 clist_init(&m->file_active_list);
63 clist_init(&m->hook_list);
64 clist_init(&m->hook_done_list);
65 clist_init(&m->process_list);
66 clist_init(&m->signal_list);
67 #ifdef CONFIG_UCW_EPOLL
68 m->epoll_fd = epoll_create(64);
70 die("epoll_create() failed: %m");
71 m->epoll_events = xmalloc(EPOLL_BUF_SIZE * sizeof(struct epoll_event *));
72 clist_init(&m->file_recalc_list);
74 m->poll_table_obsolete = 1;
77 sigemptyset(&m->want_signals);
78 m->sig_pipe_recv = m->sig_pipe_send = -1;
84 main_delete(struct main_context *m)
86 if (m->sigchld_handler)
87 signal_del(m->sigchld_handler);
89 file_del(m->sig_pipe_file);
90 if (m->sig_pipe_recv >= 0)
92 close(m->sig_pipe_recv);
93 close(m->sig_pipe_send);
95 ASSERT(clist_empty(&m->file_list));
96 ASSERT(clist_empty(&m->file_active_list));
97 ASSERT(clist_empty(&m->hook_list));
98 ASSERT(clist_empty(&m->hook_done_list));
99 ASSERT(clist_empty(&m->process_list));
100 ASSERT(clist_empty(&m->signal_list));
101 GARY_FREE(m->timer_table);
102 #ifdef CONFIG_UCW_EPOLL
103 ASSERT(clist_empty(&m->file_recalc_list));
104 xfree(m->epoll_events);
107 GARY_FREE(m->poll_table);
108 GARY_FREE(m->poll_file_table);
111 // FIXME: Some mechanism for cleaning up after fork()
114 struct main_context *
115 main_switch_context(struct main_context *m)
117 struct ucwlib_context *c = ucwlib_thread_context();
118 struct main_context *m0 = c->main_context;
121 * Not only we need to switch the signal sets of the two contexts,
122 * but it is also necessary to avoid invoking a signal handler
123 * in the middle of changing c->main_context.
125 if (m0 && !clist_empty(&m0->signal_list))
126 THREAD_SIGMASK(SIG_BLOCK, &m0->want_signals, NULL);
128 if (m && !clist_empty(&m->signal_list))
129 THREAD_SIGMASK(SIG_UNBLOCK, &m->want_signals, NULL);
134 struct main_context *
137 struct ucwlib_context *c = ucwlib_thread_context();
138 struct main_context *m = c->main_context;
146 struct main_context *m = main_switch_context(main_new());
153 struct main_context *m = main_switch_context(NULL);
160 do_main_get_time(main_current());
164 count_timers(struct main_context *m)
166 return GARY_SIZE(m->timer_table) - 1;
170 timer_add(struct main_timer *tm, timestamp_t expires)
172 struct main_context *m = main_current();
176 GARY_INIT(m->timer_table, 1);
177 m->timer_table[0] = NULL;
181 DBG("MAIN: Setting timer %p (expire at now+%lld)", tm, (long long)(expires - m->now));
183 DBG("MAIN: Clearing timer %p", tm);
184 uns num_timers = count_timers(m);
185 if (tm->expires < expires)
189 tm->expires = expires;
190 tm->index = num_timers + 1;
191 *GARY_PUSH(m->timer_table, 1) = tm;
192 HEAP_INSERT(struct main_timer *, m->timer_table, tm->index, MAIN_TIMER_LESS, MAIN_TIMER_SWAP);
196 tm->expires = expires;
197 HEAP_INCREASE(struct main_timer *, m->timer_table, num_timers, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
200 else if (tm->expires > expires)
204 ASSERT(tm->index && tm->index <= num_timers);
205 HEAP_DELETE(struct main_timer *, m->timer_table, num_timers, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
208 GARY_POP(m->timer_table, 1);
212 tm->expires = expires;
213 HEAP_DECREASE(struct main_timer *, m->timer_table, num_timers, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
219 timer_add_rel(struct main_timer *tm, timestamp_t expires_delta)
221 struct main_context *m = main_current();
222 return timer_add(tm, m->now + expires_delta);
226 timer_del(struct main_timer *tm)
232 file_want_events(struct main_file *fi)
235 if (fi->read_handler)
236 events |= POLLIN | POLLHUP | POLLERR;
237 if (fi->write_handler)
238 events |= POLLOUT | POLLERR;
243 file_add(struct main_file *fi)
245 struct main_context *m = main_current();
247 DBG("MAIN: Adding file %p (fd=%d)", fi, fi->fd);
248 ASSERT(!clist_is_linked(&fi->n));
249 clist_add_tail(&m->file_list, &fi->n);
251 #ifdef CONFIG_UCW_EPOLL
252 struct epoll_event evt = {
253 .events = file_want_events(fi),
256 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, fi->fd, &evt) < 0)
257 die("epoll_ctl() failed: %m");
258 fi->last_want_events = evt.events;
260 m->poll_table_obsolete = 1;
262 if (fcntl(fi->fd, F_SETFL, O_NONBLOCK) < 0)
263 msg(L_ERROR, "Error setting fd %d to non-blocking mode: %m. Keep fingers crossed.", fi->fd);
267 file_chg(struct main_file *fi)
269 #ifdef CONFIG_UCW_EPOLL
270 clist_remove(&fi->n);
271 clist_add_tail(&main_current()->file_recalc_list, &fi->n);
273 struct pollfd *p = fi->pollfd;
275 p->events = file_want_events(fi);
280 file_del(struct main_file *fi)
282 struct main_context *m = main_current();
284 DBG("MAIN: Deleting file %p (fd=%d)", fi, fi->fd);
285 ASSERT(clist_is_linked(&fi->n));
286 clist_unlink(&fi->n);
288 #ifdef CONFIG_UCW_EPOLL
289 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, fi->fd, NULL) < 0)
290 die("epoll_ctl() failed: %m");
292 m->poll_table_obsolete = 1;
299 struct main_context *m = main_current();
301 CLIST_FOR_EACH(struct main_file *, f, m->file_list)
306 hook_add(struct main_hook *ho)
308 struct main_context *m = main_current();
310 DBG("MAIN: Adding hook %p", ho);
311 ASSERT(!clist_is_linked(&ho->n));
312 clist_add_tail(&m->hook_list, &ho->n);
316 hook_del(struct main_hook *ho)
318 DBG("MAIN: Deleting hook %p", ho);
319 ASSERT(clist_is_linked(&ho->n));
320 clist_unlink(&ho->n);
324 sigchld_received(struct main_signal *sg UNUSED)
326 struct main_context *m = main_current();
330 while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
332 DBG("MAIN: Child %d exited with status %x", pid, stat);
333 CLIST_FOR_EACH(struct main_process *, pr, m->process_list)
338 format_exit_status(pr->status_msg, pr->status);
339 DBG("MAIN: Calling process exit handler");
347 process_add(struct main_process *mp)
349 struct main_context *m = main_current();
351 DBG("MAIN: Adding process %p (pid=%d)", mp, mp->pid);
352 ASSERT(!clist_is_linked(&mp->n));
354 clist_add_tail(&m->process_list, &mp->n);
355 if (!m->sigchld_handler)
357 struct main_signal *sg = xmalloc_zero(sizeof(*sg));
358 m->sigchld_handler = sg;
359 sg->signum = SIGCHLD;
360 sg->handler = sigchld_received;
366 process_del(struct main_process *mp)
368 DBG("MAIN: Deleting process %p (pid=%d)", mp, mp->pid);
369 ASSERT(clist_is_linked(&mp->n));
370 clist_unlink(&mp->n);
374 process_fork(struct main_process *mp)
379 DBG("MAIN: Fork failed");
381 format_exit_status(mp->status_msg, -1);
389 DBG("MAIN: Forked process %d", (int) pid);
397 pipe_read_handler(struct main_file *mf UNUSED)
399 struct main_context *m = main_current();
401 int n = read(m->sig_pipe_recv, &signum, sizeof(signum));
406 msg(L_ERROR, "Error reading signal pipe: %m");
409 ASSERT(n == sizeof(signum));
411 DBG("MAIN: Sigpipe: received signal %d", signum);
412 struct main_signal iter = { .signum = -1 };
413 struct main_signal *sg = clist_head(&m->signal_list);
416 if (sg->signum == signum)
418 DBG("MAIN: Sigpipe: invoking handler %p", sg);
419 clist_insert_after(&iter.n, &sg->n);
421 sg = clist_next(&m->signal_list, &iter.n);
422 clist_remove(&iter.n);
425 sg = clist_next(&m->signal_list, &sg->n);
432 pipe_configure(int fd)
435 if ((flags = fcntl(fd, F_GETFL)) < 0 || fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0)
436 die("Could not set file descriptor %d to non-blocking: %m", fd);
440 pipe_setup(struct main_context *m)
442 DBG("MAIN: Sigpipe: Setting up the pipe");
445 if (pipe(pipe_result) == -1)
446 die("Could not create signal pipe: %m");
447 pipe_configure(pipe_result[0]);
448 pipe_configure(pipe_result[1]);
449 m->sig_pipe_recv = pipe_result[0];
450 m->sig_pipe_send = pipe_result[1];
452 struct main_file *f = xmalloc_zero(sizeof(*f));
453 m->sig_pipe_file = f;
454 f->fd = m->sig_pipe_recv;
455 f->read_handler = pipe_read_handler;
460 signal_handler_pipe(int signum)
462 struct main_context *m = main_current();
464 msg(L_DEBUG | L_SIGHANDLER, "MAIN: Sigpipe: sending signal %d down the drain", signum);
466 write(m->sig_pipe_send, &signum, sizeof(signum));
470 signal_add(struct main_signal *ms)
472 struct main_context *m = main_current();
474 DBG("MAIN: Adding signal %p (sig=%d)", ms, ms->signum);
476 ASSERT(!clist_is_linked(&ms->n));
477 // Adding at the head of the list is better if we are in the middle of walking the list.
478 clist_add_head(&m->signal_list, &ms->n);
479 if (m->sig_pipe_recv < 0)
482 struct sigaction sa = {
483 .sa_handler = signal_handler_pipe,
484 .sa_flags = SA_NOCLDSTOP | SA_RESTART,
486 sigaction(ms->signum, &sa, NULL);
490 sigaddset(&ss, ms->signum);
491 THREAD_SIGMASK(SIG_UNBLOCK, &ss, NULL);
492 sigaddset(&m->want_signals, ms->signum);
496 signal_del(struct main_signal *ms)
498 struct main_context *m = main_current();
500 DBG("MAIN: Deleting signal %p (sig=%d)", ms, ms->signum);
502 ASSERT(clist_is_linked(&ms->n));
503 clist_unlink(&ms->n);
506 CLIST_FOR_EACH(struct main_signal *, s, m->signal_list)
507 if (s->signum == ms->signum)
513 sigaddset(&ss, ms->signum);
514 THREAD_SIGMASK(SIG_BLOCK, &ss, NULL);
515 sigdelset(&m->want_signals, ms->signum);
520 main_debug_context(struct main_context *m UNUSED)
523 msg(L_DEBUG, "### Main loop status on %lld", (long long) m->now);
524 msg(L_DEBUG, "\tActive timers:");
525 uns num_timers = count_timers(m);
526 for (uns i = 1; i <= num_timers; i++)
528 struct main_timer *tm = m->timer_table[i];
529 msg(L_DEBUG, "\t\t%p (expires %lld, data %p)", tm, (long long)(tm->expires ? tm->expires - m->now : 999999), tm->data);
531 msg(L_DEBUG, "\tActive files:");
532 CLIST_FOR_EACH(struct main_file *, fi, m->file_list)
533 msg(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, data %p)",
534 fi, fi->fd, fi->read_handler, fi->write_handler, fi->data);
535 CLIST_FOR_EACH(struct main_file *, fi, m->file_list)
536 msg(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, data %p) [pending events: %x]",
537 fi, fi->fd, fi->read_handler, fi->write_handler, fi->data, fi->events);
538 // FIXME: Can we display status of block_io requests somehow?
539 #ifdef CONFIG_UCW_EPOLL
540 CLIST_FOR_EACH(struct main_file *, fi, m->file_recalc_list)
541 msg(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, data %p) [pending recalculation]",
542 fi, fi->fd, fi->read_handler, fi->write_handler, fi->data);
544 msg(L_DEBUG, "\tActive hooks:");
545 CLIST_FOR_EACH(struct main_hook *, ho, m->hook_done_list)
546 msg(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
547 CLIST_FOR_EACH(struct main_hook *, ho, m->hook_list)
548 msg(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
549 msg(L_DEBUG, "\tActive processes:");
550 CLIST_FOR_EACH(struct main_process *, pr, m->process_list)
551 msg(L_DEBUG, "\t\t%p (pid %d, func %p, data %p)", pr, pr->pid, pr->handler, pr->data);
552 msg(L_DEBUG, "\tActive signal catchers:");
553 CLIST_FOR_EACH(struct main_signal *, sg, m->signal_list)
555 msg(L_DEBUG, "\t\t(placeholder)");
557 msg(L_DEBUG, "\t\t%p (sig %d, func %p, data %p)", sg, sg->signum, sg->handler, sg->data);
562 process_timers(struct main_context *m)
564 struct main_timer *tm;
565 while (GARY_SIZE(m->timer_table) > 1 && (tm = m->timer_table[1])->expires <= m->now)
567 DBG("MAIN: Timer %p expired at now-%lld", tm, (long long)(m->now - tm->expires));
572 static enum main_hook_return
573 process_hooks(struct main_context *m)
575 int hook_min = HOOK_RETRY;
576 int hook_max = HOOK_SHUTDOWN;
577 struct main_hook *ho;
579 while (ho = clist_remove_head(&m->hook_list))
581 clist_add_tail(&m->hook_done_list, &ho->n);
582 DBG("MAIN: Hook %p", ho);
583 int ret = ho->handler(ho);
584 hook_min = MIN(hook_min, ret);
585 hook_max = MAX(hook_max, ret);
587 clist_move(&m->hook_list, &m->hook_done_list);
588 if (hook_min == HOOK_SHUTDOWN ||
589 hook_min == HOOK_DONE && hook_max == HOOK_DONE ||
592 DBG("MAIN: Shut down by %s", m->shutdown ? "main_shutdown" : "a hook");
593 return HOOK_SHUTDOWN;
595 if (hook_max == HOOK_RETRY)
601 #ifdef CONFIG_UCW_EPOLL
604 recalc_files(struct main_context *m)
606 struct main_file *fi;
608 while (fi = clist_remove_head(&m->file_recalc_list))
610 struct epoll_event evt = {
611 .events = file_want_events(fi),
614 if (evt.events != fi->last_want_events)
616 DBG("MAIN: Changing requested events for fd %d to %x", fi->fd, evt.events);
617 fi->last_want_events = evt.events;
618 if (epoll_ctl(main_current()->epoll_fd, EPOLL_CTL_MOD, fi->fd, &evt) < 0)
619 die("epoll_ctl() failed: %m");
621 clist_add_tail(&m->file_list, &fi->n);
628 rebuild_poll_table(struct main_context *m)
630 GARY_INIT_OR_RESIZE(m->poll_table, m->file_cnt);
631 GARY_INIT_OR_RESIZE(m->poll_file_table, m->file_cnt);
632 DBG("MAIN: Rebuilding poll table: %d entries", m->file_cnt);
634 struct pollfd *p = m->poll_table;
635 struct main_file **pf = m->poll_file_table;
636 CLIST_FOR_EACH(struct main_file *, fi, m->file_list)
639 p->events = file_want_events(fi);
643 m->poll_table_obsolete = 0;
651 DBG("MAIN: Entering main_loop");
652 struct main_context *m = main_current();
657 timestamp_t wake = m->now + 1000000000;
659 switch (process_hooks(m))
669 wake = MIN(wake, m->timer_table[1]->expires);
671 int timeout = ((wake > m->now) ? wake - m->now : 0);
673 #ifdef CONFIG_UCW_EPOLL
675 DBG("MAIN: Epoll for %d fds and timeout %d ms", m->file_cnt, timeout);
676 int n = epoll_wait(m->epoll_fd, m->epoll_events, EPOLL_BUF_SIZE, timeout);
678 if (m->poll_table_obsolete)
679 rebuild_poll_table(m);
680 DBG("MAIN: Poll for %d fds and timeout %d ms", m->file_cnt, timeout);
681 int n = poll(m->poll_table, m->file_cnt, timeout);
684 DBG("\t-> %d events", n);
685 if (n < 0 && errno != EAGAIN && errno != EINTR)
686 die("(e)poll failed: %m");
687 timestamp_t old_now = m->now;
689 m->idle_time += m->now - old_now;
694 // Relink all files with a pending event to file_active_list
695 #ifdef CONFIG_UCW_EPOLL
696 for (int i=0; i<n; i++)
698 struct epoll_event *e = &m->epoll_events[i];
699 struct main_file *fi = e->data.ptr;
700 clist_remove(&fi->n);
701 clist_add_tail(&m->file_active_list, &fi->n);
702 fi->events = e->events;
705 struct pollfd *p = m->poll_table;
706 struct main_file **pf = m->poll_file_table;
707 for (uns i=0; i < m->file_cnt; i++)
710 struct main_file *fi = pf[i];
711 clist_remove(&fi->n);
712 clist_add_tail(&m->file_active_list, &fi->n);
713 fi->events = p[i].revents;
717 // Process the buffered file events
718 struct main_file *fi;
719 while (fi = clist_remove_head(&m->file_active_list))
721 clist_add_tail(&m->file_list, &fi->n);
722 if (fi->events & (POLLIN | POLLHUP | POLLERR))
725 DBG("MAIN: Read event on fd %d", fi->fd);
726 while (fi->read_handler && fi->read_handler(fi));
728 if (fi->events & (POLLOUT | POLLERR))
731 DBG("MAIN: Write event on fd %d", fi->fd);
732 while (fi->write_handler && fi->write_handler(fi));
740 static struct main_process mp;
741 static struct main_block_io fin, fout;
742 static struct main_hook hook;
743 static struct main_timer tm;
744 static struct main_signal sg;
748 static void dread(struct main_block_io *bio)
750 if (bio->rpos < bio->rlen)
752 msg(L_INFO, "Read EOF");
757 msg(L_INFO, "Read done");
758 block_io_read(bio, rb, sizeof(rb));
762 static void derror(struct main_block_io *bio, int cause)
764 msg(L_INFO, "Error: %m !!! (cause %d)", cause);
768 static void dwrite(struct main_block_io *bio UNUSED)
770 msg(L_INFO, "Write done");
773 static int dhook(struct main_hook *ho UNUSED)
775 msg(L_INFO, "Hook called");
779 static void dtimer(struct main_timer *tm)
781 msg(L_INFO, "Timer tick");
782 timer_add_rel(tm, 11000);
783 timer_add_rel(tm, 10000);
786 static void dentry(void)
788 msg(L_INFO, "*** SUBPROCESS START ***");
790 msg(L_INFO, "*** SUBPROCESS FINISH ***");
794 static void dexit(struct main_process *pr)
796 msg(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
799 static void dsignal(struct main_signal *sg UNUSED)
801 msg(L_INFO, "SIGINT received (use Ctrl-\\ to really quit)");
810 fin.read_done = dread;
811 fin.error_handler = derror;
812 block_io_add(&fin, 0);
813 block_io_read(&fin, rb, sizeof(rb));
815 fout.write_done = dwrite;
816 fout.error_handler = derror;
817 block_io_add(&fout, 1);
818 block_io_write(&fout, "Hello, world!\n", 14);
820 hook.handler = dhook;
824 timer_add_rel(&tm, 1000);
827 sg.handler = dsignal;
831 if (!process_fork(&mp))
837 msg(L_INFO, "Finished.");