improved WHOHandler multi thread stability
[NeonServV5.git] / src / main.c
index 4ad2d0d5c0f13626048ecb4833c6f51266be678e..107d50eef806d9b8f31f816c9a9bd10ec24f8a27 100644 (file)
@@ -44,6 +44,8 @@ int statistics_enabled;
 TIMEQ_CALLBACK(main_statistics);
 #ifdef HAVE_THREADS
 int running_threads;
+pthread_mutex_t cache_sync;
+pthread_mutex_t whohandler_sync, whohandler_mass_sync;
 #endif
 
 void cleanup() {
@@ -103,11 +105,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;
@@ -135,6 +134,12 @@ main:
     
     statistics_enabled = get_int_field("statistics.enable");
     
+    #ifdef HAVE_THREADS
+    THREAD_MUTEX_INIT(cache_sync);
+    THREAD_MUTEX_INIT(whohandler_sync);
+    THREAD_MUTEX_INIT(whohandler_mass_sync);
+    #endif
+    
     queue_init();
     init_sockets();
     init_timeq();
@@ -168,6 +173,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);
     }