/*
* UCW Library -- Main Loop
*
- * (c) 2004--2005 Martin Mares <mj@ucw.cz>
+ * (c) 2004--2006 Martin Mares <mj@ucw.cz>
*
* This software may be freely distributed and used according to the terms
* of the GNU Lesser General Public License.
#include <stdio.h>
#include <string.h>
-#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
gettimeofday(&tv, NULL);
main_now_seconds = tv.tv_sec;
main_now = (timestamp_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
- DBG("It's %Ld o'clock", (long long) main_now);
+ // DBG("It's %lld o'clock", (long long) main_now);
}
void
clist_init(&main_file_list);
clist_init(&main_hook_list);
clist_init(&main_process_list);
+ main_file_cnt = 0;
+ main_poll_table_obsolete = 1;
main_get_time();
}
void
timer_add(struct main_timer *tm, timestamp_t expires)
{
+ if (expires)
+ DBG("MAIN: Setting timer %p (expire at now+%lld)", tm, (long long)(expires-main_now));
+ else
+ DBG("MAIN: Clearing timer %p", tm);
if (tm->expires)
clist_remove(&tm->n);
tm->expires = expires;
void
file_add(struct main_file *fi)
{
+ DBG("MAIN: Adding file %p (fd=%d)", fi, fi->fd);
ASSERT(!fi->n.next);
clist_add_tail(&main_file_list, &fi->n);
fi->timer.handler = file_timer_expired;
main_file_cnt++;
main_poll_table_obsolete = 1;
if (fcntl(fi->fd, F_SETFL, O_NONBLOCK) < 0)
- log(L_ERROR, "Error setting fd %d to non-blocking mode: %m. Keep fingers crossed.", fi->fd);
+ msg(L_ERROR, "Error setting fd %d to non-blocking mode: %m. Keep fingers crossed.", fi->fd);
}
void
void
file_del(struct main_file *fi)
{
+ DBG("MAIN: Deleting file %p (fd=%d)", fi, fi->fd);
ASSERT(fi->n.next);
timer_del(&fi->timer);
clist_remove(&fi->n);
void
hook_add(struct main_hook *ho)
{
+ DBG("MAIN: Adding hook %p", ho);
ASSERT(!ho->n.next);
clist_add_tail(&main_hook_list, &ho->n);
}
void
hook_del(struct main_hook *ho)
{
+ DBG("MAIN: Deleting hook %p", ho);
ASSERT(ho->n.next);
clist_remove(&ho->n);
ho->n.next = ho->n.prev = NULL;
void
process_add(struct main_process *mp)
{
+ DBG("MAIN: Adding process %p (pid=%d)", mp, mp->pid);
ASSERT(!mp->n.next);
ASSERT(mp->handler);
clist_add_tail(&main_process_list, &mp->n);
void
process_del(struct main_process *mp)
{
+ DBG("MAIN: Deleting process %p (pid=%d)", mp, mp->pid);
ASSERT(mp->n.next);
clist_remove(&mp->n);
- mp->pid = 0;
mp->n.next = NULL;
}
int
process_fork(struct main_process *mp)
{
- ASSERT(!mp->pid);
pid_t pid = fork();
if (pid < 0)
{
main_debug(void)
{
#ifdef CONFIG_DEBUG
- log(L_DEBUG, "### Main loop status on %Ld", (long long)main_now);
- log(L_DEBUG, "\tActive timers:");
+ msg(L_DEBUG, "### Main loop status on %lld", (long long)main_now);
+ msg(L_DEBUG, "\tActive timers:");
struct main_timer *tm;
CLIST_WALK(tm, main_timer_list)
- log(L_DEBUG, "\t\t%p (expires %Ld, data %p)", tm, (long long)(tm->expires ? tm->expires-main_now : 999999), tm->data);
+ msg(L_DEBUG, "\t\t%p (expires %lld, data %p)", tm, (long long)(tm->expires ? tm->expires-main_now : 999999), tm->data);
struct main_file *fi;
- log(L_DEBUG, "\tActive files:");
+ msg(L_DEBUG, "\tActive files:");
CLIST_WALK(fi, main_file_list)
- log(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, eh %p, expires %Ld, data %p)",
+ msg(L_DEBUG, "\t\t%p (fd %d, rh %p, wh %p, eh %p, expires %lld, data %p)",
fi, fi->fd, fi->read_handler, fi->write_handler, fi->error_handler,
(long long)(fi->timer.expires ? fi->timer.expires-main_now : 999999), fi->data);
- log(L_DEBUG, "\tActive hooks:");
+ msg(L_DEBUG, "\tActive hooks:");
struct main_hook *ho;
CLIST_WALK(ho, main_hook_list)
- log(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
- log(L_DEBUG, "\tActive processes:");
+ msg(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
+ msg(L_DEBUG, "\tActive processes:");
struct main_process *pr;
CLIST_WALK(pr, main_process_list)
- log(L_DEBUG, "\t\t%p (pid %d, data %p)", pr, pr->pid, pr->data);
+ msg(L_DEBUG, "\t\t%p (pid %d, data %p)", pr, pr->pid, pr->data);
#endif
}
main_poll_table = xmalloc(sizeof(struct pollfd) * main_poll_table_size);
}
struct pollfd *p = main_poll_table;
- DBG("MAIN: Rebuliding poll table: %d of %d entries set", main_file_cnt, main_poll_table_size);
+ DBG("MAIN: Rebuilding poll table: %d of %d entries set", main_file_cnt, main_poll_table_size);
CLIST_WALK(fi, main_file_list)
{
p->fd = fi->fd;
main_poll_table_obsolete = 0;
}
-int
+void
main_loop(void)
{
DBG("MAIN: Entering main_loop");
timestamp_t wake = main_now + 1000000000;
while ((tm = clist_head(&main_timer_list)) && tm->expires <= main_now)
{
- DBG("MAIN: Timer %p expired at %Ld", tm, (long long) tm->expires);
+ DBG("MAIN: Timer %p expired at now-%lld", tm, (long long)(main_now - tm->expires));
tm->handler(tm);
}
+ int hook_min = HOOK_RETRY;
+ int hook_max = HOOK_SHUTDOWN;
CLIST_WALK_DELSAFE(ho, main_hook_list, tmp)
{
DBG("MAIN: Hook %p", ho);
- if (ho->handler(ho))
- wake = 0;
+ int ret = ho->handler(ho);
+ hook_min = MIN(hook_min, ret);
+ hook_max = MAX(hook_max, ret);
+ }
+ if (hook_min == HOOK_SHUTDOWN ||
+ hook_min == HOOK_DONE && hook_max == HOOK_DONE ||
+ main_shutdown)
+ {
+ DBG("MAIN: Shut down by %s", main_shutdown ? "main_shutdown" : "a hook");
+ return;
}
+ if (hook_max == HOOK_RETRY)
+ wake = 0;
if (main_poll_table_obsolete)
main_rebuild_poll_table();
if (!clist_empty(&main_process_list))
wake = 0;
}
}
- if (clist_empty(&main_file_list) &&
- clist_empty(&main_timer_list) &&
- clist_empty(&main_hook_list) &&
- clist_empty(&main_process_list))
- {
- DBG("MAIN: Nothing to do, exiting");
- return 0;
- }
- if (main_shutdown)
- {
- DBG("MAIN: Shutdown");
- return 1;
- }
/* FIXME: Here is a small race window where SIGCHLD can come unnoticed. */
if ((tm = clist_head(&main_timer_list)) && tm->expires < wake)
wake = tm->expires;