Make timeq_init() automatic.
[srvx.git] / src / timeq.c
index 63906b2a96b6700bf0bd05d9148c649e4b034f5d..aa73fbcf41b2a60158b16cde59136c5aedc4a376 100644 (file)
@@ -34,9 +34,10 @@ timeq_cleanup(void)
 {
     timeq_del(0, 0, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_FUNC|TIMEQ_IGNORE_DATA);
     heap_delete(timeq);
+    timeq = NULL;
 }
 
-void
+static void
 timeq_init(void)
 {
     timeq = heap_new(int_comparator);
@@ -47,6 +48,8 @@ time_t
 timeq_next(void)
 {
     void *time;
+    if (!timeq)
+        return ~0;
     heap_peek(timeq, &time, 0);
     return (time_t)time;
 }
@@ -60,6 +63,8 @@ timeq_add(time_t when, timeq_func func, void *data)
     ent->func = func;
     ent->data = data;
     w = (void*)when;
+    if (!timeq)
+        timeq_init();
     heap_insert(timeq, w, ent);
 }
 
@@ -93,7 +98,8 @@ timeq_del(time_t when, timeq_func func, void *data, int mask)
     extra.func = func;
     extra.data = data;
     extra.mask = mask;
-    heap_remove_pred(timeq, timeq_del_matching, &extra);
+    if (timeq)
+        heap_remove_pred(timeq, timeq_del_matching, &extra);
 }
 
 unsigned int