improved WHOHandler multi thread stability
[NeonServV5.git] / src / WHOHandler.c
index a8a55d91c693956cebd57329babecb6ec73fa829..63232f7c7998d0be5451118cecb9cedfb662172b 100644 (file)
 
 struct WHOQueueEntry {
     char type;
+    int whoid;
     struct ChanNode *chan;
     struct UserNode *user;
     struct WHOQueueEntry *next;
     void *callback[MAXCALLBACKS];
     void *data[MAXCALLBACKS];
+    #ifdef HAVE_THREADS
+    unsigned long lock_tid;
+    pthread_mutex_t lock_mutex;
+    int lock_count;
+    #endif
 };
+
+static int checkWHOID(struct ClientSocket *client, int whoid) {
+    struct WHOQueueEntry *entry;
+    for(entry = client->whoqueue_first; entry; entry = entry->next) {
+        if(entry->whoid == whoid)
+            return 1;
+    }
+    return 0;
+}
+
 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
+    SYNCHRONIZE(whohandler_sync);
+    int whoid = 0;
+    do {
+        whoid++;
+    } while(checkWHOID(client, whoid) && whoid < 1000);
+    if(whoid == 1000) {
+        DESYNCHRONIZE(whohandler_sync);
+        return NULL;
+    }
     struct WHOQueueEntry *entry = malloc(sizeof(*entry));
     if (!entry)
     {
         perror("malloc() failed");
+        DESYNCHRONIZE(whohandler_sync);
         return NULL;
     }
-    SYNCHRONIZE(cache_sync);
     entry->next = NULL;
+    entry->whoid = whoid;
+    #ifdef HAVE_THREADS
+    entry->lock_tid = 0;
+    THREAD_MUTEX_INIT(entry->lock_mutex);
+    entry->lock_count = 0;
+    #endif
     if(client->whoqueue_last) {
         client->whoqueue_last->next = entry;
     } else {
         client->whoqueue_first = entry;
     }
     client->whoqueue_last = entry;
-    DESYNCHRONIZE(cache_sync);
+    DESYNCHRONIZE(whohandler_sync);
     return entry;
 }
 
-static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
+static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int whoid, int freeEntry) {
     if(!client->whoqueue_first) return NULL;
-    SYNCHRONIZE(cache_sync);
-    struct WHOQueueEntry *entry = client->whoqueue_first;
+    SYNCHRONIZE(whohandler_sync);
+    struct WHOQueueEntry *entry;
+    for(entry = client->whoqueue_first; entry; entry = entry->next) {
+        if(entry->whoid == whoid)
+            break;
+    }
+    if(!entry) {
+        DESYNCHRONIZE(whohandler_sync);
+        return NULL;
+    }
     if(freeEntry) {
         client->whoqueue_first = entry->next;
         if(entry == client->whoqueue_last) {
             client->whoqueue_last = NULL;
         }
+        #ifdef HAVE_THREADS
+        entry->lock_tid = -1;
+        if(entry->lock_count) {
+            int i;
+            for(i = 0; i < entry->lock_count; i++) {
+                DESYNCHRONIZE(entry->lock_mutex); //unlock ALL
+            }
+        }
+        #endif
     }
-    DESYNCHRONIZE(cache_sync);
+    DESYNCHRONIZE(whohandler_sync);
     return entry;
 }
 
 void clear_whoqueue(struct ClientSocket *client) {
     if(!client->whoqueue_first) return;
-    SYNCHRONIZE(cache_sync);
+    SYNCHRONIZE(whohandler_sync);
     struct WHOQueueEntry *entry, *next;
     for(entry = client->whoqueue_first; entry; entry = next) {
         next = entry->next;
+        #ifdef HAVE_THREADS
+        entry->lock_tid = -1;
+        if(entry->lock_count) {
+            int i;
+            for(i = 0; i < entry->lock_count; i++) {
+                DESYNCHRONIZE(entry->lock_mutex); //unlock ALL
+            }
+        }
+        #endif
         free(entry);
     }
     client->whoqueue_last = NULL;
     client->whoqueue_first = NULL;
-    DESYNCHRONIZE(cache_sync);
+    DESYNCHRONIZE(whohandler_sync);
+}
+
+#if HAVE_THREADS
+void whohandler_end_of_recv(struct ClientSocket *client) {
+    SYNCHRONIZE(whohandler_sync);
+    unsigned long tid = syscall(SYS_gettid);
+    struct WHOQueueEntry *entry;
+    for(entry = client->whoqueue_first; entry; entry = entry->next) {
+        if(entry->lock_tid == tid) {
+            entry->lock_tid = 0;
+            DESYNCHRONIZE(entry->lock_mutex);
+        }
+    }
+    DESYNCHRONIZE(whohandler_sync);
 }
