]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/mainloop.c
hashtable: Implemented new HASH_TABLE_VARS parameter.
[libucw.git] / ucw / mainloop.c
index a9b5e3e1a4d97070e2f3e8fc5a71054baa866449..4138aae1ec237aa42d79d90a56f6833f901b42b4 100644 (file)
@@ -37,7 +37,7 @@ static main_timer_table_t main_timer_table;
 #define MAIN_TIMER_LESS(x,y) ((x)->expires < (y)->expires)
 #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))
 
-clist main_file_list, main_hook_list, main_process_list;
+clist main_file_list, main_hook_list, main_hook_done_list, main_process_list;
 static uns main_file_cnt;
 static uns main_poll_table_obsolete, main_poll_table_size;
 static struct pollfd *main_poll_table;
@@ -68,6 +68,7 @@ main_init(void)
   main_timer_cnt = 0;
   clist_init(&main_file_list);
   clist_init(&main_hook_list);
+  clist_init(&main_hook_done_list);
   clist_init(&main_process_list);
   main_file_cnt = 0;
   main_poll_table_obsolete = 1;
@@ -81,19 +82,36 @@ timer_add(struct main_timer *tm, timestamp_t 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)
+  if (tm->expires < expires)
     {
-      ASSERT(tm->index && tm->index <= main_timer_cnt);
-      HEAP_DELETE(struct main_timer *, main_timer_table.ptr, main_timer_cnt, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
-      tm->index = 0;
+      if (!tm->expires)
+       {
+         tm->expires = expires;
+         tm->index = ++main_timer_cnt;
+         main_timer_table_grow(&main_timer_table, tm->index + 1);
+         main_timer_table.ptr[tm->index] = tm;
+         HEAP_INSERT(struct main_timer *, main_timer_table.ptr, main_timer_cnt, MAIN_TIMER_LESS, MAIN_TIMER_SWAP);
+       }
+      else
+       {
+         tm->expires = expires;
+         HEAP_INCREASE(struct main_timer *, main_timer_table.ptr, main_timer_cnt, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
+       }
     }
-  tm->expires = expires;
-  if (expires)
+  else if (tm->expires > expires)
     {
-      tm->index = ++main_timer_cnt;
-      main_timer_table_grow(&main_timer_table, tm->index + 1);
-      main_timer_table.ptr[tm->index] = tm;
-      HEAP_INSERT(struct main_timer *, main_timer_table.ptr, main_timer_cnt, MAIN_TIMER_LESS, MAIN_TIMER_SWAP);
+      if (!expires)
+       {
+         ASSERT(tm->index && tm->index <= main_timer_cnt);
+         HEAP_DELETE(struct main_timer *, main_timer_table.ptr, main_timer_cnt, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
+         tm->index = 0;
+         tm->expires = 0;
+       }
+      else
+       {
+         tm->expires = expires;
+         HEAP_DECREASE(struct main_timer *, main_timer_table.ptr, main_timer_cnt, MAIN_TIMER_LESS, MAIN_TIMER_SWAP, tm->index);
+       }
     }
 }
 
@@ -396,6 +414,8 @@ main_debug(void)
        (long long)(fi->timer.expires ? fi->timer.expires-main_now : 999999), fi->data);
   msg(L_DEBUG, "\tActive hooks:");
   struct main_hook *ho;
+  CLIST_WALK(ho, main_hook_done_list)
+    msg(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
   CLIST_WALK(ho, main_hook_list)
     msg(L_DEBUG, "\t\t%p (func %p, data %p)", ho, ho->handler, ho->data);
   msg(L_DEBUG, "\tActive processes:");
@@ -452,17 +472,15 @@ main_loop(void)
        }
       int hook_min = HOOK_RETRY;
       int hook_max = HOOK_SHUTDOWN;
-      clist hook_done;
-      clist_init(&hook_done);
       while (ho = clist_remove_head(&main_hook_list))
        {
-         clist_add_tail(&hook_done, &ho->n);
+         clist_add_tail(&main_hook_done_list, &ho->n);
          DBG("MAIN: Hook %p", ho);
          int ret = ho->handler(ho);
          hook_min = MIN(hook_min, ret);
          hook_max = MAX(hook_max, ret);
        }
-      clist_move(&main_hook_list, &hook_done);
+      clist_move(&main_hook_list, &main_hook_done_list);
       if (hook_min == HOOK_SHUTDOWN ||
          hook_min == HOOK_DONE && hook_max == HOOK_DONE ||
          main_shutdown)