moved timeq loop to the "main thread" and made it more precise
authorpk910 <philipp@zoelle1.de>
Sat, 14 Jan 2012 08:39:20 +0000 (09:39 +0100)
committerpk910 <philipp@zoelle1.de>
Sat, 14 Jan 2012 08:57:56 +0000 (09:57 +0100)
src/cmd_neonserv_extscript.c
src/main.c
src/main.h
src/timeq.c
src/timeq.h

index d060d57e1ddc2155ca34d9bd7538aed04a1b0bd2..e4a4e2e345ca02688eb6a6a0182ef51e00e69826 100644 (file)
@@ -139,7 +139,7 @@ CMD_BIND(neonserv_cmd_extscript) {
     #ifndef WIN32
     fcntl(fileno(cache->pipe), F_SETFL, O_NONBLOCK);
     #endif
-    timeq_add(1, neonserv_cmd_extscript_callback, cache);
+    timeq_uadd(200, neonserv_cmd_extscript_callback, cache);
 }
 
 static TIMEQ_CALLBACK(neonserv_cmd_extscript_callback) {
@@ -163,7 +163,7 @@ static TIMEQ_CALLBACK(neonserv_cmd_extscript_callback) {
         else
             reply(cache->textclient, cache->user, "%s", command);
     }
-    timeq_add(1, neonserv_cmd_extscript_callback, cache);
+    timeq_uadd(200, neonserv_cmd_extscript_callback, cache);
 }
 
 
index 4ad2d0d5c0f13626048ecb4833c6f51266be678e..ffa830d2a90563b04f67c442a339feb45327596b 100644 (file)
@@ -103,11 +103,8 @@ void * thread_main(void *arg) {
         do {
             socket_loop(SOCKET_SELECT_TIME);
         } while(time(0) < socket_wait);
-        timeq_tick();
-        loop_bots();
         clearTempUsers();
         destroyEvents();
-        queue_loop();
     }
     running_threads--;
     return NULL;
@@ -168,6 +165,13 @@ main:
         running_threads++;
         pthread_create(&tid[tid_id], NULL, thread_main, NULL);
     }
+    int usleep_delay = 1000000 / TICKS_PER_SECOND;
+    while(running) {
+        timeq_tick();
+        loop_bots();
+        queue_loop();
+        usleep(usleep_delay);
+    }
     for(tid_id = 0; tid_id < worker_threads; tid_id++) {
         pthread_join(tid[tid_id], NULL);
     }
index 5002bcd6d0b3391e061be8a7b1ca649f7d9ca64e..329aa5d13c60a3c3072e2365985b6b173fa39503 100644 (file)
 #define BOTWAR_DETECTION_TIME 7
 #define BOTWAR_DETECTION_EVENTS 6
 #define REWHO_TIMEOUT   10 /* wait 10 seconds before WHO an unauthed user again */
+#define TICKS_PER_SECOND 10
 
 //valid nick chars
 #define VALID_NICK_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890{|}~[\\]^-_`"
 
 #define TEMPUSER_LIST_INDEX VALID_NICK_CHARS_FIRST_LEN
 
+#define timeval_is_bigger(x,y) ((x.tv_sec > y.tv_sec) || (x.tv_sec == y.tv_sec && x.tv_usec > y.tv_usec))
+
 extern time_t start_time;
 extern int statistics_enabled;
 #ifdef HAVE_THREADS
index 17b190488381576cfa9837b7c24709061258dc0c..41e4b1a3b1dec349c6547c191e87bd9defac812e 100644 (file)
@@ -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;
@@ -44,7 +45,12 @@ void timeq_tick() {
 }
 
 struct timeq_entry* timeq_add(int seconds, timeq_callback_t *callback, void *data) {
-    time_t now = time(0);
+    return timeq_uadd(seconds * 1000, callback, data);
+}
+
+struct timeq_entry* timeq_uadd(int useconds, timeq_callback_t *callback, void *data) {
+    struct timeval now;
+    gettimeofday(&now, NULL);
     struct timeq_entry *entry = malloc(sizeof(*entry));
     if (!entry)
     {
@@ -52,13 +58,15 @@ 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->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;
@@ -75,8 +83,12 @@ struct timeq_entry* timeq_add(int seconds, timeq_callback_t *callback, void *dat
 }
 
 struct timeq_entry* timeq_add_name(char *name, int seconds, timeq_callback_t *callback, void *data) {
+    return timeq_uadd_name(name, seconds * 1000, callback, data);
+}
+
+struct timeq_entry* timeq_uadd_name(char *name, int useconds, timeq_callback_t *callback, void *data) {
     SYNCHRONIZE(synchronized);
-    struct timeq_entry *entry = timeq_add(seconds, callback, data);
+    struct timeq_entry *entry = timeq_uadd(useconds, callback, data);
     entry->name = strdup(name);
     DESYNCHRONIZE(synchronized);
     return entry;
index 762df29a2faf2aeaa7d3e7749a6cad9b614b6b3b..4e943b7de8003ae4d38616e6e291cb7a71889ed5 100644 (file)
@@ -24,7 +24,7 @@ typedef TIMEQ_CALLBACK(timeq_callback_t);
 
 struct timeq_entry {
     char *name;
-    time_t execute;
+    struct timeval execute;
     timeq_callback_t *callback;
     void *data;
     
@@ -34,7 +34,9 @@ struct timeq_entry {
 void init_timeq();
 void timeq_tick();
 struct timeq_entry* timeq_add(int seconds, timeq_callback_t *callback, void *data);
+struct timeq_entry* timeq_uadd(int useconds, timeq_callback_t *callback, void *data);
 struct timeq_entry* timeq_add_name(char *name, int seconds, timeq_callback_t *callback, void *data);
+struct timeq_entry* timeq_uadd_name(char *name, int useconds, timeq_callback_t *callback, void *data);
 int timeq_del(struct timeq_entry* entry);
 int timeq_del_name(char *name);
 int timeq_name_exists(char *name);