+#endif
 
 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
     struct ClientSocket *bot;
@@ -114,7 +186,7 @@ void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *dat
         entry->data[0] = data;
         for(i = 1; i < MAXCALLBACKS; i++)
             entry->data[i] = NULL;
-        putsock(bot, "WHO %s,%d %%tuihnaf,%d", chan->name, entry->type, entry->type);
+        putsock(bot, "WHO %s,%d %%tuihnaf,%d", chan->name, entry->whoid, entry->whoid);
     } else
         callback(bot, chan, data);
 }
@@ -149,7 +221,7 @@ void _get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t cal
         entry->data[0] = data;
         for(i = 1; i < MAXCALLBACKS; i++)
             entry->data[i] = NULL;
-        putsock(bot, "WHO %s,%d d%%tuihnaf,%d", chan->name, entry->type, entry->type);
+        putsock(bot, "WHO %s,%d d%%tuihnaf,%d", chan->name, entry->whoid, entry->whoid);
     } else
         callback(bot, chan, data);
 }
@@ -197,18 +269,33 @@ void get_userauth(struct UserNode *user, userauth_callback_t callback, void *dat
     for(i = 1; i < MAXCALLBACKS; i++)
             entry->data[i] = NULL;
     //WHO ".$user->getNick().",".$id." %tuhna,".$id
-    putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
+    putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->whoid, entry->whoid);
 }
 
+static void _recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc);
 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
+    _recv_whohandler_354(client, argv, argc);
+}
+
+static void _recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
     int i;
     if(argc < 2) return;
     int type = atoi(argv[1]);
     if(!type) return;
-    if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
-    struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
-    if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
-    if(type & WHOQUEUETYPE_USERLIST) {
+    struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, type, 0);
+    if(entry == NULL) return;
+    #ifdef HAVE_THREADS
+    unsigned long tid = syscall(SYS_gettid);
+    if(entry->lock_tid != tid) {
+        entry->lock_count++;
+        SYNCHRONIZE(entry->lock_mutex);
+        if(entry->lock_tid == -1) {
+            return; //entry has been destroyed
+        }
+        entry->lock_tid = tid;
+    }
+    #endif
+    if(entry->type & WHOQUEUETYPE_USERLIST) {
         if(argc < 7) return;
         //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
         struct ChanNode *chan = entry->chan;
@@ -267,7 +354,7 @@ void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int
             strcpy(user->auth, argv[7]);
             user->flags |= USERFLAG_ISAUTHED;
         }
-    } else if((type & WHOQUEUETYPE_USERAUTH) && !(entry->type & WHOQUEUETYPE_FOUND)) {
+    } else if((entry->type & WHOQUEUETYPE_USERAUTH) && !(entry->type & WHOQUEUETYPE_FOUND)) {
         //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
         entry->type |= WHOQUEUETYPE_FOUND;
         entry->user->last_who = time(0);
@@ -283,16 +370,32 @@ void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int
     }
 }
 
+static void _recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc);
 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
+    _recv_whohandler_315(client, argv, argc);
+}
+
+static void _recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
     if(argc < 2) return;
     char *typestr = strstr(argv[1], ",");
     if(!typestr) return;
     typestr++;
     int type = atoi(typestr);
-    if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
-    struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
-    if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
-    if(type & WHOQUEUETYPE_USERLIST) {
+    struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, type, 0);
+    if(entry == NULL) return;
+    #ifdef HAVE_THREADS
+    unsigned long tid = syscall(SYS_gettid);
+    if(entry->lock_tid != tid) {
+        entry->lock_count++;
+        SYNCHRONIZE(entry->lock_mutex);
+        if(entry->lock_tid == -1) {
+            return; //entry has been destroyed
+        }
+        entry->lock_tid = tid;
+    }
+    #endif
+    getNextWHOQueueEntry(client, type, 1);
+    if(entry->type & WHOQUEUETYPE_USERLIST) {
         //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
         entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
         userlist_callback_t *callback;
@@ -312,7 +415,7 @@ void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int
                 }
             }
         }
-    } else if(type & WHOQUEUETYPE_USERAUTH) {
+    } else if(entry->type & WHOQUEUETYPE_USERAUTH) {
         if(!(entry->type & WHOQUEUETYPE_FOUND)) {
             userauth_callback_t *callback;
             int i;