*** VERSION 5.4.0 ***
[NeonServV5.git] / src / timeq.c
index 17b190488381576cfa9837b7c24709061258dc0c..9b03b981a3e6708c129e6449371c16dc3dbecb83 100644 (file)
@@ -1,4 +1,4 @@
-/* timeq.c - NeonServ v5.3
+/* timeq.c - NeonServ v5.4
  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
  * 
  * This program is free software: you can redistribute it and/or modify
@@ -29,9 +29,10 @@ void init_timeq() {
 void timeq_tick() {
     SYNCHRONIZE(synchronized);
     struct timeq_entry *entry, *next;
-    time_t now = time(0);
+    struct timeval now;
+    gettimeofday(&now, NULL);
     for(entry = timeq_events; entry; entry = next) {
-        if(entry->execute <= now) {
+        if(!timeval_is_bigger(entry->execute, now)) {
             next = entry->next;
             if(timeq_events == entry)
                 timeq_events = next;
@@ -43,8 +44,13 @@ void timeq_tick() {
     DESYNCHRONIZE(synchronized);
 }
 
-struct timeq_entry* timeq_add(int seconds, timeq_callback_t *callback, void *data) {
-    time_t now = time(0);
+struct timeq_entry* timeq_add(int seconds, int module_id, timeq_callback_t *callback, void *data) {
+    return timeq_uadd(seconds * 1000, module_id, callback, data);
+}
+
+struct timeq_entry* timeq_uadd(int useconds, int module_id, timeq_callback_t *callback, void *data) {
+    struct timeval now;
+    gettimeofday(&now, NULL);
     struct timeq_entry *entry = malloc(sizeof(*entry));
     if (!entry)
     {
@@ -52,13 +58,16 @@ struct timeq_entry* timeq_add(int seconds, timeq_callback_t *callback, void *dat
         return NULL;
     }
     SYNCHRONIZE(synchronized);
-    entry->execute = now + seconds;
+    now.tv_usec += (useconds % 1000);
+    now.tv_sec += (useconds / 1000);
+    entry->execute = now;
+    entry->module_id = module_id;
     entry->callback = callback;
     entry->data = data;
     entry->name = NULL;
     struct timeq_entry *next, *prev = NULL;
     for(next = timeq_events; next; next = next->next) {
-        if(next->execute >= entry->execute)
+        if(timeval_is_bigger(next->execute, now))
             break;
         else
             prev = next;
@@ -74,9 +83,13 @@ struct timeq_entry* timeq_add(int seconds, timeq_callback_t *callback, void *dat
     return entry;
 }
 
-struct timeq_entry* timeq_add_name(char *name, int seconds, timeq_callback_t *callback, void *data) {
+struct timeq_entry* timeq_add_name(char *name, int seconds, int module_id, timeq_callback_t *callback, void *data) {
+    return timeq_uadd_name(name, seconds * 1000, module_id, callback, data);
+}
+
+struct timeq_entry* timeq_uadd_name(char *name, int useconds, int module_id, timeq_callback_t *callback, void *data) {
     SYNCHRONIZE(synchronized);
-    struct timeq_entry *entry = timeq_add(seconds, callback, data);
+    struct timeq_entry *entry = timeq_uadd(useconds, module_id, callback, data);
     entry->name = strdup(name);
     DESYNCHRONIZE(synchronized);
     return entry;
@@ -137,3 +150,21 @@ int timeq_name_exists(char *name) {
     DESYNCHRONIZE(synchronized);
     return 0;
 }
+
+void unregister_module_timers(int module_id) {
+    SYNCHRONIZE(synchronized);
+    struct timeq_entry *centry, *next, *last = NULL;
+    for(centry = timeq_events; centry; centry = next) {
+        next = centry->next;
+        if(centry->module_id == module_id) {
+            if(last)
+                last->next = centry->next;
+            else
+                timeq_events = centry->next;
+            free(centry->name);
+            free(centry);
+        } else
+            last = centry;
+    }
+    DESYNCHRONIZE(synchronized);
+}